adds update of existing images
PiwigoDirectorySync/pipeline/head This commit looks good Details

This commit is contained in:
Philipp Häfelfinger 2023-09-02 16:24:19 +02:00
parent b92aabfbcb
commit c94956abdd
2 changed files with 33 additions and 2 deletions

View File

@ -19,12 +19,12 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.10"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.10"/>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="Piwigo.Client" Version="0.1.0.17"/>
<PackageReference Include="Piwigo.Client" Version="0.1.0.19" />
<PackageReference Include="Spectre.Console.Analyzer" Version="0.47.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -36,6 +36,31 @@ public class ImageSynchronizer : IImageSynchronizer
await GetImageIdsFromServerAsync(piwigoClient, piwigoServer, ct);
await UploadNewImagesToServerAsync(piwigoClient, piwigoServer, ct);
await UploadChangedImagesToServerAsync(piwigoClient, piwigoServer, ct);
}
private async Task UploadChangedImagesToServerAsync(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 && i.UploadRequired)
.ToListAsync(ct);
_logger.LogInformation("Updating {Count} images", imagesToUpload.Count);
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("Updated image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id,
imageEntity.ServerImageId);
await _persistenceContext.SaveChangesAsync(ct);
}
}
private async Task UploadNewImagesToServerAsync(IPiwigoClient piwigoClient, ServerEntity piwigoServer, CancellationToken ct)
@ -44,6 +69,8 @@ public class ImageSynchronizer : IImageSynchronizer
.Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id)
.ToListAsync(ct);
_logger.LogInformation("Uploading {Count} images", imagesToUpload.Count);
foreach (var imageEntity in imagesToUpload)
{
var fileInfo = new FileInfo(Path.Combine(piwigoServer.RootDirectory, imageEntity.FilePath));
@ -79,6 +106,8 @@ public class ImageSynchronizer : IImageSynchronizer
.Where(i => i.ServerImageId == null && i.Album.ServerId == piwigoServer.Id)
.ToListAsync(ct);
_logger.LogInformation("Checking {Count} images if they exist", imagesToSearch.Count);
var md5SumsToCheck = imagesToSearch.Where(i => i.Md5Sum != null).DistinctBy(i => i.Md5Sum).ToDictionary(i => i.Md5Sum!, i => i, StringComparer.OrdinalIgnoreCase);
var processedImages = 0;
@ -91,6 +120,8 @@ public class ImageSynchronizer : IImageSynchronizer
{
var imageEntity = md5SumsToCheck[existingImage.Key];
imageEntity.ServerImageId = existingImage.Value;
imageEntity.UploadRequired = false;
_logger.LogInformation("Found image {ImageEntityName} ({ImageEntityId}) on piwigo server with id {ImageEntityServerImageId}", imageEntity.Name, imageEntity.Id,
imageEntity.ServerImageId);
}