using Chronos.Core.Archiving; namespace Chronos.Core.Import; /// /// Import destination "Folder" (BRIEF ยง12.4): photos go to root/<folder>/<name> without sidecars or index. /// A name already taken by identical content is a duplicate; by other content, the photo gets a suffix. Nothing at the /// destination is ever replaced. /// public sealed class FolderImportSink(string root) : IImportSink { public string StagingDirectory { get; } = Path.Combine(Path.GetFullPath(root), ArchiveLayout.MetaFolder, ArchiveLayout.StagingFolder); public async Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken) { var folder = Path.Combine(Path.GetFullPath(root), session.Folder.Replace('/', Path.DirectorySeparatorChar)); Directory.CreateDirectory(folder); var results = new List(files.Count); foreach (var file in files) { var name = file.Item.FileName; var stem = Path.GetFileNameWithoutExtension(name); var duplicate = false; for (var n = 2; File.Exists(Path.Combine(folder, name)); n++) { if (await IsSameAsync(Path.Combine(folder, name), file, cancellationToken).ConfigureAwait(false)) { duplicate = true; break; } name = $"{stem}_{n}{Path.GetExtension(file.Item.FileName)}"; } if (!duplicate) File.Move(file.StagedPath, Path.Combine(folder, name), overwrite: false); results.Add(new SinkCommit(null, session.Folder + "/" + name, duplicate)); } return results; } private static async Task IsSameAsync(string existing, StagedFile file, CancellationToken cancellationToken) => new FileInfo(existing).Length == file.Item.Size && await ContentHash.Sha256Async(existing, null, cancellationToken).ConfigureAwait(false) == file.Sha256; }