adds common read only flag for sync commands
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-11 23:16:03 +02:00
parent baa0117e1e
commit 092738d767
8 changed files with 37 additions and 23 deletions

View File

@ -1,5 +1,4 @@
using System.ComponentModel; using System.Diagnostics;
using System.Diagnostics;
using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Services; using PiwigoDirectorySync.Services;
using Serilog; using Serilog;
@ -23,7 +22,7 @@ internal class SyncAlbumsCommand : CancellableAsyncCommand<SyncAlbumsCommand.Syn
var stopWatch = Stopwatch.StartNew(); var stopWatch = Stopwatch.StartNew();
await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, settings.CreateAlbumsOnServer, cancellationToken); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, settings.ReadOnly, cancellationToken);
stopWatch.Stop(); stopWatch.Stop();
_logger.Information("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, _logger.Information("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId,
@ -32,10 +31,7 @@ internal class SyncAlbumsCommand : CancellableAsyncCommand<SyncAlbumsCommand.Syn
return 0; return 0;
} }
internal class SyncAlbumsSettings : CommonCommandSettings internal class SyncAlbumsSettings : SyncCommandSettings
{ {
[CommandOption("-c|--create-albums")]
[DefaultValue(true)]
public bool CreateAlbumsOnServer { get; set; }
} }
} }

View File

@ -0,0 +1,12 @@
using System.ComponentModel;
using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands;
internal class SyncCommandSettings : CommonCommandSettings
{
[Description("specify this flag to only read from the piwigo server and skip all updates. This might help on initial sync with an existing gallerie to not change any data.")]
[CommandOption("-ro|--read-only")]
[DefaultValue(false)]
public bool ReadOnly { get; set; }
}

View File

@ -31,10 +31,10 @@ internal class SyncFullCommand : CancellableAsyncCommand<SyncFullCommand.SyncFul
await ScanCommand.ScanDirectory(_logger, _fileIndexer, _fileSystemScanner, settings.PiwigoServerId, cancellationToken); await ScanCommand.ScanDirectory(_logger, _fileIndexer, _fileSystemScanner, settings.PiwigoServerId, cancellationToken);
_logger.Information("running album synchronization"); _logger.Information("running album synchronization");
await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, true, cancellationToken); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, settings.ReadOnly, cancellationToken);
_logger.Information("running image synchronization"); _logger.Information("running image synchronization");
await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken); await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, settings.ReadOnly, cancellationToken);
stopWatch.Stop(); stopWatch.Stop();
_logger.Information("Full synchronization for piwigo server {SettingsPiwigoServerId} finished in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, _logger.Information("Full synchronization for piwigo server {SettingsPiwigoServerId} finished in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId,
@ -43,7 +43,7 @@ internal class SyncFullCommand : CancellableAsyncCommand<SyncFullCommand.SyncFul
return 0; return 0;
} }
internal class SyncFullSettings : CommonCommandSettings internal class SyncFullSettings : SyncCommandSettings
{ {
} }
} }

View File

@ -21,7 +21,7 @@ internal class SyncImagesCommand : CancellableAsyncCommand<SyncImagesCommand.Syn
_logger.Information("Starting image synchronization of piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); _logger.Information("Starting image synchronization of piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId);
var stopWatch = Stopwatch.StartNew(); var stopWatch = Stopwatch.StartNew();
await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken); await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, settings.ReadOnly, cancellationToken);
stopWatch.Stop(); stopWatch.Stop();
_logger.Information("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, _logger.Information("Synchronized all images with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId,
@ -30,7 +30,7 @@ internal class SyncImagesCommand : CancellableAsyncCommand<SyncImagesCommand.Syn
return 0; return 0;
} }
internal class SyncImagesSettings : CommonCommandSettings internal class SyncImagesSettings : SyncCommandSettings
{ {
} }
} }

View File

