| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 4 | | using NodaTime; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Worker.Ingestion.Classification; |
| | | 7 | | |
| | | 8 | | /// <summary>The content a row takes, from processing a file or from another row.</summary> |
| | | 9 | | /// <param name="ContentHash">The content's hash.</param> |
| | | 10 | | /// <param name="Captured">The capture date the row takes; null makes it undated.</param> |
| | | 11 | | /// <param name="MediaType">The content's media type.</param> |
| | | 12 | | /// <param name="Description">What extraction read, owned by the row that takes it.</param> |
| | | 13 | | /// <param name="Blurhash">The content's placeholder.</param> |
| | | 14 | | /// <param name="Proxies">The content's proxies, owned by the row that takes it.</param> |
| | 102 | 15 | | internal sealed record AssetContent( |
| | 102 | 16 | | string ContentHash, |
| | 102 | 17 | | CaptureDate? Captured, |
| | 102 | 18 | | MediaType MediaType, |
| | 102 | 19 | | MediaDescription Description, |
| | 102 | 20 | | string? Blurhash, |
| | 102 | 21 | | IReadOnlyList<ProxyFile> Proxies) |
| | | 22 | | { |
| | | 23 | | /// <summary>The content of <paramref name="source"/>, with a copy of its description, since a row owns its own.</su |
| | | 24 | | /// <param name="source">The row whose content another row takes.</param> |
| | | 25 | | /// <param name="proxies">The proxies the taking row owns.</param> |
| | | 26 | | public static AssetContent Of(MediaAsset source, IReadOnlyList<ProxyFile> proxies) |
| | 26 | 27 | | { |
| | 26 | 28 | | var metadata = source.Metadata ?? new Metadata(); |
| | 26 | 29 | | return new(source.ContentHash, source.Captured, source.MediaType, metadata.Description with { }, metadata.Blurha |
| | 26 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Makes <paramref name="asset"/> hold this content's fields and metadata.</summary> |
| | | 33 | | /// <param name="asset">The row, with its metadata loaded when it has any.</param> |
| | | 34 | | public void ApplyTo(MediaAsset asset) |
| | 100 | 35 | | { |
| | 100 | 36 | | asset.ContentHash = ContentHash; |
| | 100 | 37 | | asset.Captured = Captured; |
| | 100 | 38 | | asset.MediaType = MediaType; |
| | 100 | 39 | | var metadata = asset.Metadata ??= new Metadata { AssetId = asset.Id }; |
| | 100 | 40 | | metadata.Description = Description; |
| | 100 | 41 | | metadata.Blurhash = Blurhash; |
| | 100 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal sealed partial class StoredContent( |
| | | 46 | | IMediaAssetRepository repository, IBurstRepository bursts, IClock clock, ILogger<StoredContent> logger) |
| | | 47 | | { |
| | | 48 | | /// <summary> |
| | | 49 | | /// Makes <paramref name="asset"/> hold <paramref name="content"/>: its content fields, metadata and proxy rows, |
| | | 50 | | /// out of its burst, and active. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="context">The item that stages the auto-restore count.</param> |
| | | 53 | | /// <param name="asset">A tracked row.</param> |
| | | 54 | | /// <param name="content">The content it takes.</param> |
| | | 55 | | /// <param name="ct">Cancels the lookups.</param> |
| | | 56 | | /// <returns>Whether the row was soft-deleted.</returns> |
| | | 57 | | public async Task<bool> TakeAsync(IngestionContext context, MediaAsset asset, AssetContent content, CancellationToke |
| | | 58 | | { |
| | | 59 | | await repository.LoadMetadataAsync(asset, ct); |
| | | 60 | | content.ApplyTo(asset); |
| | | 61 | | ProxyFileRows.Replace(repository, asset, content.Proxies); |
| | | 62 | | asset.BurstId = null; |
| | | 63 | | return await ReactivateAsync(context, asset, ct); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary>Makes the row active and seen now, and clears its burst-checked mark.</summary> |
| | | 67 | | /// <param name="context">The item that stages the auto-restore count.</param> |
| | | 68 | | /// <param name="asset">A tracked row.</param> |
| | | 69 | | /// <param name="ct">Cancels the lookup.</param> |
| | | 70 | | /// <returns>Whether the row was soft-deleted.</returns> |
| | | 71 | | public async Task<bool> ReactivateAsync(IngestionContext context, MediaAsset asset, CancellationToken ct) |
| | | 72 | | { |
| | | 73 | | var wasSoftDeleted = asset.IsSoftDeleted; |
| | | 74 | | if (wasSoftDeleted) |
| | | 75 | | context.StageAutoRestored(); |
| | | 76 | | asset.IsSoftDeleted = false; |
| | | 77 | | asset.LastSeenOnNas = clock.GetCurrentInstant(); |
| | | 78 | | await bursts.RemoveCheckAsync(asset.Id, ct); |
| | | 79 | | return wasSoftDeleted; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Soft-deletes the row and, when it is a Live Photo's photo, its video. A row already soft-deleted is left alone |
| | | 84 | | /// and not counted again. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="context">The item that stages the soft-delete counts.</param> |
| | | 87 | | /// <param name="asset">A tracked row.</param> |
| | | 88 | | /// <param name="ct">Cancels the lookup of the video.</param> |
| | | 89 | | public async Task RetireAsync(IngestionContext context, MediaAsset asset, CancellationToken ct) |
| | | 90 | | { |
| | | 91 | | if (!SoftDelete(context, asset) || asset.PairedAssetId is not { } videoId) |
| | | 92 | | return; |
| | | 93 | | |
| | | 94 | | if (await repository.FindAsync(videoId, ct) is { } video && SoftDelete(context, video)) |
| | | 95 | | Log.RetiredWithItsPhoto(logger, video.Id, video.FilePath, asset.Id); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static bool SoftDelete(IngestionContext context, MediaAsset asset) |
| | | 99 | | { |
| | | 100 | | if (asset.IsSoftDeleted) |
| | | 101 | | return false; |
| | | 102 | | |
| | | 103 | | asset.IsSoftDeleted = true; |
| | | 104 | | context.StageSoftDeleted(); |
| | | 105 | | return true; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static partial class Log |
| | | 109 | | { |
| | | 110 | | [LoggerMessage( |
| | | 111 | | Level = LogLevel.Information, |
| | | 112 | | Message = "Soft-deleted asset {VideoId} at {FilePath} with its photo, asset {PhotoId}: it is that Live Photo |
| | | 113 | | public static partial void RetiredWithItsPhoto(ILogger logger, Guid videoId, string filePath, Guid photoId); |
| | | 114 | | } |
| | | 115 | | } |