| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Worker.Crawling; |
| | | 6 | | |
| | | 7 | | /// <summary>Why a crawl soft-deleted nothing, though rows may be missing.</summary> |
| | | 8 | | [JsonConverter(typeof(JsonStringEnumConverter<MissingFileSkip>))] |
| | | 9 | | public enum MissingFileSkip |
| | | 10 | | { |
| | | 11 | | /// <summary>The walk skipped a folder it could not read.</summary> |
| | | 12 | | IncompleteWalk = 0, |
| | | 13 | | |
| | | 14 | | /// <summary>The walk found no media files while the config has active rows, as when a share is not mounted.</summar |
| | | 15 | | EmptyRoot = 1, |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary>What a crawl's missing-file sweep did.</summary> |
| | | 19 | | /// <param name="SoftDeleted">Rows the sweep soft-deleted.</param> |
| | | 20 | | /// <param name="SkippedBecause">Why the sweep soft-deleted nothing, or null when it ran.</param> |
| | | 21 | | internal sealed record MissingFiles(int SoftDeleted, MissingFileSkip? SkippedBecause); |
| | | 22 | | |
| | | 23 | | internal interface IMissingFileSweep |
| | | 24 | | { |
| | | 25 | | Task<MissingFiles> SweepAsync(UserStorageConfig config, CrawlRun run, CancellationToken ct); |
| | | 26 | | } |
| | | 27 | | |
| | 15 | 28 | | internal sealed partial class MissingFileSweep( |
| | 15 | 29 | | IMediaAssetRepository repository, |
| | 15 | 30 | | ILogger<MissingFileSweep> logger) : IMissingFileSweep |
| | | 31 | | { |
| | | 32 | | public async Task<MissingFiles> SweepAsync(UserStorageConfig config, CrawlRun run, CancellationToken ct) |
| | 14 | 33 | | { |
| | 14 | 34 | | if (!run.WalkComplete) |
| | 1 | 35 | | return new MissingFiles(0, MissingFileSkip.IncompleteWalk); |
| | | 36 | | |
| | 13 | 37 | | var active = await repository.GetActivePathsAsync(config.Id, ct); |
| | 13 | 38 | | if (run.FoundPaths.Count == 0 && active.Count > 0) |
| | 1 | 39 | | return new MissingFiles(0, MissingFileSkip.EmptyRoot); |
| | | 40 | | |
| | 12 | 41 | | var missing = active.Where(row => !run.FoundPaths.Contains(row.FilePath)).ToList(); |
| | 12 | 42 | | var missingIds = missing.Select(row => row.Id).ToHashSet(); |
| | 12 | 43 | | var videosOfMissingPhotos = missing.Select(row => row.PairedAssetId).OfType<Guid>().ToHashSet(); |
| | 12 | 44 | | var videosLeftBehind = active |
| | 12 | 45 | | .Where(row => !missingIds.Contains(row.Id) && videosOfMissingPhotos.Contains(row.Id)) |
| | 12 | 46 | | .ToList(); |
| | 12 | 47 | | if (missing.Count + videosLeftBehind.Count == 0) |
| | 3 | 48 | | return new MissingFiles(0, null); |
| | | 49 | | |
| | 53 | 50 | | foreach (var row in missing) |
| | 13 | 51 | | Log.SoftDeleting(logger, row.Id, row.FilePath); |
| | 29 | 52 | | foreach (var row in videosLeftBehind) |
| | 1 | 53 | | Log.SoftDeletingLeftBehindVideo(logger, row.Id, row.FilePath); |
| | 20 | 54 | | foreach (var (photo, video) in |
| | 9 | 55 | | from photo in active |
| | 9 | 56 | | where !missingIds.Contains(photo.Id) |
| | 9 | 57 | | join video in missing on photo.PairedAssetId equals video.Id |
| | 9 | 58 | | select (photo, video)) |
| | 1 | 59 | | { |
| | 1 | 60 | | Log.NowAStillPhoto(logger, photo.Id, video.Id, video.FilePath); |
| | 1 | 61 | | } |
| | | 62 | | |
| | 9 | 63 | | return new MissingFiles( |
| | 9 | 64 | | await repository.SoftDeleteAsync([.. missing.Concat(videosLeftBehind).Select(row => row.Id)], ct), null); |
| | 14 | 65 | | } |
| | | 66 | | |
| | | 67 | | private static partial class Log |
| | | 68 | | { |
| | | 69 | | [LoggerMessage(Level = LogLevel.Debug, Message = "Soft-deleting asset {AssetId} at {FilePath}: a complete crawl |
| | | 70 | | public static partial void SoftDeleting(ILogger logger, Guid assetId, string filePath); |
| | | 71 | | |
| | | 72 | | [LoggerMessage( |
| | | 73 | | Level = LogLevel.Debug, |
| | | 74 | | Message = "Soft-deleting asset {AssetId} at {FilePath}: it is the video of a Live Photo whose photo is missi |
| | | 75 | | public static partial void SoftDeletingLeftBehindVideo(ILogger logger, Guid assetId, string filePath); |
| | | 76 | | |
| | | 77 | | [LoggerMessage( |
| | | 78 | | Level = LogLevel.Information, |
| | | 79 | | Message = "Asset {PhotoId} is now a still photo: its video, asset {VideoId} at {FilePath}, is missing.")] |
| | | 80 | | public static partial void NowAStillPhoto(ILogger logger, Guid photoId, Guid videoId, string filePath); |
| | | 81 | | } |
| | | 82 | | } |