adds option to only read albums from the server and not creating new ones (helps during init sync with existing piwigo setup)

This commit is contained in:
Philipp Häfelfinger 2023-09-11 16:27:25 +02:00
parent 2fe5270d93
commit ee2e9e3b77
4 changed files with 18 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using System.Diagnostics; using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PiwigoDirectorySync.Infrastructure; using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Services; using PiwigoDirectorySync.Services;
@ -23,7 +24,7 @@ internal class SyncAlbumsCommand : CancellableAsyncCommand<SyncAlbumsCommand.Syn
var stopWatch = Stopwatch.StartNew(); var stopWatch = Stopwatch.StartNew();
await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, cancellationToken); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, settings.CreateAlbumsOnServer, cancellationToken);
stopWatch.Stop(); stopWatch.Stop();
_logger.LogInformation("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, _logger.LogInformation("Synchronized all albums with piwigo server {SettingsPiwigoServerId} in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId,
@ -34,5 +35,8 @@ internal class SyncAlbumsCommand : CancellableAsyncCommand<SyncAlbumsCommand.Syn
internal class SyncAlbumsSettings : CommonCommandSettings internal class SyncAlbumsSettings : CommonCommandSettings
{ {
[CommandOption("-c|--create-albums")]
[DefaultValue(true)]
public bool CreateAlbumsOnServer { get; set; }
} }
} }

View File

@ -33,7 +33,7 @@ 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.LogInformation("running album synchronization"); _logger.LogInformation("running album synchronization");
await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, cancellationToken); await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, true, cancellationToken);
_logger.LogInformation("running image synchronization"); _logger.LogInformation("running image synchronization");
await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken); await _imageSynchronizer.SynchronizeImagesAsync(settings.PiwigoServerId, cancellationToken);

View File

@ -20,7 +20,7 @@ internal class AlbumSynchronizer : IAlbumSynchronizer
_persistenceContext = persistenceContext; _persistenceContext = persistenceContext;
} }
public async Task SynchronizeAlbums(int piwigoServerId, CancellationToken ct) public async Task SynchronizeAlbums(int piwigoServerId, bool createAlbumsOnServer, 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)
@ -32,8 +32,16 @@ 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)
{
_logger.LogInformation("Creating missing albums on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name);
await AddMissingAlbumsToServerAsync(piwigoClient, piwigoServer, ct); await AddMissingAlbumsToServerAsync(piwigoClient, piwigoServer, ct);
} }
else
{
_logger.LogInformation("Skipping album creation on server {PiwigoServerId} - {PiwigoServerName}", piwigoServer.Id, piwigoServer.Name);
}
}
private async Task AddMissingAlbumsToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) private async Task AddMissingAlbumsToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
{ {

View File

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