Skip to content

Commit

Permalink
Merge pull request #43 from bmachek/42-feature-create-album-based-on-…
Browse files Browse the repository at this point in the history
…parent-folder-on-export

Implemented album option: Folder based in export module.
  • Loading branch information
bmachek authored Oct 8, 2024
2 parents f92a184 + 093ed74 commit 4d183da
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions immich-plugin.lrplugin/ExportDialogSections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function ExportDialogSections.sectionsForTopOfDialog(_, propertyTable)
{ title = 'Choose on export', value = 'onexport' },
{ title = 'Existing album', value = 'existing' },
{ title = 'Create new album', value = 'new' },
{ title = 'Create/use folder name as album', value = 'folder' },
{ title = 'Do not use an album', value = 'none' },
},
value = bind 'albumMode',
Expand Down
9 changes: 9 additions & 0 deletions immich-plugin.lrplugin/ExportTask.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function ExportTask.processRenderedPhotos(functionContext, exportContext)
{ title = 'Do not use an album', value = 'none' },
{ title = 'Existing album', value = 'existing' },
{ title = 'Create new album', value = 'new' },
{ title = 'Create/use folder name as album', value = 'folder' },
},
value = LrView.bind('albumMode'),
immediate = true,
Expand Down Expand Up @@ -132,6 +133,8 @@ function ExportTask.processRenderedPhotos(functionContext, exportContext)
useAlbum = true
elseif exportParams.albumMode == 'none' then
log:trace('Not using any albums, just uploading assets.')
elseif exportParams.albumMode == 'folder' then
log:trace('Create/use folder name as album.')
else
log:trace('Unknown albumMode: ' .. exportParams.albumMode .. '. Ignoring.')
end
Expand Down Expand Up @@ -167,6 +170,12 @@ function ExportTask.processRenderedPhotos(functionContext, exportContext)
if useAlbum then
log:trace('Adding asset to album')
immich:addAssetToAlbum(albumId, id)
elseif exportParams.albumMode == 'folder' then
local folderName = rendition.photo:getFormattedMetadata("folderName")
local folderAlbumId = immich:createOrGetAlbumFolderBased(folderName)
if folderAlbumId ~= nil then
immich:addAssetToAlbum(folderAlbumId, id)
end
end
end

Expand Down
44 changes: 44 additions & 0 deletions immich-plugin.lrplugin/ImmichAPI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,31 @@ function ImmichAPI:createAlbum(albumName)
return nil
end


function ImmichAPI:createOrGetAlbumFolderBased(albumName)
if util.nilOrEmpty(albumName) then
util.handleError('createAlbum: albumName empty', 'No album name given. Check logs.')
return nil
end

local existingAlbums = ImmichAPI:getAlbumsByNameFolderBased(albumName)
if existingAlbums ~= nil then
if #existingAlbums > 0 then
log:trace("Found existing folder based album with id: " .. existingAlbums[1].value)
return existingAlbums[1].value
end
end

local apiPath = '/albums'
local postBody = { albumName = albumName, description = 'Based on Lightroom folder: ' .. albumName }

local parsedResponse = ImmichAPI.doPostRequest(self, apiPath, postBody)
if parsedResponse ~= nil then
return parsedResponse.id
end
return nil
end

function ImmichAPI:deleteAlbum(albumId)
local path = '/albums/' .. albumId

Expand Down Expand Up @@ -349,6 +374,25 @@ function ImmichAPI:getAlbums()
end
end


function ImmichAPI:getAlbumsByNameFolderBased(albumName)
local path = '/albums'
local parsedResponse = ImmichAPI.doGetRequest(self, path)
local albums = {}
if parsedResponse then
for i = 1, #parsedResponse do
local row = parsedResponse[i]
if row.albumName == albumName and row.description == 'Based on Lightroom folder: ' .. albumName then
table.insert(albums,
{ title = row.albumName .. ' (' .. string.sub(row.createdAt, 1, 19) .. ')', value = row.id })
end
end
return albums
else
return nil
end
end

function ImmichAPI:getActivities(albumId, assetId)
local path = '/activities?albumId=' .. albumId

Expand Down

0 comments on commit 4d183da

Please sign in to comment.