moves download to sync download and make it more stable
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-15 23:20:37 +02:00
parent b0f211f568
commit b910987a24
5 changed files with 49 additions and 21 deletions

View File

@ -6,17 +6,17 @@ using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands; namespace PiwigoDirectorySync.Commands;
internal class DownloadImagesCommand : CancellableAsyncCommand<DownloadImagesCommand.DownloadImagesSettings> internal class SyncDownloadCommand : CancellableAsyncCommand<SyncDownloadCommand.SyncDownloadSettings>
{ {
private readonly IImageSynchronizer _imageSynchronizer; private readonly IImageSynchronizer _imageSynchronizer;
private readonly ILogger _logger = Log.ForContext<DownloadImagesCommand>(); private readonly ILogger _logger = Log.ForContext<SyncDownloadCommand>();
public DownloadImagesCommand(IImageSynchronizer imageSynchronizer) public SyncDownloadCommand(IImageSynchronizer imageSynchronizer)
{ {
_imageSynchronizer = imageSynchronizer; _imageSynchronizer = imageSynchronizer;
} }
protected override async Task<int> ExecuteAsync(CommandContext context, DownloadImagesSettings settings, CancellationToken cancellationToken) protected override async Task<int> ExecuteAsync(CommandContext context, SyncDownloadSettings settings, CancellationToken cancellationToken)
{ {
_logger.Information("Starting image download for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); _logger.Information("Starting image download for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId);
var stopWatch = Stopwatch.StartNew(); var stopWatch = Stopwatch.StartNew();
@ -30,7 +30,7 @@ internal class DownloadImagesCommand : CancellableAsyncCommand<DownloadImagesCom
return 0; return 0;
} }
internal class DownloadImagesSettings : CommonCommandSettings internal class SyncDownloadSettings : CommonCommandSettings
{ {
} }
} }

View File

@ -54,12 +54,12 @@ app.Configure(config =>
#endif #endif
config.AddCommand<ScanCommand>("scan"); config.AddCommand<ScanCommand>("scan");
config.AddCommand<DownloadImagesCommand>("download");
config.AddBranch("sync", c => config.AddBranch("sync", c =>
{ {
c.AddCommand<SyncFullCommand>("full"); c.AddCommand<SyncFullCommand>("full");
c.AddCommand<SyncAlbumsCommand>("albums"); c.AddCommand<SyncAlbumsCommand>("albums");
c.AddCommand<SyncImagesCommand>("images"); c.AddCommand<SyncImagesCommand>("images");
c.AddCommand<SyncDownloadCommand>("download");
c.AddCommand<SyncPendingChangesCommand>("pending"); c.AddCommand<SyncPendingChangesCommand>("pending");
}); });
config.AddBranch("piwigo", c => config.AddBranch("piwigo", c =>

View File

@ -39,7 +39,7 @@
}, },
"Download": { "Download": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "download", "commandLineArgs": "sync download 5",
"environmentVariables": { "environmentVariables": {
} }
}, },

View File

@ -49,6 +49,12 @@ internal class ImageSynchronizer : IImageSynchronizer
foreach (var albumId in albumIdsToDownload) foreach (var albumId in albumIdsToDownload)
{ {
var albumInfos = await piwigoClient.Album.GetListAsync(albumId, false, false, ThumbnailSize.Thumb, ct); var albumInfos = await piwigoClient.Album.GetListAsync(albumId, false, false, ThumbnailSize.Thumb, ct);
if (!albumInfos.Any())
{
Log.Warning("Album with server {AlbumId} not found on piwigo server", albumId);
continue;
}
var albumInfo = albumInfos.First(); var albumInfo = albumInfos.First();
_logger.Information("Starting downloads for album {AlbumInfoName}", albumInfo.Name); _logger.Information("Starting downloads for album {AlbumInfoName}", albumInfo.Name);
@ -101,7 +107,19 @@ internal class ImageSynchronizer : IImageSynchronizer
foreach (var image in images.Images) foreach (var image in images.Images)
{ {
await DownloadImageAsync(piwigoServer, albumInfo, image, ct); try
{
await DownloadImageAsync(piwigoServer, albumInfo, image, ct);
}
catch (OperationCanceledException)
{
Log.Debug("Cancel of download requested");
return;
}
catch (Exception ex)
{
Log.Warning(ex, "Download of server image {ImageId} / {ImageName} / {ImageElementUrl}", image.Id, image.Name, image.ElementUrl);
}
} }
currentPage++; currentPage++;
@ -123,10 +141,11 @@ internal class ImageSynchronizer : IImageSynchronizer
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, localImage.FilePath)); var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, localImage.FilePath));
if (fileInfo.Exists) if (fileInfo.Exists)
{ {
_logger.Warning("Tried to download image {ImageFile} but it already exists", image.File); _logger.Debug("Tried to download image {ImageFile} but it already exists", fileInfo.FullName);
return; return;
} }
_logger.Information("Downloading image {ImageFile}", fileInfo.FullName);
await image.ElementUrl.DownloadFileAsync(fileInfo.Directory!.FullName, fileInfo.Name, cancellationToken: ct); await image.ElementUrl.DownloadFileAsync(fileInfo.Directory!.FullName, fileInfo.Name, cancellationToken: ct);
localImage.Md5Sum = await FilesystemHelpers.CalculateMd5SumAsync(fileInfo.FullName, ct); localImage.Md5Sum = await FilesystemHelpers.CalculateMd5SumAsync(fileInfo.FullName, ct);
await _persistenceContext.SaveChangesAsync(ct); await _persistenceContext.SaveChangesAsync(ct);
@ -134,21 +153,30 @@ internal class ImageSynchronizer : IImageSynchronizer
private async Task<ImageEntity> GetOrAddImageFromServerAsync(AlbumEntity album, Image image, CancellationToken ct) private async Task<ImageEntity> GetOrAddImageFromServerAsync(AlbumEntity album, Image image, CancellationToken ct)
{ {
var imageFullPath = Path.Combine(album.Path, image.File!);
var imageEntity = await _persistenceContext.PiwigoImages.Where(i => i.AlbumId == album.Id && i.ServerImageId == image.Id).FirstOrDefaultAsync(ct); var imageEntity = await _persistenceContext.PiwigoImages.Where(i => i.AlbumId == album.Id && i.ServerImageId == image.Id).FirstOrDefaultAsync(ct);
if (imageEntity is null) if (imageEntity is not null)
{ {
imageEntity = new ImageEntity return imageEntity;
{
AlbumId = album.Id,
Album = album,
ServerImageId = image.Id,
FilePath = Path.Combine(album.Path, image.File!),
UploadRequired = false,
DeleteRequired = false
};
_persistenceContext.PiwigoImages.Add(imageEntity);
} }
imageEntity = await _persistenceContext.PiwigoImages.Where(i => i.AlbumId == album.Id && i.FilePath == imageFullPath).FirstOrDefaultAsync(ct);
if (imageEntity is not null)
{
imageEntity.ServerImageId = image.Id;
return imageEntity;
}
imageEntity = new ImageEntity
{
AlbumId = album.Id,
Album = album,
ServerImageId = image.Id,
FilePath = imageFullPath,
UploadRequired = false,
DeleteRequired = false
};
_persistenceContext.PiwigoImages.Add(imageEntity);
return imageEntity; return imageEntity;
} }

View File

@ -8,7 +8,7 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"Sqlite": "Data Source=piwigoSync.db", "Sqlite": "Data Source=../../../piwigoSync.db",
"InMemory": "InMemorySyncDb" "InMemory": "InMemorySyncDb"
}, },
"Settings": { "Settings": {