1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#!/usr/bin/env bash
# Upload images to Immich, skipping duplicates via SHA1 checksum.
# Usage: immich-upload <file_or_directory>
#
# If <file_or_directory> is a single file, upload just that file.
# If it is a directory, recursively find all image files and upload them.
# Duplicates are detected via the bulk-upload-check API before uploading.
set -euo pipefail
IMMICH_LAN_URL="http://immich.f3s.lan.buetow.org" # LAN-only, fast when on home network
IMMICH_PUBLIC_URL="https://immich.f3s.buetow.org" # public, works from anywhere
IMMICH_URL="" # auto-detected in main()
API_KEY_FILE="$HOME/.immich_paul_key"
# Supported image extensions (case-insensitive)
IMAGE_EXTENSIONS='\.(jpg|jpeg|png|gif|webp|heic|heif|raw|cr2|nef|arw|dng|tiff|tif|bmp)$'
# ------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------
die() { echo "ERROR: $1" >&2; exit 1; }
warn() { echo "WARN: $1" >&2; }
info() { echo "==> $1"; }
# Probe the Immich /api/server/ping endpoint at the given base URL.
# Returns success only on HTTP 200 within a short timeout (follows redirects).
immich_reachable() {
local url="$1"
local code
code=$(curl -sL -m 5 -o /dev/null -w '%{http_code}' \
"$url/api/server/ping" 2>/dev/null) || return 1
[[ "$code" == "200" ]]
}
# Pick the LAN URL if reachable, otherwise fall back to the public URL.
detect_immich_url() {
if immich_reachable "$IMMICH_LAN_URL"; then
IMMICH_URL="$IMMICH_LAN_URL"
info "Using LAN ingress: $IMMICH_URL"
elif immich_reachable "$IMMICH_PUBLIC_URL"; then
IMMICH_URL="$IMMICH_PUBLIC_URL"
info "LAN ingress unreachable, using public ingress: $IMMICH_URL"
else
die "Immich is not reachable via LAN ($IMMICH_LAN_URL) or public ($IMMICH_PUBLIC_URL)"
fi
}
# Compute SHA1 (hex, lowercase) for a file.
file_sha1() {
sha1sum "$1" | awk '{print $1}'
}
# Extract fileCreatedAt and fileModifiedAt from exiftool if available,
# otherwise fall back to filesystem timestamps.
# Output is ISO 8601 with milliseconds, e.g. 2024-01-15T10:30:00.000Z
file_timestamps() {
local path="$1"
local created modified
if command -v exiftool &>/dev/null; then
# Try to get DateTimeOriginal first, then CreateDate, then FileModifyDate
created=$(exiftool -s3 -DateTimeOriginal "$path" 2>/dev/null || true)
if [[ -z "$created" ]]; then
created=$(exiftool -s3 -CreateDate "$path" 2>/dev/null || true)
fi
if [[ -z "$created" ]]; then
created=$(exiftool -s3 -FileModifyDate "$path" 2>/dev/null || true)
fi
# exiftool returns something like "2024:01:15 10:30:00" or
# "2024:01:15 10:30:00+03:00" with timezone offset.
if [[ -n "$created" ]]; then
# Strip timezone offset if present, replace colons with dashes for date part,
# replace space with T, append .000Z
created=$(echo "$created" | sed -E 's/([0-9]{4}):([0-9]{2}):([0-9]{2})/\1-\2-\3/; s/ /T/; s/[+-][0-9]{2}:[0-9]{2}$//; s/$/.000Z/')
fi
fi
if [[ -z "$created" ]]; then
created=$(date -r "$path" -u '+%Y-%m-%dT%H:%M:%S.000Z')
fi
modified=$(date -r "$path" -u '+%Y-%m-%dT%H:%M:%S.000Z')
echo "$created"
echo "$modified"
}
# ------------------------------------------------------------------
# Upload a single file
# ------------------------------------------------------------------
upload_file() {
local file="$1"
local api_key="$2"
local filename
filename=$(basename "$file")
local checksum
checksum=$(file_sha1 "$file")
local fileCreatedAt fileModifiedAt
local timestamps
timestamps=$(file_timestamps "$file")
fileCreatedAt=$(echo "$timestamps" | sed -n '1p')
fileModifiedAt=$(echo "$timestamps" | sed -n '2p')
info "Uploading: $filename"
local response tmpfile
tmpfile=$(mktemp)
trap "rm -f $tmpfile" RETURN
# Immich 3.x dropped the deviceAssetId/deviceId columns (migration
# DropDeviceIdAndDeviceAssetId). The /api/assets endpoint still tolerates
# those multipart fields but ignores them, so they are removed here to stay
# correct and future-proof. Duplicate detection is done server-side via the
# x-immich-checksum header (and the bulk-upload-check pre-pass), which
# returns HTTP 200 {"status":"duplicate"} instead of 201 {"status":"created"}.
local http_code
http_code=$(curl -s -o "$tmpfile" -w '%{http_code}' -X POST \
-H "x-api-key: $api_key" \
-H "x-immich-checksum: $checksum" \
-F "assetData=@$file" \
-F "fileCreatedAt=$fileCreatedAt" \
-F "fileModifiedAt=$fileModifiedAt" \
-F "filename=$filename" \
"$IMMICH_URL/api/assets" 2>/dev/null) || { http_code="000"; }
if [[ "$http_code" == "200" ]]; then
info " Skipped (duplicate): $filename"
elif [[ "$http_code" == "201" ]]; then
info " Uploaded: $filename"
elif [[ "$http_code" == "000" ]]; then
warn " Failed to upload: $filename (curl error)"
else
warn " Unexpected response $http_code for: $filename"
cat "$tmpfile" >&2 || true
fi
rm -f "$tmpfile"
}
# ------------------------------------------------------------------
# Bulk upload with duplicate checking
# ------------------------------------------------------------------
bulk_upload() {
local api_key="$1"
local files_list="$2"
local total missing_count
total=$(wc -l < "$files_list")
# Compute SHA1 for each file and build the bulk check JSON
local check_json tmp_json tmp_missing
tmp_json=$(mktemp)
tmp_missing=$(mktemp)
info "Computing SHA1 checksums for $total file(s)..."
while IFS= read -r file; do
local checksum
checksum=$(file_sha1 "$file")
printf '%s\t%s\n' "$checksum" "$file" >> "$tmp_json"
done < "$files_list"
# Build the bulk-upload-check JSON
{
echo '{"assets":['
local first=1
while IFS=$'\t' read -r checksum file; do
[[ "$first" -eq 1 ]] || echo ','
printf '{"id":"%s","checksum":"%s"}' "$file" "$checksum"
first=0
done < "$tmp_json"
echo ']}'
} > "$tmp_json.json"
info "Checking for duplicates on server..."
local response
response=$(curl -sf -X POST \
-H "x-api-key: $api_key" \
-H "Content-Type: application/json" \
-d @"$tmp_json.json" \
"$IMMICH_URL/api/assets/bulk-upload-check" 2>/dev/null) || die "bulk-upload-check failed"
# Parse the response to find which files are duplicates.
# Response: { "results": [{ "id": "...", "action": "reject", "reason": "duplicate", ... }] }
local resp_file
resp_file=$(mktemp)
echo "$response" > "$resp_file"
python3 -c "
import json, sys
try:
with open('$resp_file') as f:
d = json.load(f)
for r in d.get('results', []):
if r.get('action') == 'reject' and r.get('reason') == 'duplicate':
print(r['id'])
except Exception:
pass
" > "$tmp_missing.exists"
rm -f "$resp_file"
# Build the missing files list
while IFS=$'\t' read -r checksum file; do
if ! grep -Fxq "$file" "$tmp_missing.exists"; then
echo "$file" >> "$tmp_missing"
fi
done < "$tmp_json"
missing_count=$(wc -l < "$tmp_missing" | awk '{print $1}')
local dup_count=$(( total - missing_count ))
info "Found $dup_count duplicate(s), $missing_count file(s) to upload"
if [[ "$missing_count" -eq 0 ]]; then
info "Nothing to upload."
rm -f "$tmp_json" "$tmp_json.json" "$tmp_missing" "$tmp_missing.exists"
return
fi
# Upload missing files
local n=0
while IFS= read -r file; do
((n++)) || true
echo "[$n/$missing_count] $file"
upload_file "$file" "$api_key"
done < "$tmp_missing"
rm -f "$tmp_json" "$tmp_json.json" "$tmp_missing" "$tmp_missing.exists"
}
# ------------------------------------------------------------------
# Main
# ------------------------------------------------------------------
main() {
if [[ $# -ne 1 ]]; then
die "Usage: $(basename "$0") <file_or_directory>"
fi
local src="$1"
if [[ ! -e "$src" ]]; then
die "Path does not exist: $src"
fi
if [[ ! -r "$src" ]]; then
die "Path is not readable: $src"
fi
if [[ ! -f "$API_KEY_FILE" ]]; then
die "API key file not found: $API_KEY_FILE"
fi
local api_key
api_key=$(cat "$API_KEY_FILE")
if [[ -z "$api_key" ]]; then
die "API key file is empty: $API_KEY_FILE"
fi
detect_immich_url
if [[ -f "$src" ]]; then
# Single file upload
info "Uploading single file: $src"
upload_file "$src" "$api_key"
elif [[ -d "$src" ]]; then
# Directory: collect image files recursively
local files_list
files_list=$(mktemp)
trap "rm -f $files_list" EXIT
info "Scanning directory: $src"
find "$src" -type f -regextype posix-extended -iregex ".*$IMAGE_EXTENSIONS" -print > "$files_list"
local count
count=$(wc -l < "$files_list" | awk '{print $1}')
if [[ "$count" -eq 0 ]]; then
die "No image files found in: $src"
fi
info "Found $count image file(s)"
bulk_upload "$api_key" "$files_list"
rm -f "$files_list"
else
die "Unsupported path type: $src"
fi
}
main "$@"
|