@ -19,7 +19,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer
_persistenceContext = persistenceContext; _persistenceContext = persistenceContext;
} }
public async Task SynchronizeAlbums(int piwigoServerId, bool createAlbumsOnServer, CancellationToken ct) public async Task SynchronizeAlbums(int piwigoServerId, bool readOnly, CancellationToken ct)
{ {
var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct);
if (piwigoServer is null) if (piwigoServer is null)
@ -31,7 +31,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer
var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
await UpdatePiwigoAlbumsFromServerAsync(piwigoClient, piwigoServer, ct); await UpdatePiwigoAlbumsFromServerAsync(piwigoClient, piwigoServer, ct);
if (createAlbumsOnServer) if (!readOnly)
{ {
_logger.Information("Creating missing albums on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name); _logger.Information("Creating missing albums on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name);
await AddMissingAlbumsToServerAsync(piwigoClient, piwigoServer, ct); await AddMissingAlbumsToServerAsync(piwigoClient, piwigoServer, ct);

View File

@ -2,5 +2,5 @@ namespace PiwigoDirectorySync.Services;
internal interface IAlbumSynchronizer internal interface IAlbumSynchronizer
{ {
Task SynchronizeAlbums(int piwigoServerId, bool createAlbumsOnServer, CancellationToken ct); Task SynchronizeAlbums(int piwigoServerId, bool readOnly, CancellationToken ct);
} }

View File

@ -2,6 +2,6 @@
internal interface IImageSynchronizer internal interface IImageSynchronizer
{ {
Task SynchronizeImagesAsync(int piwigoServerId, CancellationToken ct); Task SynchronizeImagesAsync(int piwigoServerId, bool readOnly, CancellationToken ct);
Task DownloadImagesAsync(int piwigoServerId, CancellationToken ct); Task DownloadImagesAsync(int piwigoServerId, CancellationToken ct);
} }

View File

@ -43,12 +43,12 @@ internal class ImageSynchronizer : IImageSynchronizer
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);
await DownloadImagesForAlbumAsync(piwigoClient, piwigoServer, albumId, albumInfo, ct); await DownloadImagesForAlbumAsync(piwigoClient, piwigoServer, albumId, albumInfo, ct);
} }
} }
public async Task SynchronizeImagesAsync(int piwigoServerId, CancellationToken ct) public async Task SynchronizeImagesAsync(int piwigoServerId, bool readOnly, CancellationToken ct)
{ {
var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct); var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct);
if (piwigoServer is null) if (piwigoServer is null)
@ -57,14 +57,20 @@ internal class ImageSynchronizer : IImageSynchronizer
return; return;
} }
_logger.Information("Synchronizing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, _logger.Information("Synchronizing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name, piwigoServer.RootDirectory);
piwigoServer.RootDirectory);
var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
await GetImageIdsFromServerAsync(piwigoClient, piwigoServer, ct); await GetImageIdsFromServerAsync(piwigoClient, piwigoServer, ct);
await UploadNewImagesToServerAsync(piwigoClient, piwigoServer, ct); if (!readOnly)
await UploadChangedImagesToServerAsync(piwigoClient, piwigoServer, ct); {
await UploadNewImagesToServerAsync(piwigoClient, piwigoServer, ct);
await UploadChangedImagesToServerAsync(piwigoClient, piwigoServer, ct);
}
else
{
_logger.Information("Skipping image upload of server {PiwigoServerName} as readOnly is specified", piwigoServer.Name);
}
} }
private async Task DownloadImagesForAlbumAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, int albumId, Album albumInfo, CancellationToken ct) private async Task DownloadImagesForAlbumAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, int albumId, Album albumInfo, CancellationToken ct)
@ -140,7 +146,7 @@ internal class ImageSynchronizer : IImageSynchronizer
private async Task UploadChangedImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) private async Task UploadChangedImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
{ {
var imagesToUpload = await _persistenceContext.PiwigoImages.Include(i => i.Album) var imagesToUpload = await _persistenceContext.PiwigoImages.Include(i => i.Album)
.Where(i => i.ServerImageId != null && i.Album.ServerId == piwigoServer.Id && i.UploadRequired) .Where(i => i.ServerImageId != null && i.Album.ServerId == piwigoServer.Id && i.UploadRequired && i.ServerImageId != null)
.ToListAsync(ct); .ToListAsync(ct);
_logger.Information("Updating {Count} images", imagesToUpload.Count); _logger.Information("Updating {Count} images", imagesToUpload.Count);