Files
Chronos/src/Titano.Core/Import/FolderImportSink.cs
T
Claude 8cb9672520 feat(core): card import planning and verified copy engine
Card scanning (DCIM and flat), session splitting with test-shot merging and
user splits/merges, per-body import plans named by templates with archive
duplicate detection, hash-verified resumable copy with per-file errors, a
plain-folder destination and a verified card erase with dry run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:30:14 +00:00

43 lines
2.0 KiB
C#

using Titano.Core.Archiving;
namespace Titano.Core.Import;
/// <summary>
/// Import destination "Folder" (BRIEF §12.4): photos go to <c>root/&lt;folder&gt;/&lt;name&gt;</c> 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.
/// </summary>
public sealed class FolderImportSink(string root) : IImportSink
{
public string StagingDirectory { get; } = Path.Combine(Path.GetFullPath(root), ArchiveLayout.MetaFolder, ArchiveLayout.StagingFolder);
public async Task<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
{
var folder = Path.Combine(Path.GetFullPath(root), session.Folder.Replace('/', Path.DirectorySeparatorChar));
Directory.CreateDirectory(folder);
var results = new List<SinkCommit>(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<bool> IsSameAsync(string existing, StagedFile file, CancellationToken cancellationToken) =>
new FileInfo(existing).Length == file.Item.Size
&& await ContentHash.Sha256Async(existing, null, cancellationToken).ConfigureAwait(false) == file.Sha256;
}