adds errorhandling to not fully stop if one file fails
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-10-01 00:28:11 +02:00
parent 0670bdd97e
commit f924c2a7d7

View File

@ -190,19 +190,36 @@ internal class ImageSynchronizer : IImageSynchronizer
_logger.Information("Updating {Count} images", imagesToUpload.Count); _logger.Information("Updating {Count} images", imagesToUpload.Count);
foreach (var imageEntity in imagesToUpload) foreach (var imageEntity in imagesToUpload)
{
try
{ {
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath));
var fileSizeInKb = fileInfo.Length / 1024f;
_logger.Information("Updating image {ImageFilePath} ({ImageId}) to piwigo server ({Size} KiB)", imageEntity.FilePath, imageEntity.ServerImageId, fileSizeInKb);
var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime);
var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct);
imageEntity.ServerImageId = imageUploaded.ImageId; imageEntity.ServerImageId = imageUploaded.ImageId;
imageEntity.UploadRequired = false; imageEntity.UploadRequired = false;
_logger.Information("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id, _logger.Information("Updated image {ImageEntityPath} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.FilePath, imageEntity.Id,
imageEntity.ServerImageId); 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 update image {ImageEntityPath} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.FilePath,
imageEntity.Id, imageEntity.ServerImageId);
}
}
} }
private async Task UploadNewImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) private async Task UploadNewImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
@ -215,12 +232,13 @@ internal class ImageSynchronizer : IImageSynchronizer
_logger.Information("Uploading {Count} images", imagesToUpload.Count); _logger.Information("Uploading {Count} images", imagesToUpload.Count);
foreach (var imageEntity in imagesToUpload) foreach (var imageEntity in imagesToUpload)
{
try
{ {
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath)); var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath));
var fileSizeInKb = fileInfo.Length / 1024f; var fileSizeInKb = fileInfo.Length / 1024f;
_logger.Information("Uploading image {ImageFilePath} ({ImageId}) to piwigo server with id {ImageServerImageId} ({Size} KiB)", imageEntity.FilePath, imageEntity.Id, _logger.Information("Uploading image {ImageFilePath} ({ImageId}) to piwigo server ({Size} KiB)", imageEntity.FilePath, imageEntity.ServerImageId, fileSizeInKb);
imageEntity.ServerImageId, fileSizeInKb);
var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime); var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime);
var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct); var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct);
@ -233,6 +251,17 @@ internal class ImageSynchronizer : IImageSynchronizer
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);
}
}
} }
private static ImageUpload GetImageUpload(ImageEntity imageEntity, DateTime createdAt) private static ImageUpload GetImageUpload(ImageEntity imageEntity, DateTime createdAt)