| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 4 | | using System.IO.Abstractions; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Worker.Ingestion.Classification; |
| | | 7 | | |
| | 114 | 8 | | internal sealed class IngestionClassifier(IMediaAssetRepository repository, IFileSystem fileSystem) |
| | | 9 | | { |
| | | 10 | | public async Task<IngestionClassification> ClassifyAsync(IngestionContext context, CancellationToken ct) |
| | 112 | 11 | | { |
| | 112 | 12 | | var (item, contentHash, secondaryHash) = (context.Item, context.ContentHash!, context.SecondaryHash!); |
| | 112 | 13 | | var mov = item.SecondaryFile; |
| | 112 | 14 | | string[] hashes = mov is null ? [contentHash] : [contentHash, secondaryHash]; |
| | 112 | 15 | | string[] paths = mov is null ? [item.RelativePath] : [item.RelativePath, mov.RelativePath]; |
| | | 16 | | |
| | 112 | 17 | | var rows = await repository.FindByHashesOrPathsAsync(context.Config.Id, hashes, paths, ct); |
| | | 18 | | |
| | 112 | 19 | | var rootPath = context.Config.RootPath; |
| | 112 | 20 | | var primary = (mov is null ? LoneLivePhotoVideo(rows, item.RelativePath, ownVideoIds: []) : null) |
| | 112 | 21 | | ?? Classify(rows, contentHash, item.RelativePath, rootPath, AtPath(rows, item.RelativePath)); |
| | 112 | 22 | | var secondary = mov is null ? null : ClassifyVideo(rows, primary, secondaryHash, mov.RelativePath, rootPath); |
| | 112 | 23 | | return new(primary, secondary, PairingFor(primary, secondary)); |
| | 112 | 24 | | } |
| | | 25 | | |
| | | 26 | | private FileClassification ClassifyVideo( |
| | | 27 | | IReadOnlyList<MediaAsset> rows, |
| | | 28 | | FileClassification photo, |
| | | 29 | | string contentHash, |
| | | 30 | | string relativePath, |
| | | 31 | | string rootPath) |
| | 68 | 32 | | { |
| | 68 | 33 | | var paired = rows.FirstOrDefault(row => row.Id == photo.Row?.PairedAssetId); |
| | 68 | 34 | | if (paired is not null) |
| | 32 | 35 | | { |
| | 32 | 36 | | var holder = rows.FirstOrDefault(row => row.FilePath == relativePath && row != paired); |
| | 32 | 37 | | return Classify(rows, contentHash, relativePath, rootPath, holder ?? paired); |
| | | 38 | | } |
| | | 39 | | |
| | 36 | 40 | | var movedPhoto = photo.Outcome == FileOutcome.OverwrittenByMove ? photo.Retired : null; |
| | 36 | 41 | | return LoneLivePhotoVideo(rows, relativePath, [photo.Row?.PairedAssetId, movedPhoto?.PairedAssetId]) |
| | 36 | 42 | | ?? Classify(rows, contentHash, relativePath, rootPath, AtPath(rows, relativePath)); |
| | 68 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static MediaAsset? AtPath(IReadOnlyList<MediaAsset> rows, string relativePath) |
| | 148 | 46 | | => rows.FirstOrDefault(row => row.FilePath == relativePath); |
| | | 47 | | |
| | | 48 | | private static FileClassification? LoneLivePhotoVideo( |
| | | 49 | | IReadOnlyList<MediaAsset> rows, string relativePath, Guid?[] ownVideoIds) |
| | 80 | 50 | | { |
| | 80 | 51 | | if (rows.FirstOrDefault(row => row.IsLivePhotoVideo && !ownVideoIds.Contains(row.Id)) is not { } video) |
| | 65 | 52 | | return null; |
| | | 53 | | |
| | 15 | 54 | | var atPath = AtPath(rows, relativePath); |
| | 15 | 55 | | return new( |
| | 15 | 56 | | FileOutcome.LoneLivePhotoVideo, |
| | 15 | 57 | | video, |
| | 15 | 58 | | Retired: atPath is { IsLivePhotoVideo: false, IsSoftDeleted: false } ? atPath : null); |
| | 80 | 59 | | } |
| | | 60 | | |
| | | 61 | | private FileClassification Classify( |
| | | 62 | | IReadOnlyList<MediaAsset> rows, string contentHash, string relativePath, string rootPath, MediaAsset? atPath) |
| | 165 | 63 | | { |
| | 165 | 64 | | var stored = rows.FirstOrDefault(row => !row.IsSoftDeleted && row.ContentHash == contentHash); |
| | 165 | 65 | | if (stored is null) |
| | 51 | 66 | | return ClassifyUnstoredContent(rows, contentHash, atPath); |
| | 114 | 67 | | if (stored == atPath) |
| | 55 | 68 | | return new(stored.FilePath == relativePath ? FileOutcome.Unchanged : FileOutcome.Moved, stored); |
| | 59 | 69 | | if (fileSystem.File.Exists(fileSystem.Path.Combine(rootPath, stored.FilePath))) |
| | 22 | 70 | | { |
| | 22 | 71 | | return atPath is { IsSoftDeleted: false } |
| | 22 | 72 | | ? new(FileOutcome.OverwrittenByCopy, stored, Retired: atPath) |
| | 22 | 73 | | : new(FileOutcome.DuplicateCopy, stored); |
| | | 74 | | } |
| | | 75 | | |
| | 37 | 76 | | return atPath is null ? new(FileOutcome.Moved, stored) : new(FileOutcome.OverwrittenByMove, atPath, Retired: sto |
| | 165 | 77 | | } |
| | | 78 | | |
| | | 79 | | private static FileClassification ClassifyUnstoredContent( |
| | | 80 | | IReadOnlyList<MediaAsset> rows, string contentHash, MediaAsset? atPath) |
| | 51 | 81 | | { |
| | 51 | 82 | | if (atPath is { IsSoftDeleted: false }) |
| | 14 | 83 | | return new(FileOutcome.ContentChanged, atPath); |
| | 37 | 84 | | if (atPath is not null) |
| | 12 | 85 | | { |
| | 12 | 86 | | return atPath.ContentHash == contentHash |
| | 12 | 87 | | ? new(FileOutcome.AutoRestored, atPath) |
| | 12 | 88 | | : new(FileOutcome.AutoRestoredContentChanged, atPath); |
| | | 89 | | } |
| | | 90 | | |
| | 25 | 91 | | var softDeleted = rows |
| | 25 | 92 | | .Where(row => row.ContentHash == contentHash) |
| | 25 | 93 | | .MaxBy(row => row.LastSeenOnNas); |
| | 25 | 94 | | return softDeleted is null ? new(FileOutcome.New, null) : new(FileOutcome.AutoRestoredMoved, softDeleted); |
| | 51 | 95 | | } |
| | | 96 | | |
| | | 97 | | private static PairingAction PairingFor(FileClassification photo, FileClassification? video) |
| | 112 | 98 | | { |
| | 112 | 99 | | if (photo.Row is not { } photoRow |
| | 112 | 100 | | || photo.Outcome is FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged |
| | 112 | 101 | | || video is not { DefersToAnotherRow: false }) |
| | 67 | 102 | | { |
| | 67 | 103 | | return PairingAction.None; |
| | | 104 | | } |
| | | 105 | | |
| | 45 | 106 | | if (video.Row is not { } videoRow || photoRow.PairedAssetId != videoRow.Id) |
| | 23 | 107 | | return video.TakesContent ? PairingAction.PointAtMov : PairingAction.AttachMov; |
| | 22 | 108 | | return video.TakesContent ? PairingAction.None : PairingAction.BackfillMovProxy; |
| | 112 | 109 | | } |
| | | 110 | | } |