diff --git a/PiwigoDirectorySync/PiwigoDirectorySync.csproj b/PiwigoDirectorySync/PiwigoDirectorySync.csproj index 0eab756..8c997ec 100644 --- a/PiwigoDirectorySync/PiwigoDirectorySync.csproj +++ b/PiwigoDirectorySync/PiwigoDirectorySync.csproj @@ -19,12 +19,12 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/PiwigoDirectorySync/Services/ImageSynchronizer.cs b/PiwigoDirectorySync/Services/ImageSynchronizer.cs index b149dc1..68c5365 100644 --- a/PiwigoDirectorySync/Services/ImageSynchronizer.cs +++ b/PiwigoDirectorySync/Services/ImageSynchronizer.cs @@ -36,6 +36,31 @@ public class ImageSynchronizer : IImageSynchronizer await GetImageIdsFromServerAsync(piwigoClient, piwigoServer, ct); await UploadNewImagesToServerAsync(piwigoClient, piwigoServer, ct); + await UploadChangedImagesToServerAsync(piwigoClient, piwigoServer, ct); + } + + private async Task UploadChangedImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) + { + var imagesToUpload = await _persistenceContext.PiwigoImages.Include(i => i.Album) + .Where(i => i.ServerImageId != null && i.Album.ServerId == piwigoServer.Id && i.UploadRequired) + .ToListAsync(ct); + + _logger.LogInformation("Updating {Count} images", imagesToUpload.Count); + + foreach (var imageEntity in imagesToUpload) + { + var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); + var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); + var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); + + imageEntity.ServerImageId = imageUploaded.ImageId; + imageEntity.UploadRequired = false; + + _logger.LogInformation("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, + imageEntity.ServerImageId); + + await _persistenceContext.SaveChangesAsync(ct); + } } private async Task UploadNewImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) @@ -44,6 +69,8 @@ public class ImageSynchronizer : IImageSynchronizer .Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id) .ToListAsync(ct); + _logger.LogInformation("Uploading {Count} images", imagesToUpload.Count); + foreach (var imageEntity in imagesToUpload) { var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); @@ -79,6 +106,8 @@ public class ImageSynchronizer : IImageSynchronizer .Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id) .ToListAsync(ct); + _logger.LogInformation("Checking {Count} images if they exist", imagesToSearch.Count); + var md5SumsToCheck = imagesToSearch.Where(i => i.Md5Sum != null).DistinctBy(i => i.Md5Sum).ToDictionary(i => i.Md5Sum!, i => i, StringComparer.OrdinalIgnoreCase); var processedImages = 0; @@ -91,6 +120,8 @@ public class ImageSynchronizer : IImageSynchronizer { var imageEntity = md5SumsToCheck[existingImage.Key]; imageEntity.ServerImageId = existingImage.Value; + imageEntity.UploadRequired = false; + _logger.LogInformation("Found image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, imageEntity.ServerImageId); }