| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | |
| | | 3 | | namespace Anichron.Worker.Ingestion.Classification; |
| | | 4 | | |
| | | 5 | | internal enum FileOutcome |
| | | 6 | | { |
| | | 7 | | New = 0, |
| | | 8 | | Unchanged = 1, |
| | | 9 | | Moved = 2, |
| | | 10 | | DuplicateCopy = 3, |
| | | 11 | | ContentChanged = 5, |
| | | 12 | | OverwrittenByCopy = 6, |
| | | 13 | | OverwrittenByMove = 7, |
| | | 14 | | AutoRestored = 8, |
| | | 15 | | AutoRestoredMoved = 9, |
| | | 16 | | AutoRestoredContentChanged = 10, |
| | | 17 | | LoneLivePhotoVideo = 11, |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | internal enum PairingAction |
| | | 21 | | { |
| | | 22 | | None = 0, |
| | | 23 | | AttachMov = 1, |
| | | 24 | | BackfillMovProxy = 2, |
| | | 25 | | PointAtMov = 3, |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>What one crawled file is, and the stored rows its outcome acts on.</summary> |
| | | 29 | | /// <param name="Outcome">What the crawled file is.</param> |
| | | 30 | | /// <param name="Stored">The row the outcome acts on.</param> |
| | | 31 | | /// <param name="Retired">The row the outcome soft-deletes.</param> |
| | | 32 | | internal sealed record FileClassification(FileOutcome Outcome, MediaAsset? Stored, MediaAsset? Retired = null) |
| | | 33 | | { |
| | | 34 | | /// <summary>The row that describes the file at its path once the outcome is applied; null when the file is new or a |
| | | 35 | | public MediaAsset? Row => DefersToAnotherRow ? null : Stored; |
| | | 36 | | |
| | | 37 | | /// <summary>Whether the file holds content another row keeps, so it has no row of its own.</summary> |
| | | 38 | | public bool DefersToAnotherRow => Outcome is FileOutcome.DuplicateCopy or FileOutcome.OverwrittenByCopy or FileOutco |
| | | 39 | | |
| | | 40 | | /// <summary>Whether <see cref="Stored"/> takes content it does not hold yet.</summary> |
| | | 41 | | public bool TakesContent |
| | | 42 | | => Outcome is FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged or FileOutcome.OverwrittenByM |
| | | 43 | | } |
| | | 44 | | |
| | 277 | 45 | | internal sealed record IngestionClassification( |
| | 277 | 46 | | FileClassification Primary, |
| | 277 | 47 | | FileClassification? Secondary, |
| | 277 | 48 | | PairingAction Pairing) |
| | | 49 | | { |
| | | 50 | | public bool NeedsProcessing |
| | 129 | 51 | | => Primary.Outcome is FileOutcome.New or FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The rows soft-deleted before the item's save, so another row can take their content: every half's retired |
| | | 55 | | /// row once either half was overwritten by a move, otherwise none. |
| | | 56 | | /// </summary> |
| | | 57 | | public IReadOnlyList<MediaAsset> RetiredFirst |
| | 134 | 58 | | => Halves.Any(file => file.Outcome == FileOutcome.OverwrittenByMove) |
| | 134 | 59 | | ? [.. Halves.Select(file => file.Retired).OfType<MediaAsset>()] |
| | 134 | 60 | | : []; |
| | | 61 | | |
| | 157 | 62 | | private IEnumerable<FileClassification> Halves => Secondary is null ? [Primary] : [Primary, Secondary]; |
| | | 63 | | |
| | 34 | 64 | | public int DuplicateCopies => NeedsProcessing |
| | 34 | 65 | | ? 0 |
| | 34 | 66 | | : new[] { Primary, Secondary }.Count(file => file?.Outcome is FileOutcome.DuplicateCopy or FileOutcome.Overwritt |
| | | 67 | | } |