-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathimmich.lua
335 lines (299 loc) · 11.4 KB
/
immich.lua
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
--[[
This file is part of darktable,
copyright (c) 2024 Giorgio Massussi
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
IMMICH
Upload selection to an Immich server
USAGE
This plugin allows you to upload selected photos to a Immich server (https://immich.app/)
Previously exported photos will be overwritten, using unique Dartable internal ids.
Photos uploaded for the first time are automatically added to an album. It is possible to specify the name of the album in the Album title field in the module options; in the absence of the title, the roll name will be used.
In the lua options you must specify:
* the hostname of the server immich
* an api key generated in the Account settings - API Keys menu of the immich server
* a unique id identiphing the Darktable instance; this id is used as the device id uploading photos to Immich
USAGE
* install luasec and cjson for Lua 5.4 on your system
]]
local dt = require "darktable"
local du = require "lib/dtutils"
local df = require "lib/dtutils.file"
local cjson = require "cjson.safe"
local https = require "ssl.https"
local http = require "socket.http"
local ltn12 = require "ltn12"
local gettext = dt.gettext.gettext
dt.gettext.bindtextdomain("immich", dt.configuration.config_dir .."/lua/locale/")
local function _(msgid)
return gettext(msgid)
end
du.check_min_api_version("7.0.0", "immich")
local function call_immich_api(method,api,body,content_type)
local immichserver = dt.preferences.read("immich","immich_server","string")
local client = string.find(immichserver,"^https") ~= nil and https or http
local headers = { }
headers["x-api-key"] = dt.preferences.read("immich","immich_key","string")
local source = nil
if body == nil then
elseif (content_type == nil or content_type == "application/json") then
headers["Content-Type"] = "application/json"
source = cjson.encode(body)
headers["Content-Length"] = string.len(source)
source = ltn12.source.string(source)
elseif (content_type == "multipart/form-data") then
local boundary = "----DarktableImmichBoundary" .. math.random(1, 1e16)
headers["Content-Type"] = "multipart/form-data; boundary="..boundary
source = ltn12.source.empty()
local content_length = 0
for name,value in pairs(body) do
if (value.filename ~= nil) then
local form_data_table = {}
if (content_length > 0) then
table.insert(form_data_table,"")
end
table.insert(form_data_table, "--"..boundary)
table.insert(form_data_table, "Content-Disposition: form-data; name=\""..name.."\"; filename=\"".. value.filename .. "\"")
table.insert(form_data_table, "Content-Type: application/octet-stream")
table.insert(form_data_table, "")
table.insert(form_data_table, "")
local form_data = table.concat(form_data_table, "\r\n")
content_length = content_length+value.file:seek("end")+string.len(form_data)
value.file:seek("set",0)
source = ltn12.source.simplify(ltn12.source.cat(source,
ltn12.source.string(form_data),
ltn12.source.file(value.file)))
else
local form_data_table = {}
if (content_length > 0) then
table.insert(form_data_table,"")
end
table.insert(form_data_table, "--"..boundary)
table.insert(form_data_table, "Content-Disposition: form-data; name=\""..name.."\"")
table.insert(form_data_table, "")
table.insert(form_data_table, value)
local form_data = table.concat(form_data_table, "\r\n")
content_length = content_length+string.len(form_data)
source = ltn12.source.cat(source,ltn12.source.string(form_data))
end
end
content_length = content_length+6+string.len(boundary)
source = ltn12.source.cat(source,ltn12.source.string("\r\n--"..boundary.."--"))
headers["Content-Length"] = content_length
end
local res_table={}
local res, err, response_headers = client.request{
method=method,
url=immichserver.."/api/"..api,
headers=headers,
source=source,
sink=ltn12.sink.table(res_table)
}
if response_headers["content-type"] == "application/json; charset=utf-8" then
return cjson.decode(table.concat(res_table)), err, response_headers
end
return table.concat(res_table), err, response_headers
end
local function initialize(storage,format,images,high_quality,extra_data)
extra_data.device_id = dt.preferences.read("immich","immich_device_id","string")
if extra_data.device_id == nil then
extra_data.device_id = "darktable"
else
extra_data.device_id = "darktable_"..extra_data.device_id
end
local assets_ids = {}
extra_data.images_existence = {}
for i,image in ipairs(images) do
assets_ids[i] = tostring(image.id)
extra_data.images_existence[tostring(image.id)] = false
end
local res,err = call_immich_api("POST","assets/exist",{deviceAssetIds = assets_ids,deviceId = extra_data.device_id})
if (err ~= 200) then
if err == 401 then
extra_data.error = "Authentication error. Check your Immich API key in LUA settings."
elseif res ~= nil and res.message ~= nil then
extra_data.error = res.message
else
extra_data.error = "Error contacting Immich server: HTTP "..err
end
return {}
end
if (res.existingIds ~= nil) then
for i,id in ipairs(res.existingIds) do
extra_data.images_existence[id] = true
end
end
extra_data.album_assets = {}
extra_data.remote_albums = {}
local res_albums, err_albums = call_immich_api("GET","albums")
if err_albums == 200 then
for _,album in ipairs(res_albums) do
extra_data.remote_albums[album.albumName] = album.id
end
end
return images
end
local function iso_exif_datetime_taken(image)
local yr,mo,dy,h,m,s = string.match(image.exif_datetime_taken, "(%d-):(%d-):(%d-) (%d-):(%d-):(%d+)")
return os.date("!%Y-%m-%dT%H:%M:%S",os.time{year=yr, month=mo, day=dy, hour=h, min=m, sec=s})
end
local function replace_image(image,filename,device_id,asset_id)
local date = iso_exif_datetime_taken(image)
local form_data = {
deviceAssetId=tostring(image.id),
deviceId=device_id,
fileCreatedAt=date,
fileModifiedAt=date,
assetData={
filename=df.get_filename(filename),
file=io.open(filename)
}
}
local res,err = call_immich_api("PUT","assets/"..asset_id.."/original",form_data,"multipart/form-data")
if err == 200 then
return asset_id
end
return nil
end
local function upload_image(image,filename,device_id)
local date = iso_exif_datetime_taken(image)
local form_data = {
deviceAssetId=tostring(image.id),
deviceId=device_id,
fileCreatedAt=date,
fileModifiedAt=date,
assetData={
filename=df.get_filename(filename),
file=io.open(filename)
}
}
local res,err = call_immich_api("POST","assets",form_data,"multipart/form-data")
if err == 201 then
return res.id
end
return nil
end
local function store_image(storage,image,format,filename,number,total,high_quality,extra_data)
local asset_id,replaced = false
if (extra_data.images_existence[tostring(image.id)]) then
local res_search,err_search = call_immich_api("POST","search/metadata",{deviceId=extra_data.device_id,deviceAssetId=tostring(image.id),size=1})
if (err_search == 200) then
if (res_search.assets.count >= 1) then
replaced = true
asset_id = replace_image(image,filename,extra_data.device_id,res_search.assets.items[1].id)
else
asset_id = upload_image(image,filename,extra_data.device_id)
end
end
else
asset_id = upload_image(image,filename,extra_data.device_id)
end
if asset_id == nil then
extra_data.error = "Error uploading some image"
return
end
if not replaced then
local album_name = title_widget.text
if album_name == "" then
local tags = image.get_tags(image)
for i,tag in ipairs(tags) do
if string.find(tag.name,"^Album|") ~= nil then
for w in string.gmatch(tag.name,"[^|]+") do
album_name = w
end
end
end
end
if album_name == "" then
for w in string.gmatch(image.path,"[^/\\]+") do
album_name = w
end
end
local album_assets = extra_data.album_assets[album_name]
if album_assets == nil then
album_assets = {}
extra_data.album_assets[album_name] = album_assets
end
table.insert(album_assets,asset_id)
end
end
local function finalize(storage,image_table,extra_data)
if extra_data.album_assets ~= nil then
for album_name,album_assets in pairs(extra_data.album_assets) do
local album_id = extra_data.remote_albums[album_name]
if album_id == nil then
dt.print("Creating new album: " .. album_name)
call_immich_api("POST","albums",{albumName=album_name,assetIds=album_assets})
else
dt.print("Adding assets to album: "..album_name)
call_immich_api("PUT","albums/"..album_id.."/assets",{ids=album_assets})
end
end
end
if extra_data.error ~= nil then
dt.print(extra_data.error)
end
end
local function destroy()
dt.destroy_storage("immich")
end
local device_id = dt.preferences.read("immich","immich_device_id","string")
if device_id == nil or device_id == "" then
local uuid_template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
device_id = string.gsub(uuid_template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
end
dt.preferences.register
("immich","immich_server","string",
_("Immich server"),
_("The url of the Immich server to upload"),
"http://localhost:2283")
dt.preferences.register
("immich","immich_key","string",
_("Immich API key"),
_("A valid Immich API key"),
"T38JGhBrVOiWCE4tZXMoGKWoe39IIj2G8KNrfy0Eg")
dt.preferences.register
("immich","immich_device_id","string",
_("Immich Device ID"),
_("A unique ID identifying this local Darktable installation"),
device_id)
local title_widget = dt.new_widget("entry") {
placeholder=_("Use roll name")
}
local widget = dt.new_widget("box") {
orientation=horizontal,
dt.new_widget("label"){label = _("Album Title"), tooltip = _("Album title. If not specied roll name will be used") },
title_widget
}
dt.register_storage("immich",_("immich"),
store_image,
finalize,
nil,
initialize,
widget)
local script_data = {}
script_data.metadata = {
name = "immich",
purpose = _("upload all selected images to Immich server"),
author = "Giorgio Massussi"
}
script_data.destroy = destroy -- function to destory the script
script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet, otherwise leave as nil
script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
script_data.show = nil -- only required for libs since the destroy_method only hides them
return script_data
--
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua