Skip to content

Commit

Permalink
Merge pull request #36 from bmachek/32-feature-implement-ability-to-d…
Browse files Browse the repository at this point in the history
…elete-assets

Ability to delete assets in Immich when "unpublished" in Lightroom
  • Loading branch information
bmachek authored Sep 18, 2024
2 parents 2673ef4 + ed26c78 commit 38f3d46
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
37 changes: 36 additions & 1 deletion immich-plugin.lrplugin/ImmichAPI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ function ImmichAPI:replaceAsset(immichId, pathOrMessage, localId)
return nil
end


function ImmichAPI:deleteAsset(immichId)
if util.nilOrEmpty(immichId) then
util.handleError('deleteAsset: immichId empty', 'Immich asset ID missing. Check logs.')
return nil
end

local apiPath = '/assets'

local body = { ids = { immichId } }

local parsedResponse = ImmichAPI:doCustomRequest('DELETE', apiPath, body)
if parsedResponse ~= nil then
return true
end
return false
end


function ImmichAPI:removeAssetFromAlbum(albumId, assetId)
if util.nilOrEmpty(albumId) then
util.handleError('removeAssetFromAlbum: albumId empty', 'Immich album ID missing. Check logs.')
Expand Down Expand Up @@ -370,6 +389,22 @@ function ImmichAPI:checkIfAssetExists(localId, filename, dateCreated)
return nil
end

function ImmichAPI:checkIfAssetIsInAnAlbum(immichId)
local postBody = { id = immichId, deviceId = self.deviceIdString, isTrashed = false, isNotInAlbum = false }
local response = ImmichAPI.doPostRequest(self, '/search/metadata', postBody)

if not response then
log:trace('checkIfAssetIsInAnAlbum: No response')
return false
elseif response.assets.count == 1 then
log:trace('checkIfAssetIsInAnAlbum: ' .. immichId .. ' is in an album')
return true
end

log:trace('checkIfAssetIsInAnAlbum: ' .. immichId .. ' is NOT in an album')
return false
end

function ImmichAPI:getLocalIdForAssetId(assetId)
local parsedResponse = ImmichAPI.getAssetInfo(self, assetId)

Expand Down Expand Up @@ -464,7 +499,7 @@ function ImmichAPI:doCustomRequest(method, apiPath, postBody)

local response, headers = LrHttp.post(url, JSON:encode(postBody), ImmichAPI.createHeaders(self), method, 5)

if headers.status == 201 or headers.status == 200 then
if headers.status == 204 or headers.status == 201 or headers.status == 200 then
log:trace('ImmichAPI ' .. method .. ' request succeeded: ' .. response)
if util.nilOrEmpty(response) then
return {}
Expand Down
15 changes: 15 additions & 0 deletions immich-plugin.lrplugin/PublishTask.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,27 @@ function PublishTask.deletePhotosFromPublishedCollection(publishSettings, arrayO
return nil
end

local delete = LrDialogs.confirm('Delete photos', 'Should removed photos be trashed in Immich?', 'If not included in any album', 'No', 'Yes (dangerous!)')

local catalog = LrApplication.activeCatalog()
local publishedCollection = catalog:getPublishedCollectionByLocalIdentifier(localCollectionId)

for i = 1, #arrayOfPhotoIds do
if immich:removeAssetFromAlbum(publishedCollection:getRemoteId(), arrayOfPhotoIds[i]) then
deletedCallback(arrayOfPhotoIds[i])
local success = true

if delete == 'ok' then
success = immich:deleteAsset(arrayOfPhotoIds[i])
elseif delete == 'other' then
if not immich:checkIfAssetIsInAnAlbum(arrayOfPhotoIds[i]) then
success = immich:deleteAsset(arrayOfPhotoIds[i])
end
end

if not success then
util.handleError('Failed to delete asset ' .. arrayOfPhotoIds[i] .. ' from Immich', 'Failed to delete asset (check logs)')
end
end
end
end
Expand Down

0 comments on commit 38f3d46

Please sign in to comment.