Files
Chronos/src/Titano.App/Import/ServerImportSink.cs
T
Claude f7affe7840 feat(app): import and archive manager logic (plans, card pool, card gate, server sink, tree, charts, reports)
Plain classes behind the Import and Archive screens: plan totals, session
rows and the Show plan listing; duplicate and title choices without a new
scan; folder and server destinations with the protocol client's chunked,
hash-verified uploads; the after-import gate that allows deleting or
formatting a card only after a verified archive import and a dry run; the
archive tree, chart aggregation, mass rename/retag and maintenance reports.

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

52 lines
2.6 KiB
C#

using System.IO;
using Titano.Core.Import;
using Titano.Protocol;
using Titano.Protocol.Client;
namespace Titano.App.Import;
/// <summary>
/// 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.
/// </summary>
public sealed class ServerImportSink(TitanoClient client, string stagingDirectory) : IImportSink
{
private readonly HashSet<Guid> _titled = [];
public string StagingDirectory { get; } = stagingDirectory;
public async Task<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
{
var results = new List<SinkCommit>(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;
}
}