diff --git a/PiwigoDirectorySync/Services/ImageSynchronizer.cs b/PiwigoDirectorySync/Services/ImageSynchronizer.cs index 5a8e933..cbd0dbe 100644 --- a/PiwigoDirectorySync/Services/ImageSynchronizer.cs +++ b/PiwigoDirectorySync/Services/ImageSynchronizer.cs @@ -191,17 +191,34 @@ internal class ImageSynchronizer : IImageSynchronizer 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); + try + { + var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); - imageEntity.ServerImageId = imageUploaded.ImageId; - imageEntity.UploadRequired = false; + var fileSizeInKb = fileInfo.Length / 1024f; + _logger.Information("Updating image {ImageFilePath} ({ImageId}) to piwigo server ({Size} KiB)", imageEntity.FilePath, imageEntity.ServerImageId, fileSizeInKb); - _logger.Information("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, - imageEntity.ServerImageId); + var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); + var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); - await _persistenceContext.SaveChangesAsync(ct); + imageEntity.ServerImageId = imageUploaded.ImageId; + imageEntity.UploadRequired = false; + + _logger.Information("Updated image {ImageEntityPath} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.FilePath, imageEntity.Id, + imageEntity.ServerImageId); + + await _persistenceContext.SaveChangesAsync(ct); + } + catch (OperationCanceledException) + { + Log.Debug("Cancel of image upload requested"); + return; + } + catch (PiwigoException ex) + { + _logger.Error(ex,"Could not update image {ImageEntityPath} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.FilePath, + imageEntity.Id, imageEntity.ServerImageId); + } } } @@ -216,22 +233,34 @@ internal class ImageSynchronizer : IImageSynchronizer foreach (var imageEntity in imagesToUpload) { - var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); + try + { + var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); - var fileSizeInKb = fileInfo.Length / 1024f; - _logger.Information("Uploading image {ImageFilePath} ({ImageId}) to piwigo server with id {ImageServerImageId} ({Size} KiB)", imageEntity.FilePath, imageEntity.Id, - imageEntity.ServerImageId, fileSizeInKb); + var fileSizeInKb = fileInfo.Length / 1024f; + _logger.Information("Uploading image {ImageFilePath} ({ImageId}) to piwigo server ({Size} KiB)", imageEntity.FilePath, imageEntity.ServerImageId, fileSizeInKb); - var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); - var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); + var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); + var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); - imageEntity.ServerImageId = imageUploaded.ImageId; - imageEntity.UploadRequired = false; + imageEntity.ServerImageId = imageUploaded.ImageId; + imageEntity.UploadRequired = false; - _logger.Information("Uploaded image {ImageFilePath} ({ImageId}) to piwigo server with id {ImageServerImageId}", imageEntity.FilePath, imageEntity.Id, - imageEntity.ServerImageId); + _logger.Information("Uploaded image {ImageFilePath} ({ImageId}) to piwigo server with id {ImageServerImageId}", imageEntity.FilePath, imageEntity.Id, + imageEntity.ServerImageId); - await _persistenceContext.SaveChangesAsync(ct); + await _persistenceContext.SaveChangesAsync(ct); + } + catch (OperationCanceledException) + { + Log.Debug("Cancel of image upload requested"); + return; + } + catch (PiwigoException ex) + { + _logger.Error(ex, "Could not upload image {ImageEntityPath} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.FilePath, + imageEntity.Id, imageEntity.ServerImageId); + } } }