From f86cacac20e3a2529d0cf7cb865680aeff5826ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Thu, 31 Aug 2023 23:24:19 +0200 Subject: [PATCH] makes album sync work --- .../Persistence/ExtensionMethods.cs | 5 ++ .../Services/AlbumSynchronizer.cs | 54 ++++++++++++++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/PiwigoDirectorySync/Persistence/ExtensionMethods.cs b/PiwigoDirectorySync/Persistence/ExtensionMethods.cs index e76b633..8570870 100644 --- a/PiwigoDirectorySync/Persistence/ExtensionMethods.cs +++ b/PiwigoDirectorySync/Persistence/ExtensionMethods.cs @@ -18,4 +18,9 @@ public static class ExtensionMethods { return await dbSet.Where(a => a.ServerId == piwigoServerId && a.ServerAlbumId == serverAlbumId).FirstOrDefaultAsync(ct); } + + public static Task GetByIdAsync(this DbSet dbSet, int serverId, CancellationToken ct) + { + return dbSet.Where(a => a.Id == serverId).FirstAsync(ct); + } } \ No newline at end of file diff --git a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs index 74d52e6..fc0e759 100644 --- a/PiwigoDirectorySync/Services/AlbumSynchronizer.cs +++ b/PiwigoDirectorySync/Services/AlbumSynchronizer.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using Piwigo.Client; using Piwigo.Client.Albums; using PiwigoDirectorySync.Infrastructure; @@ -30,16 +31,38 @@ public class AlbumSynchronizer : IAlbumSynchronizer var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct); await UpdatePiwigoAlbumsFromServerAsync(dbContext, piwigoClient, piwigoServer, ct); + await AddMissingAlbumsToServerAsync(dbContext, piwigoClient, piwigoServer, ct); } - private static async Task UpdatePiwigoAlbumsFromServerAsync(PersistenceContext dbContext, IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) + private static async Task AddMissingAlbumsToServerAsync(PersistenceContext dbContext, IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) + { + var albumsToCreate = await dbContext.PiwigoAlbums.Where(a => a.ServerAlbumId == null && a.ServerId == piwigoServer.Id) + .OrderBy(a => a.Path) + .Select(a => a.Id) + .ToListAsync(ct); + + foreach (var albumId in albumsToCreate) + { + var albumEntity = await dbContext.PiwigoAlbums.GetByIdAsync(albumId, ct); + var piwigoParentId = albumEntity.ParentId.HasValue ? (await dbContext.PiwigoAlbums.GetByIdAsync(albumEntity.ParentId.Value, ct)).ServerAlbumId : null; + + albumEntity.ServerAlbumId = await piwigoClient.Album.AddAsync(albumEntity.Name, piwigoParentId, visible: true, position: AlbumPosition.First, + status: AlbumStatus.Public, cancellationToken: ct); + + await dbContext.SaveChangesAsync(ct); + } + } + + private async Task UpdatePiwigoAlbumsFromServerAsync(PersistenceContext dbContext, IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct) { var serverAlbums = await piwigoClient.Album.GetListAsync(null, true, false, ThumbnailSize.Thumb, ct); var serverAlbumDictionary = serverAlbums.ToDictionary(a => a.Id, a => a); foreach (var serverAlbum in serverAlbums) { - var albumEntity = await GetOrAddPiwigoAlbumEntityAsync(dbContext, piwigoServer, serverAlbum, serverAlbumDictionary, ct); + _logger.LogInformation("Updating piwigo server album {ServerAlbumName} with piwigo id {ServerAlbumId}", serverAlbum.Name, serverAlbum.Id); + + var albumEntity = await GetOrAddPiwigoAlbumEntityFromServerAsync(dbContext, piwigoServer, serverAlbum, serverAlbumDictionary, ct); if (serverAlbum.IdUpperCat.HasValue) { albumEntity.ParentId = (await dbContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.IdUpperCat.Value, ct))?.Id; @@ -49,12 +72,27 @@ public class AlbumSynchronizer : IAlbumSynchronizer } } - private static async Task GetOrAddPiwigoAlbumEntityAsync(PersistenceContext dbContext, ServerEntity piwigoServer, Album serverAlbum, + private async Task GetOrAddPiwigoAlbumEntityFromServerAsync(PersistenceContext dbContext, ServerEntity piwigoServer, Album serverAlbum, IDictionary serverAlbumDictionary, CancellationToken ct) { + // Already synchronized so it is easy to return var albumEntity = await dbContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.Id, ct); if (albumEntity != null) { + _logger.LogDebug("Found existing album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + albumEntity.ServerAlbumId); + return albumEntity; + } + + // might exist already as the file system got scanned and created the local entries + // In this case we save the server id in our local album and link them. + var path = GeneratePath(serverAlbum, serverAlbumDictionary); + albumEntity = await dbContext.PiwigoAlbums.FindByServerAndPathAsync(piwigoServer.Id, path, ct); + if (albumEntity != null) + { + albumEntity.ServerAlbumId = serverAlbum.Id; + _logger.LogInformation("Linking existing album {AlbumEntityName} with local id {AlbumEntityId} to piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + albumEntity.ServerAlbumId); return albumEntity; } @@ -64,14 +102,16 @@ public class AlbumSynchronizer : IAlbumSynchronizer Server = piwigoServer, Name = serverAlbum.Name, ServerAlbumId = serverAlbum.Id, - Path = GeneratePath(serverAlbum, serverAlbumDictionary) + Path = path }; + _logger.LogInformation("Adding piwigo album {AlbumEntityName} with local id {AlbumEntityId} and piwigo server id {ServerAlbumId}", albumEntity.Name, albumEntity.Id, + albumEntity.ServerAlbumId); dbContext.PiwigoAlbums.Add(albumEntity); return albumEntity; } - private static string GeneratePath(Album serverAlbum, IDictionary serverAlbumDictionary) + private string GeneratePath(Album serverAlbum, IDictionary serverAlbumDictionary) { if (!serverAlbum.IdUpperCat.HasValue) { @@ -87,6 +127,8 @@ public class AlbumSynchronizer : IAlbumSynchronizer parentId = currentParent.IdUpperCat ?? 0; } + _logger.LogDebug("Generated path {Path} for album with piwigo id {ServerAlbumId}", path, serverAlbum.Id); + return path; } } \ No newline at end of file