| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data; |
| | | 3 | | using Anichron.Core.Data.Repository; |
| | | 4 | | using Anichron.Core.Domain; |
| | | 5 | | using Anichron.Core.Work; |
| | | 6 | | using Anichron.Worker.Ingestion; |
| | | 7 | | using Anichron.Worker.Ingestion.Proxy; |
| | | 8 | | using Anichron.Worker.Settings; |
| | | 9 | | using Anichron.Worker.Work; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | using System.IO.Abstractions; |
| | | 12 | | |
| | | 13 | | namespace Anichron.Worker.CompactPreviews; |
| | | 14 | | |
| | | 15 | | public sealed record CompactPreviewResult |
| | | 16 | | { |
| | | 17 | | public int Converted { get; init; } |
| | | 18 | | public int Failed { get; init; } |
| | | 19 | | public int Remaining { get; init; } |
| | | 20 | | } |
| | | 21 | | |
| | 12 | 22 | | internal sealed partial class CompactPreviewHandler( |
| | 12 | 23 | | ActiveStorageConfigs activeConfigs, |
| | 12 | 24 | | IMediaAssetRepository assets, |
| | 12 | 25 | | IUnitOfWork unitOfWork, |
| | 12 | 26 | | IWorkQueue queue, |
| | 12 | 27 | | IProxyDirectoryStrategy directories, |
| | 12 | 28 | | ProxyStagingWriter writer, |
| | 12 | 29 | | ICompactPreviewEncoder encoder, |
| | 12 | 30 | | IFileSystem fileSystem, |
| | 12 | 31 | | IGuidFactory guids, |
| | 12 | 32 | | IOptions<WorkerSettings> options, |
| | 12 | 33 | | ILogger<CompactPreviewHandler> logger) : IWorkHandler<CompactPreviewPayload> |
| | | 34 | | { |
| | | 35 | | internal const string FileName = "compact-preview.jxl"; |
| | | 36 | | |
| | | 37 | | public async Task<object?> RunAsync(CompactPreviewPayload payload, WorkRun run, CancellationToken ct) |
| | 11 | 38 | | { |
| | 11 | 39 | | if (await activeConfigs.FindAsync(CompactPreviewPayload.Kind, payload.StorageConfigId, ct) is not { } config) |
| | 1 | 40 | | return StorageConfigSkipped.MissingOrInactive; |
| | | 41 | | |
| | 10 | 42 | | var chunk = await assets.GetWithoutCompactPreviewAsync(config.Id, payload.After, options.Value.CompactPreviewChu |
| | 10 | 43 | | var attemptId = guids.NewGuid(); |
| | 10 | 44 | | var (converted, failed) = (0, 0); |
| | 55 | 45 | | foreach (var asset in chunk) |
| | 13 | 46 | | { |
| | 13 | 47 | | if (await ConvertAsync(asset, attemptId, ct)) |
| | 8 | 48 | | converted++; |
| | | 49 | | else |
| | 4 | 50 | | failed++; |
| | 12 | 51 | | run.Progress.Report(converted + failed, chunk.Count, "Making compact previews"); |
| | 12 | 52 | | } |
| | | 53 | | |
| | 9 | 54 | | var after = chunk.Count > 0 ? chunk[^1].Id : payload.After; |
| | 9 | 55 | | var remaining = await assets.CountWithoutCompactPreviewAsync(config.Id, after, ct); |
| | 9 | 56 | | if (remaining > 0) |
| | 2 | 57 | | await queue.EnqueueAsync(new CompactPreviewPayload(config.Id, after), null, ct); |
| | | 58 | | |
| | 9 | 59 | | Log.Finished(logger, config.Id, converted, failed, remaining); |
| | 9 | 60 | | return new CompactPreviewResult { Converted = converted, Failed = failed, Remaining = remaining }; |
| | 10 | 61 | | } |
| | | 62 | | |
| | | 63 | | private async Task<bool> ConvertAsync(MediaAsset asset, Guid attemptId, CancellationToken ct) |
| | 13 | 64 | | { |
| | | 65 | | ProxyFile proxy; |
| | | 66 | | try |
| | 13 | 67 | | { |
| | | 68 | | // Stryker disable once Linq |
| | 13 | 69 | | var preview = asset.ProxyFiles.Single(p => p.ProxyType == ProxyType.FullPreview); |
| | 13 | 70 | | var source = StoragePath.ResolveProxy(preview.ProxyPath) |
| | 13 | 71 | | ?? throw new InvalidOperationException($"Proxy path {preview.ProxyPath} resolves outside the proxy root. |
| | 12 | 72 | | var bytes = await fileSystem.File.ReadAllBytesAsync(source, ct); |
| | 11 | 73 | | proxy = await writer.PlaceAsync( |
| | 11 | 74 | | asset.Id, |
| | 11 | 75 | | $"{directories.GetDirectory(asset.StorageConfigId, asset.ContentHash)}/{FileName}", |
| | 11 | 76 | | ProxyType.CompactPreview, |
| | 11 | 77 | | attemptId, |
| | 11 | 78 | | (path, token) => fileSystem.File.WriteAllBytesAsync(path, encoder.Encode(bytes), token), |
| | 11 | 79 | | ct); |
| | 8 | 80 | | } |
| | 5 | 81 | | catch (Exception ex) when (!IngestionCancellation.IsRequested(ex, ct)) |
| | 4 | 82 | | { |
| | 4 | 83 | | Log.Failed(logger, asset.Id, ex); |
| | 4 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 8 | 87 | | assets.AddProxyFiles([proxy]); |
| | 8 | 88 | | await unitOfWork.SaveChangesAsync(ct); |
| | 8 | 89 | | return true; |
| | 12 | 90 | | } |
| | | 91 | | |
| | | 92 | | private static partial class Log |
| | | 93 | | { |
| | | 94 | | [LoggerMessage(Level = LogLevel.Information, Message = "Compact previews for storage config {Id}: {Converted} ma |
| | | 95 | | public static partial void Finished(ILogger logger, Guid id, int converted, int failed, int remaining); |
| | | 96 | | |
| | | 97 | | [LoggerMessage(Level = LogLevel.Warning, Message = "Making the compact preview of asset {AssetId} failed.")] |
| | | 98 | | public static partial void Failed(ILogger logger, Guid assetId, Exception ex); |
| | | 99 | | } |
| | | 100 | | } |