makes image upload 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
8718b67176
commit
b92aabfbcb
@ -15,6 +15,9 @@ public class ImageEntity
|
|||||||
|
|
||||||
public required string FilePath { get; set; }
|
public required string FilePath { get; set; }
|
||||||
|
|
||||||
|
public string Name => Path.GetFileNameWithoutExtension(FilePath);
|
||||||
|
public string FileName => Path.GetFileName(FilePath);
|
||||||
|
|
||||||
public DateTime LastChange { get; set; }
|
public DateTime LastChange { get; set; }
|
||||||
public string? Md5Sum { get; set; }
|
public string? Md5Sum { get; set; }
|
||||||
|
|
||||||
|
@ -1,11 +1,103 @@
|
|||||||
namespace PiwigoDirectorySync.Services;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Piwigo.Client;
|
||||||
|
using Piwigo.Client.Images;
|
||||||
|
using PiwigoDirectorySync.Infrastructure;
|
||||||
|
using PiwigoDirectorySync.Persistence;
|
||||||
|
|
||||||
|
namespace PiwigoDirectorySync.Services;
|
||||||
|
|
||||||
public class ImageSynchronizer : IImageSynchronizer
|
public class ImageSynchronizer : IImageSynchronizer
|
||||||
{
|
{
|
||||||
public Task SynchronizeImages(int piwigoServerId, CancellationToken ct)
|
private readonly ILogger<ImageSynchronizer> _logger;
|
||||||
|
private readonly PersistenceContext _persistenceContext;
|
||||||
|
private readonly IPiwigoClientFactory _piwigoClientFactory;
|
||||||
|
|
||||||
|
public ImageSynchronizer(ILogger<ImageSynchronizer> logger, IPiwigoClientFactory piwigoClientFactory, PersistenceContext persistenceContext)
|
||||||
{
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_piwigoClientFactory = piwigoClientFactory;
|
||||||
|
_persistenceContext = persistenceContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SynchronizeImages(int piwigoServerId, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var piwigoServer = await _persistenceContext.PiwigoServers.FindAsync(new object?[] { piwigoServerId }, ct);
|
||||||
|
if (piwigoServer is null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Could not sync images with piwigo server {PiwigoServerId}", piwigoServerId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
throw new NotImplementedException();
|
_logger.LogInformation("Synchronizing images of piwigo server {PiwigoServerName} using base path {PiwigoServerRootDirectory}", piwigoServer.Name,
|
||||||
|
piwigoServer.RootDirectory);
|
||||||
|
|
||||||
|
var piwigoClient = await _piwigoClientFactory.GetPiwigoClientAsync(piwigoServer, ct);
|
||||||
|
|
||||||
|
await GetImageIdsFromServerAsync(piwigoClient, piwigoServer, ct);
|
||||||
|
await UploadNewImagesToServerAsync(piwigoClient, piwigoServer, ct);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UploadNewImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var imagesToUpload = await _persistenceContext.PiwigoImages.Include(i => i.Album)
|
||||||
|
.Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id)
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
foreach (var imageEntity in imagesToUpload)
|
||||||
|
{
|
||||||
|
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath));
|
||||||
|
var imageUpload = GetImageUpload(imageEntity, fileInfo.CreationTime);
|
||||||
|
var imageUploaded = await piwigoClient.UploadImageAsync(fileInfo, imageUpload, ct);
|
||||||
|
|
||||||
|
imageEntity.ServerImageId = imageUploaded.ImageId;
|
||||||
|
imageEntity.UploadRequired = false;
|
||||||
|
|
||||||
|
_logger.LogInformation("Uploaded image {ImageEntityName} ({ImageEntityId}) to piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id,
|
||||||
|
imageEntity.ServerImageId);
|
||||||
|
|
||||||
|
await _persistenceContext.SaveChangesAsync(ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ImageUpload GetImageUpload(ImageEntity imageEntity, DateTime createdAt)
|
||||||
|
{
|
||||||
|
var albums = new List<(int AlbumId, int? Rank)> { (AlbumId: imageEntity.Album.ServerAlbumId!.Value, Rank: null) };
|
||||||
|
return new ImageUpload(imageEntity.Md5Sum!)
|
||||||
|
{
|
||||||
|
Name = imageEntity.Name,
|
||||||
|
FileName = imageEntity.FileName,
|
||||||
|
OriginalSum = imageEntity.Md5Sum!,
|
||||||
|
Albums = albums,
|
||||||
|
CreatedAt = createdAt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task GetImageIdsFromServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var imagesToSearch = await _persistenceContext.PiwigoImages.Include(i => i.Album)
|
||||||
|
.Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id)
|
||||||
|
.ToListAsync(ct);
|
||||||
|
|
||||||
|
var md5SumsToCheck = imagesToSearch.Where(i => i.Md5Sum != null).DistinctBy(i => i.Md5Sum).ToDictionary(i => i.Md5Sum!, i => i, StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
var processedImages = 0;
|
||||||
|
while (processedImages < md5SumsToCheck.Keys.Count)
|
||||||
|
{
|
||||||
|
var chunkKeys = md5SumsToCheck.Keys.Skip(processedImages).Take(100).ToArray();
|
||||||
|
var existingImages = await piwigoClient.Image.ExistsByMd5SumsAsync(chunkKeys, ct);
|
||||||
|
|
||||||
|
foreach (var existingImage in existingImages.Where(i => i.Value.HasValue))
|
||||||
|
{
|
||||||
|
var imageEntity = md5SumsToCheck[existingImage.Key];
|
||||||
|
imageEntity.ServerImageId = existingImage.Value;
|
||||||
|
_logger.LogInformation("Found image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id,
|
||||||
|
imageEntity.ServerImageId);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _persistenceContext.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
processedImages += chunkKeys.Length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,8 @@
|
|||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Debug",
|
"Default": "Debug",
|
||||||
"System": "Warning",
|
"System": "Warning",
|
||||||
"Microsoft": "Warning"
|
"Microsoft": "Warning",
|
||||||
|
"Piwigo.Client": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
|
Loading…
Reference in New Issue
Block a user