moves download to sync download and make it more stable
PiwigoDirectorySync/pipeline/head This commit looks good Details

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;
internal class DownloadImagesCommand : CancellableAsyncCommand<DownloadImagesCommand.DownloadImagesSettings>
internal class SyncDownloadCommand : CancellableAsyncCommand<SyncDownloadCommand.SyncDownloadSettings>
{
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;
}
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);
var stopWatch = Stopwatch.StartNew();
@ -30,7 +30,7 @@ internal class DownloadImagesCommand : CancellableAsyncCommand<DownloadImagesCom
return 0;
}
internal class DownloadImagesSettings : CommonCommandSettings
internal class SyncDownloadSettings : CommonCommandSettings
{
}
}

View File

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

View File

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

View File

@ -49,6 +49,12 @@ internal class ImageSynchronizer : IImageSynchronizer
foreach (var albumId in albumIdsToDownload)
{
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();
_logger.Information("Starting downloads for album {AlbumInfoName}", albumInfo.Name);
@ -101,7 +107,19 @@ internal class ImageSynchronizer : IImageSynchronizer
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++;
@ -123,10 +141,11 @@ internal class ImageSynchronizer : IImageSynchronizer
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, localImage.FilePath));
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;
}
_logger.Information("Downloading image {ImageFile}", fileInfo.FullName);
await image.ElementUrl.DownloadFileAsync(fileInfo.Directory!.FullName, fileInfo.Name, cancellationToken: ct);
localImage.Md5Sum = await FilesystemHelpers.CalculateMd5SumAsync(fileInfo.FullName, ct);
await _persistenceContext.SaveChangesAsync(ct);
@ -134,21 +153,30 @@ internal class ImageSynchronizer : IImageSynchronizer
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);
if (imageEntity is null)
if (imageEntity is not null)
{
imageEntity = new ImageEntity
{
AlbumId = album.Id,
Album = album,
ServerImageId = image.Id,
FilePath = Path.Combine(album.Path, image.File!),
UploadRequired = false,
DeleteRequired = false
};
_persistenceContext.PiwigoImages.Add(imageEntity);
return 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;
}

View File

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