Skip to content

Commit 34f4945

Browse files
authored
Merge pull request #38 from Salvoxia/dev
Merge features to main
2 parents 2fcb915 + 49e64e5 commit 34f4945

File tree

4 files changed

+542
-93
lines changed

4 files changed

+542
-93
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ COPY immich_auto_album.py requirements.txt docker/immich_auto_album.sh docker/se
66
RUN pip install --no-cache-dir -r /script/requirements.txt \
77
&& chmod +x /script/setup_cron.sh /script/immich_auto_album.sh \
88
&& rm -rf /tmp/* /var/tmp/* /var/cache/apk/* /var/cache/distfiles/*
9-
9+
ENV IS_DOCKER=1
1010
WORKDIR /script
1111
CMD ["sh", "-c", "/script/setup_cron.sh && crond -f"]

README.md

Lines changed: 147 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ This script is mostly based on the following original script: [REDVM/immich_auto
1919
2. [Usage (Docker)](#docker)
2020
3. [Choosing the correct `root_path`](#choosing-the-correct-root_path)
2121
4. [How It Works (with Examples)](#how-it-works)
22-
5. [Cleaning Up Albums](#cleaning-up-albums)
22+
5. [Automatic Album Sharing](#automatic-album-sharing)
23+
6. [Cleaning Up Albums](#cleaning-up-albums)
24+
7. [Dealing with External Library Changes](#dealing-with-external-library-changes)
2325

2426
## Usage
2527
### Bare Python Script
@@ -34,7 +36,9 @@ This script is mostly based on the following original script: [REDVM/immich_auto
3436
```
3537
3. Run the script
3638
```
37-
usage: immich_auto_album.py [-h] [-r ROOT_PATH] [-u] [-a ALBUM_LEVELS] [-s ALBUM_SEPARATOR] [-c CHUNK_SIZE] [-C FETCH_CHUNK_SIZE] [-l {CRITICAL,ERROR,WARNING,INFO,DEBUG}] [-k] [-i IGNORE] [-m {CREATE,CLEANUP,DELETE_ALL}] [-d] root_path api_url api_key
39+
usage: immich_auto_album.py [-h] [-r ROOT_PATH] [-u] [-a ALBUM_LEVELS] [-s ALBUM_SEPARATOR] [-c CHUNK_SIZE] [-C FETCH_CHUNK_SIZE] [-l {CRITICAL,ERROR,WARNING,INFO,DEBUG}] [-k] [-i IGNORE] [-m {CREATE,CLEANUP,DELETE_ALL}] [-d] [-x SHARE_WITH] [-o {viewer,editor}]
40+
[-S {0,1,2}] [-O {False,asc,desc}]
41+
root_path api_url api_key
3842

3943
Create Immich Albums from an external library path based on the top level folders
4044

@@ -66,6 +70,16 @@ This script is mostly based on the following original script: [REDVM/immich_auto
6670
Mode for the script to run with. CREATE = Create albums based on folder names and provided arguments; CLEANUP = Create album nmaes based on current images and script arguments, but delete albums if they exist; DELETE_ALL = Delete all
6771
albums. If the mode is anything but CREATE, --unattended does not have any effect. Only performs deletion if -d/--delete-confirm option is set, otherwise only performs a dry-run. (default: CREATE)
6872
-d, --delete-confirm Confirm deletion of albums when running in mode CLEANUP or DELETE_ALL. If this flag is not set, these modes will perform a dry run only. Has no effect in mode CREATE (default: False)
73+
-x SHARE_WITH, --share-with SHARE_WITH
74+
A user name (or email address of an existing user) to share newly created albums with. Sharing only happens if the album was actually created, not if new assets were added to an existing album. If the the share role should be specified by
75+
user, the format <userName>=<shareRole> must be used, where <shareRole> must be one of 'viewer' or 'editor'. May be specified multiple times to share albums with more than one user. (default: None)
76+
-o {viewer,editor}, --share-role {viewer,editor}
77+
The default share role for users newly created albums are shared with. Only effective if --share-with is specified at least once and the share role is not specified within --share-with. (default: viewer)
78+
-S {0,1,2}, --sync-mode {0,1,2}
79+
Synchronization mode to use. Synchronization mode helps synchronizing changes in external libraries structures to Immich after albums have already been created. Possible Modes: 0 = do nothing; 1 = Delete any empty albums; 2 = Trigger
80+
offline asset removal (REQUIRES API KEY OF AN ADMIN USER!) (default: 0)
81+
-O {False,asc,desc}, --album-order {False,asc,desc}
82+
Set sorting order for newly created albums to newest or oldest file first, Immich defaults to newest file first (default: False)
6983
```
7084
7185
__Plain example without optional arguments:__
@@ -85,16 +99,20 @@ The environment variables are analoguous to the script's command line arguments.
8599
| ROOT_PATH | yes | A single or a comma separated list of import paths for external libraries in Immich. <br>Refer to [Choosing the correct `root_path`](#choosing-the-correct-root_path).|
86100
| API_URL | yes | The root API URL of immich, e.g. https://immich.mydomain.com/api/ |
87101
| API_KEY | yes | The Immich API Key to use
88-
| CRON_EXPRESSION | yes | A [crontab-style expression](https://crontab.guru/) (e.g. "0 * * * *") to perform album creation on a schedule (e.g. every hour). |
89-
| ALBUM_LEVELS | no | Number of sub-folders or range of sub-folder levels below the root path used for album name creation. Positive numbers start from top of the folder structure, negative numbers from the bottom. Cannot be 0. If a range should be set, the start level and end level must be separated by a comma. <br>Refer to [How it works](#how-it-works) for a detailed explanation and examples. |
90-
| ALBUM_SEPARATOR | no | Separator string to use for compound album names created from nested folders. Only effective if -a is set to a value > 1 (default: " ") |
91-
| CHUNK_SIZE | no | Maximum number of assets to add to an album with a single API call (default: 2000) |
92-
| FETCH_CHUNK_SIZE | no | Maximum number of assets to fetch with a single API call (default: 5000) |
93-
| LOG_LEVEL | no | Log level to use (default: INFO), allowed values: CRITICAL,ERROR,WARNING,INFO,DEBUG |
102+
| CRON_EXPRESSION | yes | A [crontab-style expression](https://crontab.guru/) (e.g. `0 * * * *`) to perform album creation on a schedule (e.g. every hour). |
103+
| ALBUM_LEVELS | no | Number of sub-folders or range of sub-folder levels below the root path used for album name creation. Positive numbers start from top of the folder structure, negative numbers from the bottom. Cannot be `0`. If a range should be set, the start level and end level must be separated by a comma. <br>Refer to [How it works](#how-it-works) for a detailed explanation and examples. |
104+
| ALBUM_SEPARATOR | no | Separator string to use for compound album names created from nested folders. Only effective if `-a` is set to a value `> 1`(default: "` `") |
105+
| CHUNK_SIZE | no | Maximum number of assets to add to an album with a single API call (default: `2000`) |
106+
| FETCH_CHUNK_SIZE | no | Maximum number of assets to fetch with a single API call (default: `5000`) |
107+
| LOG_LEVEL | no | Log level to use (default: INFO), allowed values: `CRITICAL`,`ERROR`,`WARNING`,`INFO`,`DEBUG` |
94108
| INSECURE | no | Set to `true` to disable SSL verification for the Immich API server, useful for self-signed certificates (default: `false`), allowed values: `true`, `false` |
95-
| INSECURE | no | A string containing a list of folders, sub-folder sequences or file names separated by ':' that will be ignored. |
96-
| MODE | no | Mode for the script to run with. <br> __CREATE__ = Create albums based on folder names and provided arguments<br>__CLEANUP__ = Create album nmaes based on current images and script arguments, but delete albums if they exist <br> __DELETE_ALL__ = Delete all albums. <br> If the mode is anything but CREATE, `--unattended` does not have any effect. <br> (default: CREATE). <br>Refer to [Cleaning Up Albums](#cleaning-up-albums). |
97-
| DELETE_CONFIRM | no | Confirm deletion of albums when running in mode CLEANUP or DELETE_ALL. If this flag is not set, these modes will perform a dry run only. Has no effect in mode CREATE (default: False). <br>Refer to [Cleaning Up Albums](#cleaning-up-albums).|
109+
| INSECURE | no | A string containing a list of folders, sub-folder sequences or file names separated by '`:`' that will be ignored. |
110+
| MODE | no | Mode for the script to run with. <br> __`CREATE`__ = Create albums based on folder names and provided arguments<br>__`CLEANUP`__ = Create album nmaes based on current images and script arguments, but delete albums if they exist <br> __`DELETE_ALL`__ = Delete all albums. <br> If the mode is anything but `CREATE`, `--unattended` does not have any effect. <br> (default: `CREATE`). <br>Refer to [Cleaning Up Albums](#cleaning-up-albums). |
111+
| DELETE_CONFIRM | no | Confirm deletion of albums when running in mode `CLEANUP` or `DELETE_ALL`. If this flag is not set, these modes will perform a dry run only. Has no effect in mode `CREATE` (default: `False`). <br>Refer to [Cleaning Up Albums](#cleaning-up-albums).|
112+
| SHARE_WITH | no | A single or a colon (`:`) separated list of existing user names (or email addresses of existing users) to share newly created albums with. If the the share role should be specified by user, the format <userName>=<shareRole> must be used, where <shareRole> must be one of `viewer` or `editor`. May be specified multiple times to share albums with more than one user. (default: None) Sharing only happens if an album is actually created, not if new assets are added to it. <br>Refer to [Automatic Album Sharing](#automatic-album-sharing).|
113+
| SHARE_ROLE | no | The role for users newly created albums are shared with. Only effective if `SHARE_WITH` is not empty and no explicit share role was specified for at least one user. (default: viewer), allowed values: `viewer`, `editor` |
114+
| SYNC_MODE | no | Synchronization mode to use. Synchronization mode helps synchronizing changes in external libraries structures to Immich after albums have already been created. Possible Modes: <br>`0` = do nothing<br>`1` = Delete any empty albums<br>`2` = Trigger offline asset removal (REQUIRES API KEY OF AN ADMIN USER!)<br>(default: `0`)<br>Refer to [Dealing with External Library Changes](#dealing-with-external-library-changes). |
115+
| ALBUM_ORDER | no | Set sorting order for newly created albums to newest (`desc`) or oldest (`asc`) file first, Immich defaults to newest file first, allowed values: `asc`, `desc` |
98116

99117
#### Run the container with Docker
100118

@@ -121,8 +139,6 @@ docker run --name immich-folder-album-creator -e TZ="Europe/Berlin" -e CRON_EXPR
121139

122140
Adding the container to Immich's `docker-compose.yml` file:
123141
```yml
124-
version: "3.8"
125-
126142
#
127143
# WARNING: Make sure to use the docker-compose.yml of the current release:
128144
#
@@ -242,6 +258,52 @@ Albums created for `root_path = /external_libs/photos/Birthdays`:
242258
243259
Since Immich does not support real nested albums ([yet?](https://github.com/immich-app/immich/discussions/2073)), neither does this script.
244260
261+
## Automatic Album Sharing
262+
263+
The scripts support sharing newly created albums with a list of existing users. The sharing role (`viewer` or `editor`) can be specified for all users at once or individually per user.
264+
265+
### Album Sharing Examples (Bare Python Script)
266+
Two arguments control this feature:
267+
- `-o / --share-role`: The default role for users an album is shared with. Allowed values are `viewer` or `editor`. This argument is optional and defaults to `viewer`.
268+
- `-x / --share-with`: Specify once per user to share with. The value should be either the user name or the user's email address as specified in Immich. If the user name is used and it contains blanks ` `, it must be wrapped in double quotes `"`. To override the default share role and specify a role explicitly for this user, the format `<userName>=<shareRole>` must be used (refer to examples below).
269+
270+
To share new albums with users `User A` and `User B` as `viewer`, use the following call:
271+
```bash
272+
python3 ./immich_auto_album.py --share-with "User A" --share-with "User B" /path/to/external/lib https://immich.mydomain.com/api thisIsMyApiKeyCopiedFromImmichWebGui
273+
```
274+
275+
To share new albums with users `User A` and `User B` as `editor`, use the following call:
276+
```bash
277+
python3 ./immich_auto_album.py --share-with "User A" --share-with "User B" --share-role "editor" /path/to/external/lib https://immich.mydomain.com/api thisIsMyApiKeyCopiedFromImmichWebGui
278+
```
279+
280+
To share new albums with users `User A` and a user with mail address `userB@mydomain.com`, but `User A` should be an editor, use the following call:
281+
```bash
282+
python3 ./immich_auto_album.py --share-with "User A=editor" --share-with "userB@mydomain.com" /path/to/external/lib https://immich.mydomain.com/api thisIs
283+
```
284+
285+
286+
### Album Sharing Examples (Docker)
287+
Two environment variables control this feature:
288+
- `SHARE_ROLE`: The default role for users an album is shared with. Allowed values are `viewer` or `editor`. This argument is optional and defaults to `viewer`.
289+
- `SHARE_WITH`: A colon `:` separated list of either names or email addresses (or a mix) of existing users. To override the default share role and specify a role explicitly for each user, the format `<userName>=<shareRole>` must be used (refer to examples below).
290+
291+
To share new albums with users `User A` and `User B` as `viewer`, use the following call:
292+
```bash
293+
docker run -e SHARE_WITH="User A:User B" -e UNATTENDED="1" -e API_URL="https://immich.mydomain.com/api/" -e API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -e ROOT_PATH="/external_libs/photos" salvoxia/immich-folder-album-creator:latest /script/immich_auto_album.sh
294+
```
295+
296+
To share new albums with users `User A` and `User B` as `editor`, use the following call:
297+
```bash
298+
docker run -e SHARE_WITH="User A:User B" -e SHARE_ROLE="editor" -e UNATTENDED="1" -e API_URL="https://immich.mydomain.com/api/" -e API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -e ROOT_PATH="/external_libs/photos" salvoxia/immich-folder-album-creator:latest /script/immich_auto_album.sh
299+
```
300+
301+
To share new albums with users `User A` and a user with mail address `userB@mydomain.com`, but `User A` should be an editor, use the following call:
302+
```bash
303+
docker run -e SHARE_WITH="User A=editor:userB@mydomain.com" -e UNATTENDED="1" -e API_URL="https://immich.mydomain.com/api/" -e API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -e ROOT_PATH="/external_libs/photos" salvoxia/immich-folder-album-creator:latest /script/immich_auto_album.sh
304+
```
305+
306+
245307
## Cleaning Up Albums
246308
247309
The script supports differnt run modes (option `-m`/`--mode` or env variable `MODE` for Docker). The default mode is `CREATE`, which is used to create albums.
@@ -254,3 +316,75 @@ To actually delete albums, the option `-d/--delete-confirm` (or env variable `DE
254316
255317
__WARNING ⚠️__
256318
Deleting albums cannot be undone! The only option is to let the script run again and create new albums base on the passed arguments and current assets in Immich.
319+
320+
## Dealing with External Library Changes
321+
322+
Due to their nature, external libraries may be changed by the user without Immich having any say in it.
323+
Two examples of this are the user deleting or renaming folders in their external libraries after the script has created albums, or the user moving single files from one folder to another. The script would create new albums from renamed folders or add images to their new album after they have been moved.
324+
Immich itself deals with this by marking images/videos it no longer sees in their original location as "offline". This can lead to albums being completely populated by "offline" files only (if the folder was renamed or deleted) while they exist normally in a new album or with single images being offline in one album, while existing normally in their new albums.
325+
There is an administrative option to trigger deletion of "offline assets" from libraries in the Immich Admin Interface. However, in the first example this might leave behind empty albums if the album only contained offline files.
326+
327+
To clean up this mess this script offers an option called `Sync Mode`. It is an optional argument / environment variable that may have values from 0 to 2.
328+
The following behaviors wil be triggered by the different values:
329+
- `0`: No syncing (default)
330+
- `1`: Delete all empty albums at the end of a run
331+
- `2`: Trigger offline asset removal for all libraires (⚠️ This function requires the API key of an Admin User!)
332+
333+
The complete syncing process consists of two stages:
334+
1. Trigger Offline Asset Removal
335+
This will start a background job for each library to find and delete files from Immich's database that are marked as 'offline'. It is asynchronous and might take some time (depending on the size of the databases and the numver of offline files).
336+
Offline Asset Removal can only be triggered by Administrators, so the API Key used for this sync mode must be one of an Administrator. This might not be the same you use for creating albums!
337+
338+
2. Delete Empty Albums
339+
Step 1 might leave behind empty albums for a different user. The script is capable of removing empty albums at the end of a run to clean up after Offline Asset Removal.
340+
⚠️ It is __not__ possible for the script to distinguish between an album that was left behind empty after Offline Asset Removal and a manually created album with no images added to it! All empty albums of that user will be deleted!
341+
342+
It is up to you whether you want to use the full capabilities Sync Mode offers, parts of it or none.
343+
An example for the Immich `docker-compose.yml` stack when using full Sync Mode might look like this:
344+
```yml
345+
#
346+
# WARNING: Make sure to use the docker-compose.yml of the current release:
347+
#
348+
# https://github.com/immich-app/immich/releases/latest/download/docker-compose.yml
349+
#
350+
# The compose file on main may not be compatible with the latest release.
351+
#
352+
353+
name: immich
354+
355+
services:
356+
immich-server:
357+
container_name: immich_server
358+
volumes:
359+
- /path/to/my/photos:/external_libs/photos
360+
...
361+
immich-folder-album-creator-offline-asset-removal:
362+
container_name: immich_folder_album_creator
363+
image: salvoxia/immich-folder-album-creator:latest
364+
restart: unless-stopped
365+
environment:
366+
API_URL: http://immich_server:3001/api
367+
# Admin User API Key
368+
API_KEY: yyyyyyyyyyyyyyyyyy
369+
ROOT_PATH: /thisIsADummyPathThatDoesNotExist
370+
CRON_EXPRESSION: "0 * * * *"
371+
TZ: Europe/Berlin
372+
# Regularly trigger offline asset removal
373+
SYNC_MODE: "2"
374+
immich-folder-album-creator:
375+
container_name: immich_folder_album_creator
376+
image: salvoxia/immich-folder-album-creator:latest
377+
restart: unless-stopped
378+
environment:
379+
API_URL: http://immich_server:3001/api
380+
API_KEY: xxxxxxxxxxxxxxxxx
381+
ROOT_PATH: /external_libs/photos
382+
# Run 10 minutes past the hour to give Offline Asset Removal time to work
383+
CRON_EXPRESSION: "10 * * * *"
384+
TZ: Europe/Berlin
385+
# Delete empty albums after each run
386+
SYNC_MODE: "1"
387+
```
388+
389+
The `immich-folder-album-creator-offline-asset-removal` service has the one and only purpose of triggering Offline Asset Removal, since we do not want to create albums for the Admin User. Thus, a dummy `ROOT_PATH` is passed so that the script will never find any assets to create albums from. `SYNC_MODE` is set to `2` to regularly trigger Offline Asset Removal.
390+
The `immich-folder-album-creator` service is the normal service for creating albums from an external library. The cron job triggers 10 minutes past the hour, whereas Offline Asset Removal is trigger at the full hour. This gives the job 10 minutes of time to run before any empty albums are deleted at the end of the run due to `SYNC_MODE` being set to `1`.

0 commit comments

Comments
 (0)