@@ -134,6 +134,43 @@ function PublishTask.processRenderedPhotos(functionContext, exportContext)
134134
135135 local lrPhoto = rendition .photo
136136 local remoteId = rendition .publishedPhotoId or " "
137+
138+ -- Detect photo already published in this service (multi-album support)
139+ local existingPwImageId = nil
140+ if remoteId == " " then
141+ -- Method 1: Via custom metadata (photos published with plugin >= 20251224.16)
142+ local storedImageUrl = lrPhoto :getPropertyForPlugin (_PLUGIN , " pwImageURL" )
143+ local storedHost = lrPhoto :getPropertyForPlugin (_PLUGIN , " pwHostURL" )
144+
145+ log :info (" DEBUG multi-album: remoteId vide, checking metadata..." )
146+ log :info (" DEBUG storedHost: " .. tostring (storedHost ))
147+ log :info (" DEBUG storedImageUrl: " .. tostring (storedImageUrl ))
148+ log :info (" DEBUG propertyTable.host: " .. tostring (propertyTable .host ))
149+
150+ if storedHost == propertyTable .host and storedImageUrl then
151+ existingPwImageId = utils .extractPwImageIdFromUrl (storedImageUrl , propertyTable .host )
152+ end
153+
154+ -- Method 2: Search in other collections of the service (fallback)
155+ if not existingPwImageId then
156+ log :info (" DEBUG multi-album: metadata vides, recherche cross-collection..." )
157+ local publishService = publishedCollection :getService ()
158+ existingPwImageId = utils .findExistingPwImageId (publishService , lrPhoto )
159+ if existingPwImageId then
160+ log :info (" DEBUG multi-album: trouvé via cross-collection, ID = " .. tostring (existingPwImageId ))
161+ end
162+ end
163+
164+ -- Verify the image still exists on Piwigo
165+ if existingPwImageId then
166+ local checkStatus = PiwigoAPI .checkPhoto (propertyTable , existingPwImageId )
167+ if not checkStatus .status then
168+ log :info (" DEBUG multi-album: image " .. existingPwImageId .. " n'existe plus sur Piwigo" )
169+ existingPwImageId = nil
170+ end
171+ end
172+ end
173+
137174 -- Wait for next photo to render.
138175 local success , pathOrMessage = rendition :waitForRender ()
139176
@@ -149,6 +186,25 @@ function PublishTask.processRenderedPhotos(functionContext, exportContext)
149186 -- photo has been exported to temporary location - upload to piwigo
150187 callStatus = {}
151188 local filePath = pathOrMessage
189+
190+ -- If photo already exists on Piwigo, associate instead of uploading
191+ if existingPwImageId then
192+ log :info (" Photo exists on Piwigo (ID " .. existingPwImageId .. " ), associating to album " .. albumId )
193+ callStatus = PiwigoAPI .associateImageToCategory (propertyTable , existingPwImageId , albumId )
194+
195+ if callStatus .status then
196+ rendition :recordPublishedPhotoId (callStatus .remoteid )
197+ rendition :recordPublishedPhotoUrl (callStatus .remoteurl )
198+ rendition :renditionIsDone (true )
199+ LrFileUtils .delete (pathOrMessage )
200+ else
201+ log :warn (" Association failed: " .. (callStatus .statusMsg or " " ) .. " , falling back to upload" )
202+ existingPwImageId = nil
203+ end
204+ end
205+
206+ if not existingPwImageId then
207+ -- Begin existing upload block (indent all upload code until end of if success)
152208 local metaData = {}
153209 -- build metadata structure
154210 metaData = utils .getPhotoMetadata (propertyTable , lrPhoto )
@@ -202,6 +258,7 @@ function PublishTask.processRenderedPhotos(functionContext, exportContext)
202258 end
203259 -- When done with photo, delete temp file.
204260 LrFileUtils .delete (pathOrMessage )
261+ end -- end if not existingPwImageId
205262 else
206263 rendition :uploadFailed (pathOrMessage or " Render failed" )
207264 end
@@ -279,18 +336,32 @@ function PublishTask.deletePhotosFromPublishedCollection(publishSettings, arrayO
279336 local thisLrPhoto = thisPhotoToUnpublish [1 ]
280337 local thispwImageID = thisPhotoToUnpublish [2 ]
281338 local thisPubPhoto = thisPhotoToUnpublish [3 ]
282- callStatus = PiwigoAPI .deletePhoto (publishSettings , pwCatID , thispwImageID , callStatus )
339+
340+ -- Use dissociate instead of delete to preserve multi-album associations
341+ log :info (" PublishTask.deletePhotosFromPublishedCollection - dissociating photo " .. thispwImageID .. " from category " .. pwCatID )
342+ callStatus = PiwigoAPI .dissociateImageFromCategory (publishSettings , thispwImageID , pwCatID )
283343 if callStatus .status then
284- catalog :withWriteAccessDo (" Updating " .. thisLrPhoto :getFormattedMetadata (" fileName" ),
285- function ()
286- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwHostURL" , " " )
287- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwAlbumName" , " " )
288- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwAlbumURL" , " " )
289- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwImageURL" , " " )
290- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwUploadDate" , " " )
291- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwUploadTime" , " " )
292- thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwCommentSync" , " " )
293- end )
344+ -- Only clear metadata if photo is no longer in any other published collection
345+ -- Check if photo exists in other collections of this service
346+ local publishService = publishedCollection :getService ()
347+ local stillPublished = utils .findExistingPwImageId (publishService , thisLrPhoto )
348+
349+ if not stillPublished then
350+ -- Photo is no longer published anywhere, clear all metadata
351+ log :info (" PublishTask.deletePhotosFromPublishedCollection - photo " .. thispwImageID .. " orphaned, clearing metadata" )
352+ catalog :withWriteAccessDo (" Updating " .. thisLrPhoto :getFormattedMetadata (" fileName" ),
353+ function ()
354+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwHostURL" , " " )
355+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwAlbumName" , " " )
356+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwAlbumURL" , " " )
357+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwImageURL" , " " )
358+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwUploadDate" , " " )
359+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwUploadTime" , " " )
360+ thisLrPhoto :setPropertyForPlugin (_PLUGIN , " pwCommentSync" , " " )
361+ end )
362+ else
363+ log :info (" PublishTask.deletePhotosFromPublishedCollection - photo " .. thispwImageID .. " still in other collections, keeping metadata" )
364+ end
294365 thisPhotoToUnpublish [4 ] = true
295366 else
296367 PiwigoBusy = false
0 commit comments