makes album sync work
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good
This commit is contained in:
parent
b67f85cd85
commit
f86cacac20
@ -18,4 +18,9 @@ public static class ExtensionMethods
|
|||||||
{
|
{
|
||||||
return await dbSet.Where(a => a.ServerId == piwigoServerId && a.ServerAlbumId == serverAlbumId).FirstOrDefaultAsync(ct);
|
return await dbSet.Where(a => a.ServerId == piwigoServerId && a.ServerAlbumId == serverAlbumId).FirstOrDefaultAsync(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Task<AlbumEntity> GetByIdAsync(this DbSet<AlbumEntity> dbSet, int serverId, CancellationToken ct)
|
||||||
|
{
|
||||||
|
return dbSet.Where(a => a.Id == serverId).FirstAsync(ct);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Piwigo.Client;
|
using Piwigo.Client;
|
||||||
using Piwigo.Client.Albums;
|
using Piwigo.Client.Albums;
|
||||||
using PiwigoDirectorySync.Infrastructure;
|
using PiwigoDirectorySync.Infrastructure;
|
||||||
@ -30,16 +31,38 @@ public class AlbumSynchronizer : IAlbumSynchronizer
|
|||||||
var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
|
var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
|
||||||
|
|
||||||
await UpdatePiwigoAlbumsFromServerAsync(dbContext, piwigoClient, 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 serverAlbums = await piwigoClient.Album.GetListAsync(null, true, false, ThumbnailSize.Thumb, ct);
|
||||||
var serverAlbumDictionary = serverAlbums.ToDictionary(a => a.Id, a => a);
|
var serverAlbumDictionary = serverAlbums.ToDictionary(a => a.Id, a => a);
|
||||||
|
|
||||||
foreach (var serverAlbum in serverAlbums)
|
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)
|
if (serverAlbum.IdUpperCat.HasValue)
|
||||||
{
|
{
|
||||||
albumEntity.ParentId = (await dbContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.IdUpperCat.Value, ct))?.Id;
|
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<AlbumEntity> GetOrAddPiwigoAlbumEntityAsync(PersistenceContext dbContext, ServerEntity piwigoServer, Album serverAlbum,
|
private async Task<AlbumEntity> GetOrAddPiwigoAlbumEntityFromServerAsync(PersistenceContext dbContext, ServerEntity piwigoServer, Album serverAlbum,
|
||||||
IDictionary<int, Album> serverAlbumDictionary, CancellationToken ct)
|
IDictionary<int, Album> serverAlbumDictionary, CancellationToken ct)
|
||||||
{
|
{
|
||||||
|
// Already synchronized so it is easy to return
|
||||||
var albumEntity = await dbContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.Id, ct);
|
var albumEntity = await dbContext.PiwigoAlbums.FindByServerIdAsync(piwigoServer.Id, serverAlbum.Id, ct);
|
||||||
if (albumEntity != null)
|
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;
|
return albumEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,14 +102,16 @@ public class AlbumSynchronizer : IAlbumSynchronizer
|
|||||||
Server = piwigoServer,
|
Server = piwigoServer,
|
||||||
Name = serverAlbum.Name,
|
Name = serverAlbum.Name,
|
||||||
ServerAlbumId = serverAlbum.Id,
|
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);
|
dbContext.PiwigoAlbums.Add(albumEntity);
|
||||||
|
|
||||||
return albumEntity;
|
return albumEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GeneratePath(Album serverAlbum, IDictionary<int, Album> serverAlbumDictionary)
|
private string GeneratePath(Album serverAlbum, IDictionary<int, Album> serverAlbumDictionary)
|
||||||
{
|
{
|
||||||
if (!serverAlbum.IdUpperCat.HasValue)
|
if (!serverAlbum.IdUpperCat.HasValue)
|
||||||
{
|
{
|
||||||
@ -87,6 +127,8 @@ public class AlbumSynchronizer : IAlbumSynchronizer
|
|||||||
parentId = currentParent.IdUpperCat ?? 0;
|
parentId = currentParent.IdUpperCat ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug("Generated path {Path} for album with piwigo id {ServerAlbumId}", path, serverAlbum.Id);
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user