| | | 1 | | using Anichron.Core; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Anichron.Core.Domain; |
| | | 4 | | using Anichron.Worker.Ingestion.Classification; |
| | | 5 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 6 | | using NodaTime; |
| | | 7 | | |
| | | 8 | | namespace Anichron.Worker.Ingestion; |
| | | 9 | | |
| | 135 | 10 | | internal sealed partial class PairedMovResolver( |
| | 135 | 11 | | IMediaAssetRepository repository, |
| | 135 | 12 | | StoredContent storedContent, |
| | 135 | 13 | | IClock clock, |
| | 135 | 14 | | IGuidFactory guidFactory, |
| | 135 | 15 | | ILogger<PairedMovResolver> logger) |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Points <paramref name="photo"/> at the item's MOV row, stored or added now, and marks that row a Live Photo's vi |
| | | 19 | | /// every other photo still pointing at a stored one lets go of it. The video <paramref name="photo"/> lets go of is |
| | | 20 | | /// soft-deleted, unless the item's video half retires it. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="context">The classified Live Photo item, which stages the soft-delete count.</param> |
| | | 23 | | /// <param name="photo">The row that becomes the video's photo, dated already.</param> |
| | | 24 | | /// <param name="ct">Cancels the lookups of the other photos and of the previous video.</param> |
| | | 25 | | /// <returns>The MOV row.</returns> |
| | | 26 | | public async Task<MediaAsset> PairAsync(IngestionContext context, MediaAsset photo, CancellationToken ct) |
| | 48 | 27 | | { |
| | 48 | 28 | | var video = context.Classification!.Secondary!; |
| | 48 | 29 | | var stored = video.Row; |
| | 48 | 30 | | if (stored is not null) |
| | 27 | 31 | | await TakeFromOtherPhotosAsync(photo, stored, ct); |
| | | 32 | | |
| | 48 | 33 | | var mov = stored ?? Add(context); |
| | 48 | 34 | | if (stored is null || video.TakesContent) |
| | 31 | 35 | | DateFromPhoto(mov, photo); |
| | | 36 | | |
| | 48 | 37 | | await LetGoOfPreviousAsync(context, photo, mov, video.Retired, ct); |
| | 48 | 38 | | photo.PairedAssetId = mov.Id; |
| | 48 | 39 | | mov.IsLivePhotoVideo = true; |
| | 48 | 40 | | return mov; |
| | 48 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The one rule that dates a Live Photo's video: it takes its photo's whole capture date, and is undated when the p |
| | | 45 | | /// is or when there is no photo row. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="video">The video row.</param> |
| | | 48 | | /// <param name="photo">Its photo's row as it stands in this item.</param> |
| | 47 | 49 | | public static void DateFromPhoto(MediaAsset video, MediaAsset? photo) => video.Captured = photo?.Captured; |
| | | 50 | | |
| | | 51 | | private async Task TakeFromOtherPhotosAsync(MediaAsset photo, MediaAsset video, CancellationToken ct) |
| | 27 | 52 | | { |
| | 91 | 53 | | foreach (var other in await repository.FindPhotosOfAsync(video.Id, ct)) |
| | 5 | 54 | | { |
| | 5 | 55 | | if (other == photo || other.PairedAssetId != video.Id) |
| | 2 | 56 | | continue; |
| | | 57 | | |
| | 3 | 58 | | other.PairedAssetId = null; |
| | 3 | 59 | | if (!other.IsSoftDeleted) |
| | 2 | 60 | | Log.NowAStillPhoto(logger, other.Id, video.Id, photo.Id); |
| | 3 | 61 | | } |
| | 27 | 62 | | } |
| | | 63 | | |
| | | 64 | | private async Task LetGoOfPreviousAsync( |
| | | 65 | | IngestionContext context, MediaAsset photo, MediaAsset mov, MediaAsset? retiredByTheItem, CancellationToken ct) |
| | 48 | 66 | | { |
| | 48 | 67 | | if (photo.PairedAssetId is not { } previousId || previousId == mov.Id || previousId == retiredByTheItem?.Id) |
| | 37 | 68 | | return; |
| | 11 | 69 | | if (await repository.FindAsync(previousId, ct) is not { IsSoftDeleted: false } previous) |
| | 5 | 70 | | return; |
| | | 71 | | |
| | 6 | 72 | | await storedContent.RetireAsync(context, previous, ct); |
| | 6 | 73 | | Log.LetGo(logger, previous.Id, previous.FilePath, photo.Id, mov.Id); |
| | 48 | 74 | | } |
| | | 75 | | |
| | | 76 | | private MediaAsset Add(IngestionContext context) |
| | 21 | 77 | | { |
| | 21 | 78 | | var mov = context.Item.SecondaryFile!; |
| | 21 | 79 | | var asset = new MediaAsset |
| | 21 | 80 | | { |
| | 21 | 81 | | Id = guidFactory.NewGuid(), |
| | 21 | 82 | | StorageConfigId = context.Config.Id, |
| | 21 | 83 | | FilePath = mov.RelativePath, |
| | 21 | 84 | | FileName = Path.GetFileName(mov.RelativePath), |
| | 21 | 85 | | ContentHash = context.SecondaryHash!, |
| | 21 | 86 | | MediaType = mov.MediaType, |
| | 21 | 87 | | IsSoftDeleted = false, |
| | 21 | 88 | | LastSeenOnNas = clock.GetCurrentInstant(), |
| | 21 | 89 | | }; |
| | 21 | 90 | | repository.Add(asset); |
| | 21 | 91 | | return asset; |
| | 21 | 92 | | } |
| | | 93 | | |
| | | 94 | | private static partial class Log |
| | | 95 | | { |
| | | 96 | | [LoggerMessage( |
| | | 97 | | Level = LogLevel.Information, |
| | | 98 | | Message = "Asset {PhotoId} is now a still photo: its video, asset {VideoId}, is now the video of asset {NewP |
| | | 99 | | public static partial void NowAStillPhoto(ILogger logger, Guid photoId, Guid videoId, Guid newPhotoId); |
| | | 100 | | |
| | | 101 | | [LoggerMessage( |
| | | 102 | | Level = LogLevel.Information, |
| | | 103 | | Message = "Soft-deleted asset {VideoId} at {FilePath}: its photo, asset {PhotoId}, now has another video, as |
| | | 104 | | public static partial void LetGo(ILogger logger, Guid videoId, string filePath, Guid photoId, Guid newVideoId); |
| | | 105 | | } |
| | | 106 | | } |