- a card that leaves needs a new check; formatting re-lists the card and refuses one that changed since its check; files that are not photos are named in the format confirmation - an import never stays stuck in Running; Retry never resumes an older job - server imports list refused files and keep what was uploaded when stopped - the archive result matches each copy to its job's record and shows the job's own error per file - refreshing devices keeps typed titles and choices; titles lock while copying - three destination names, merge and split of sessions, camera profiles shared by the cards of a camera, photo counts and a new-card toast - disabled controls say why; keyboard focus follows the nickname field and Show plan (Esc closes it); label column fits longer languages - capture states for insertion, running, failed and a new card Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
190 lines
11 KiB
C#
190 lines
11 KiB
C#
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Cryptography;
|
|
using Titano.App.Import;
|
|
using Titano.Core.Import;
|
|
using Titano.Protocol;
|
|
using Titano.Protocol.Client;
|
|
using Titano.Testing;
|
|
using static Titano.App.Tests.Import.ImportTestData;
|
|
|
|
namespace Titano.App.Tests.Import;
|
|
|
|
/// <summary>The "Server" destination over the real protocol client against an in-memory upload endpoint.</summary>
|
|
[Category("import")]
|
|
public sealed class ServerImportSinkTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task EveryPhotoIsUploadedInChunksUnderItsCardNameAndVerifiedByHash()
|
|
{
|
|
var card = Card(context.CreateTempDirectory("card"), 8, Start, Start.AddHours(2));
|
|
var staging = context.CreateTempDirectory("staging");
|
|
var server = new FakeUploadServer(chunkSize: 256);
|
|
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
|
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
|
plan = plan with { Sessions = [plan.Sessions[0] with { Title = "Harbour sunset" }, .. plan.Sessions.Skip(1)] };
|
|
|
|
var outcome = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
|
|
|
|
Assert.Equal(16, outcome.Copied);
|
|
Assert.True(outcome.IsComplete);
|
|
Assert.Equal(16, server.Committed.Count);
|
|
foreach (var item in plan.Items)
|
|
{
|
|
var bytes = File.ReadAllBytes(Path.Combine(card, item.SourcePath));
|
|
var upload = server.Committed.Single(u => u.FileName == Path.GetFileName(item.SourcePath));
|
|
Assert.BytesEqual(bytes, upload.Bytes);
|
|
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(bytes)), upload.Sha256);
|
|
Assert.True(upload.Chunks > 1, "files larger than a chunk travel in several chunks");
|
|
}
|
|
Assert.SequenceEqual(plan.Sessions.Select(s => s.SessionKey).Order(StringComparer.Ordinal),
|
|
server.Committed.Select(u => u.SessionKey).Distinct().Order(StringComparer.Ordinal));
|
|
Assert.Equal("Harbour sunset", server.Titles.Single().Value);
|
|
Assert.Empty(Directory.EnumerateFileSystemEntries(staging));
|
|
}
|
|
|
|
[Test]
|
|
public async Task PhotosTheServerAlreadyHoldsAreNotSentAgain()
|
|
{
|
|
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
|
var staging = context.CreateTempDirectory("staging");
|
|
var server = new FakeUploadServer(chunkSize: 4096);
|
|
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
|
var destination = new UnindexedDestination(ImportDestinationKind.Server, staging);
|
|
var plan = await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
|
await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
|
|
var chunks = server.ChunkCount;
|
|
|
|
var again = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
|
|
Assert.Equal(0, again.Copied);
|
|
Assert.Equal(8, again.Duplicates);
|
|
Assert.Equal(chunks, server.ChunkCount);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AFileTheServerRefusesIsListedAndTheImportGoesOn()
|
|
{
|
|
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
|
var staging = context.CreateTempDirectory("staging");
|
|
var server = new FakeUploadServer(chunkSize: 4096) { CorruptFile = "IMG_0003.JPG" };
|
|
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
|
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
|
var sink = new ServerImportSink(client, staging);
|
|
|
|
var outcome = await LocalImport.RunAsync(plan, sink, true, null, context.Cancellation, sink.Report);
|
|
Assert.Equal(7, outcome.Copied);
|
|
Assert.Equal(7, server.Committed.Count);
|
|
Assert.Equal(new ImportFailure("DCIM/100CANON/IMG_0003.JPG", "Hash mismatch"), outcome.Failures.Single());
|
|
Assert.Empty(Directory.EnumerateFileSystemEntries(staging));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ALostSignInStopsTheImportAndKeepsWhatWasUploaded()
|
|
{
|
|
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
|
var staging = context.CreateTempDirectory("staging");
|
|
var server = new FakeUploadServer(chunkSize: 4096) { SignedOutAfter = 3 };
|
|
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
|
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
|
var sink = new ServerImportSink(client, staging);
|
|
|
|
var outcome = await LocalImport.RunAsync(plan, sink, true, null, context.Cancellation, sink.Report);
|
|
Assert.Equal(3, outcome.Copied, "the uploads the server committed before the sign-in was lost");
|
|
Assert.Equal(5, outcome.Failures.Count);
|
|
Assert.Equal("Sign in again", outcome.Failures[0].Message, "the server's sentence");
|
|
Assert.False(outcome.IsComplete);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheQuotaIsTheRoomOfAServerImport()
|
|
{
|
|
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
|
var staging = context.CreateTempDirectory("staging");
|
|
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging) { ServerSpace = 1_000 }
|
|
.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
|
Assert.Equal(1_000L, plan.AvailableBytes);
|
|
Assert.False(ImportPlans.Totals(plan, verify: true).HasEnoughSpace, "eight photos do not fit in 1 KB of quota");
|
|
|
|
var unlimited = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null,
|
|
context.Cancellation);
|
|
Assert.Null(unlimited.AvailableBytes);
|
|
Assert.True(ImportPlans.Totals(unlimited, verify: true).HasEnoughSpace);
|
|
}
|
|
|
|
/// <summary>Start, chunk and complete of BRIEF §14.4, plus the title update; the hash is checked at completion.</summary>
|
|
private sealed class FakeUploadServer(int chunkSize) : HttpMessageHandler
|
|
{
|
|
private readonly Dictionary<Guid, Upload> _open = [];
|
|
private readonly Dictionary<string, Guid> _sequences = new(StringComparer.Ordinal);
|
|
private readonly Dictionary<string, Guid> _frames = new(StringComparer.Ordinal);
|
|
|
|
public List<Upload> Committed { get; } = [];
|
|
public Dictionary<Guid, string> Titles { get; } = [];
|
|
public int ChunkCount { get; private set; }
|
|
|
|
/// <summary>Flip a byte of every chunk of this file (a damaged transfer).</summary>
|
|
public string? CorruptFile { get; init; }
|
|
|
|
/// <summary>Answer 401 to new uploads once this many were committed (the sign-in expired).</summary>
|
|
public int? SignedOutAfter { get; init; }
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
var path = request.RequestUri!.AbsolutePath.Trim('/').Split('/');
|
|
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads"])
|
|
{
|
|
var start = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.StartUploadRequest, cancellationToken))!;
|
|
if (Committed.Count >= SignedOutAfter)
|
|
return Json(new ApiError("unauthorized", "Sign in again"), ProtocolJson.Default.ApiError, HttpStatusCode.Unauthorized);
|
|
if (_frames.TryGetValue(start.Sha256, out var existing))
|
|
return Json(new StartUploadResponse(null, chunkSize, [], true, existing), ProtocolJson.Default.StartUploadResponse);
|
|
var upload = new Upload(Guid.NewGuid(), start.FileName, start.Size, start.Sha256, start.SessionKey ?? "user");
|
|
_open[upload.Id] = upload;
|
|
return Json(new StartUploadResponse(upload.Id, chunkSize, [], false, null), ProtocolJson.Default.StartUploadResponse);
|
|
}
|
|
if (request.Method == HttpMethod.Put && path is ["api", "v1", "uploads", var id, "chunks", var n])
|
|
{
|
|
var upload = _open[Guid.Parse(id)];
|
|
var bytes = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
|
|
if (upload.FileName == CorruptFile) bytes[0] ^= 0xFF;
|
|
upload.Parts[int.Parse(n, System.Globalization.CultureInfo.InvariantCulture)] = bytes;
|
|
ChunkCount++;
|
|
return Json(new ChunkReceived(int.Parse(n, System.Globalization.CultureInfo.InvariantCulture), [.. upload.Parts.Keys]), ProtocolJson.Default.ChunkReceived);
|
|
}
|
|
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads", var done, "complete"])
|
|
{
|
|
var upload = _open[Guid.Parse(done)];
|
|
if (Convert.ToHexStringLower(SHA256.HashData(upload.Bytes)) != upload.Sha256)
|
|
return Json(new ApiError("hash_mismatch", "Hash mismatch"), ProtocolJson.Default.ApiError, HttpStatusCode.UnprocessableEntity);
|
|
_open.Remove(upload.Id);
|
|
Committed.Add(upload);
|
|
var frame = Guid.NewGuid();
|
|
_frames[upload.Sha256] = frame;
|
|
if (!_sequences.TryGetValue(upload.SessionKey, out var sequence)) _sequences[upload.SessionKey] = sequence = Guid.NewGuid();
|
|
return Json(new CompleteUploadResponse(frame, sequence, Committed.Count(u => u.SessionKey == upload.SessionKey) - 1),
|
|
ProtocolJson.Default.CompleteUploadResponse);
|
|
}
|
|
if (request.Method == HttpMethod.Patch && path is ["api", "v1", "sequences", var sequenceId])
|
|
{
|
|
var update = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.UpdateSequenceRequest, cancellationToken))!;
|
|
Titles[Guid.Parse(sequenceId)] = update.Title!;
|
|
return Json(new SequenceDto(Guid.Parse(sequenceId), update.Title, update.Title!, "f", null, null, null, null, null, 0, null, 0, 0, 0, null, [], 0, null,
|
|
1, 1, 0, null, DateTimeOffset.UtcNow), ProtocolJson.Default.SequenceDto);
|
|
}
|
|
return new HttpResponseMessage(HttpStatusCode.NotFound);
|
|
}
|
|
|
|
private static HttpResponseMessage Json<T>(T value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> type, HttpStatusCode status = HttpStatusCode.OK) =>
|
|
new(status) { Content = JsonContent.Create(value, type) };
|
|
|
|
public sealed record Upload(Guid Id, string FileName, long Size, string Sha256, string SessionKey)
|
|
{
|
|
public SortedDictionary<int, byte[]> Parts { get; } = [];
|
|
public byte[] Bytes => [.. Parts.Values.SelectMany(p => p)];
|
|
public int Chunks => Parts.Count;
|
|
}
|
|
}
|
|
}
|