using System.IO; using Titano.Core.Import; using Titano.Protocol; using Titano.Protocol.Client; namespace Titano.App.Import; /// /// Import destination "Server" (BRIEF §12.4, §14.4): each verified copy staged on this PC is uploaded with the /// protocol's resumable chunks and the hash computed while copying; the server checks that hash before it commits the /// frame, and only then does the photo count as imported. Photos of one plan session share its session key, so the /// server files them in one sequence — the same one when an interrupted import is started again. A typed session /// title is set on that sequence once. /// public sealed class ServerImportSink(TitanoClient client, string stagingDirectory) : IImportSink { private readonly HashSet _titled = []; public string StagingDirectory { get; } = stagingDirectory; public async Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken) { var results = new List(files.Count); foreach (var file in files) { // The server records the uploaded file name as the original name: upload the card's name, not the staging name. var folder = Path.Combine(StagingDirectory, Path.GetFileNameWithoutExtension(file.StagedPath)); var named = Path.Combine(folder, Path.GetFileName(file.Item.SourcePath)); Directory.CreateDirectory(folder); File.Move(file.StagedPath, named); try { var committed = await client.UploadFileAsync(named, null, file.Item.CapturedAt, session.SessionKey, file.Sha256, null, cancellationToken) .ConfigureAwait(false); // An upload the server already held reports no position in a sequence. var duplicate = committed is null || committed.Index < 0; results.Add(new SinkCommit(committed?.FrameId, file.Item.FileName, duplicate)); if (committed is { Index: >= 0 } && session.Title is { Length: > 0 } title && _titled.Add(committed.SequenceId)) await client.UpdateSequenceAsync(committed.SequenceId, new UpdateSequenceRequest(title, null, null, null, null, null), cancellationToken) .ConfigureAwait(false); } finally { // Our own staged copy; the original stays on the card. File.Delete(named); Directory.Delete(folder); } } return results; } }