| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using System.IO.Abstractions; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Ingestion; |
| | | 5 | | |
| | | 6 | | internal sealed record LivePhotoLinkResult( |
| | | 7 | | IReadOnlyList<IngestionItem> Items, |
| | | 8 | | IReadOnlySet<string> ClaimedPaths); |
| | | 9 | | |
| | | 10 | | internal interface ILivePhotoLinker |
| | | 11 | | { |
| | | 12 | | LivePhotoLinkResult Link(IEnumerable<string> filesInDirectory, string rootPath); |
| | | 13 | | } |
| | | 14 | | |
| | 25 | 15 | | internal sealed class LivePhotoLinker(IFileSystem fileSystem) : ILivePhotoLinker |
| | | 16 | | { |
| | | 17 | | public LivePhotoLinkResult Link(IEnumerable<string> filesInDirectory, string rootPath) |
| | 24 | 18 | | { |
| | 24 | 19 | | var heicFiles = new List<string>(); |
| | 24 | 20 | | var movFilesByBaseName = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | | 21 | | |
| | 146 | 22 | | foreach (var path in filesInDirectory) |
| | 37 | 23 | | { |
| | 37 | 24 | | var ext = fileSystem.Path.GetExtension(path); |
| | 37 | 25 | | if (ext.Equals(MediaFileExtensions.Heic, StringComparison.OrdinalIgnoreCase)) |
| | 13 | 26 | | heicFiles.Add(path); |
| | 24 | 27 | | else if (ext.Equals(MediaFileExtensions.Mov, StringComparison.OrdinalIgnoreCase)) |
| | 13 | 28 | | movFilesByBaseName[fileSystem.Path.GetFileNameWithoutExtension(path)] = path; |
| | 37 | 29 | | } |
| | | 30 | | |
| | 24 | 31 | | var items = new List<IngestionItem>(heicFiles.Count); |
| | 24 | 32 | | var claimedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 33 | | |
| | 98 | 34 | | foreach (var heicPath in heicFiles) |
| | 13 | 35 | | { |
| | 13 | 36 | | claimedPaths.Add(heicPath); |
| | 13 | 37 | | var baseName = fileSystem.Path.GetFileNameWithoutExtension(heicPath); |
| | 13 | 38 | | var heicRelativePath = fileSystem.Path.GetRelativePath(rootPath, heicPath); |
| | | 39 | | |
| | 13 | 40 | | if (movFilesByBaseName.TryGetValue(baseName, out var movPath)) |
| | 9 | 41 | | { |
| | 9 | 42 | | var movRelativePath = fileSystem.Path.GetRelativePath(rootPath, movPath); |
| | 9 | 43 | | items.Add(new LivePhotoPairItem(heicPath, heicRelativePath, movPath, movRelativePath)); |
| | 9 | 44 | | claimedPaths.Add(movPath); |
| | 9 | 45 | | } |
| | | 46 | | else |
| | 4 | 47 | | { |
| | 4 | 48 | | items.Add(new SingleFileItem(heicPath, heicRelativePath, MediaType.Image)); |
| | 4 | 49 | | } |
| | 13 | 50 | | } |
| | | 51 | | |
| | 24 | 52 | | return new LivePhotoLinkResult(items, claimedPaths); |
| | 24 | 53 | | } |
| | | 54 | | } |