< Summary

Information
Class: Anichron.Worker.Ingestion.Classification.IngestionClassifier
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Classification/IngestionClassifier.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 69
Uncovered lines: 0
Coverable lines: 69
Total lines: 110
Line coverage: 100%
Branch coverage
100%
Covered branches: 74
Total branches: 74
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ClassifyAsync()100%1010100%
ClassifyVideo(...)100%1212100%
AtPath(...)100%11100%
LoneLivePhotoVideo(...)100%88100%
Classify(...)100%1414100%
ClassifyUnstoredContent(...)100%1010100%
PairingFor(...)100%2020100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Classification/IngestionClassifier.cs

#LineLine coverage
 1using Anichron.Core.Data.Repository;
 2using Anichron.Core.Domain;
 3using Anichron.Worker.Ingestion.Pipeline;
 4using System.IO.Abstractions;
 5
 6namespace Anichron.Worker.Ingestion.Classification;
 7
 1148internal sealed class IngestionClassifier(IMediaAssetRepository repository, IFileSystem fileSystem)
 9{
 10    public async Task<IngestionClassification> ClassifyAsync(IngestionContext context, CancellationToken ct)
 11211    {
 11212        var (item, contentHash, secondaryHash) = (context.Item, context.ContentHash!, context.SecondaryHash!);
 11213        var mov = item.SecondaryFile;
 11214        string[] hashes = mov is null ? [contentHash] : [contentHash, secondaryHash];
 11215        string[] paths = mov is null ? [item.RelativePath] : [item.RelativePath, mov.RelativePath];
 16
 11217        var rows = await repository.FindByHashesOrPathsAsync(context.Config.Id, hashes, paths, ct);
 18
 11219        var rootPath = context.Config.RootPath;
 11220        var primary = (mov is null ? LoneLivePhotoVideo(rows, item.RelativePath, ownVideoIds: []) : null)
 11221            ?? Classify(rows, contentHash, item.RelativePath, rootPath, AtPath(rows, item.RelativePath));
 11222        var secondary = mov is null ? null : ClassifyVideo(rows, primary, secondaryHash, mov.RelativePath, rootPath);
 11223        return new(primary, secondary, PairingFor(primary, secondary));
 11224    }
 25
 26    private FileClassification ClassifyVideo(
 27        IReadOnlyList<MediaAsset> rows,
 28        FileClassification photo,
 29        string contentHash,
 30        string relativePath,
 31        string rootPath)
 6832    {
 6833        var paired = rows.FirstOrDefault(row => row.Id == photo.Row?.PairedAssetId);
 6834        if (paired is not null)
 3235        {
 3236            var holder = rows.FirstOrDefault(row => row.FilePath == relativePath && row != paired);
 3237            return Classify(rows, contentHash, relativePath, rootPath, holder ?? paired);
 38        }
 39
 3640        var movedPhoto = photo.Outcome == FileOutcome.OverwrittenByMove ? photo.Retired : null;
 3641        return LoneLivePhotoVideo(rows, relativePath, [photo.Row?.PairedAssetId, movedPhoto?.PairedAssetId])
 3642            ?? Classify(rows, contentHash, relativePath, rootPath, AtPath(rows, relativePath));
 6843    }
 44
 45    private static MediaAsset? AtPath(IReadOnlyList<MediaAsset> rows, string relativePath)
 14846        => rows.FirstOrDefault(row => row.FilePath == relativePath);
 47
 48    private static FileClassification? LoneLivePhotoVideo(
 49        IReadOnlyList<MediaAsset> rows, string relativePath, Guid?[] ownVideoIds)
 8050    {
 8051        if (rows.FirstOrDefault(row => row.IsLivePhotoVideo && !ownVideoIds.Contains(row.Id)) is not { } video)
 6552            return null;
 53
 1554        var atPath = AtPath(rows, relativePath);
 1555        return new(
 1556            FileOutcome.LoneLivePhotoVideo,
 1557            video,
 1558            Retired: atPath is { IsLivePhotoVideo: false, IsSoftDeleted: false } ? atPath : null);
 8059    }
 60
 61    private FileClassification Classify(
 62        IReadOnlyList<MediaAsset> rows, string contentHash, string relativePath, string rootPath, MediaAsset? atPath)
 16563    {
 16564        var stored = rows.FirstOrDefault(row => !row.IsSoftDeleted && row.ContentHash == contentHash);
 16565        if (stored is null)
 5166            return ClassifyUnstoredContent(rows, contentHash, atPath);
 11467        if (stored == atPath)
 5568            return new(stored.FilePath == relativePath ? FileOutcome.Unchanged : FileOutcome.Moved, stored);
 5969        if (fileSystem.File.Exists(fileSystem.Path.Combine(rootPath, stored.FilePath)))
 2270        {
 2271            return atPath is { IsSoftDeleted: false }
 2272                ? new(FileOutcome.OverwrittenByCopy, stored, Retired: atPath)
 2273                : new(FileOutcome.DuplicateCopy, stored);
 74        }
 75
 3776        return atPath is null ? new(FileOutcome.Moved, stored) : new(FileOutcome.OverwrittenByMove, atPath, Retired: sto
 16577    }
 78
 79    private static FileClassification ClassifyUnstoredContent(
 80        IReadOnlyList<MediaAsset> rows, string contentHash, MediaAsset? atPath)
 5181    {
 5182        if (atPath is { IsSoftDeleted: false })
 1483            return new(FileOutcome.ContentChanged, atPath);
 3784        if (atPath is not null)
 1285        {
 1286            return atPath.ContentHash == contentHash
 1287                ? new(FileOutcome.AutoRestored, atPath)
 1288                : new(FileOutcome.AutoRestoredContentChanged, atPath);
 89        }
 90
 2591        var softDeleted = rows
 2592            .Where(row => row.ContentHash == contentHash)
 2593            .MaxBy(row => row.LastSeenOnNas);
 2594        return softDeleted is null ? new(FileOutcome.New, null) : new(FileOutcome.AutoRestoredMoved, softDeleted);
 5195    }
 96
 97    private static PairingAction PairingFor(FileClassification photo, FileClassification? video)
 11298    {
 11299        if (photo.Row is not { } photoRow
 112100            || photo.Outcome is FileOutcome.ContentChanged or FileOutcome.AutoRestoredContentChanged
 112101            || video is not { DefersToAnotherRow: false })
 67102        {
 67103            return PairingAction.None;
 104        }
 105
 45106        if (video.Row is not { } videoRow || photoRow.PairedAssetId != videoRow.Id)
 23107            return video.TakesContent ? PairingAction.PointAtMov : PairingAction.AttachMov;
 22108        return video.TakesContent ? PairingAction.None : PairingAction.BackfillMovProxy;
 112109    }
 110}