Second fix round of owner review 2, phase 3 (engine, server, desktop). - A photo never hashed whose original is missing can be deleted again: it goes into its trash record without a file and without a tombstone (nothing is left to name), stays listed until Empty erases it, and a restore keeps it with the reason "no file". FrameDeleteBlock.Unhashable now blocks only a present original that cannot be read; the API answers it as 409 photo_unreadable naming the photo, and every block has its own code and message. - Deleting a whole sequence first settles an interrupted photo deletion, restore or ingest of it (ReadLocked under its lease), so no journal or half entry travels into the sequence's trash entry; it refuses while a restore into it waits for its busy entry. - FrameTrash.Settle takes the photo entry's lease and leaves the journal for later while the entry is busy (an emptying), so it never races EraseAsync; meanwhile no other restore writes its journal over it and scans do not adopt its files. - The hashes computed for frames that stay (to find a twin of a deleted photo) go into the sidecar and index, and only a frame whose first and last 64 KiB match a deleted photo's is read whole. - A restore into a new sequence rewrites its record after each photo filed, so a crash or a database failure never leaves filed photos listed. - An upload completion no longer has a window before the commit: the ingest checks the sidecar's tombstones under the sequence lease a deletion takes (IngestRequest.RefuseRemoved), unless the upload asked to bring it back. - A restore reports each kept photo with its reason (no longer matches its record, could not be moved back, already in the sequence, had no file); the desktop line and the API message say so in en and it, and the tooltip holds the whole sentence. The desktop tells TrashMismatchException by its type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 1858f1f450a35fb8383ede8d80ce5f8de9b7ceac)
491 lines
30 KiB
C#
491 lines
30 KiB
C#
using System.Globalization;
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Chronos.Core.Codecs.Jpeg;
|
|
using Chronos.Core.Localization;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Storage;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Server.Tests.Storage;
|
|
|
|
/// <summary>
|
|
/// The admin panel's upload dialog (owner review 2, R3; <c>Admin/web/upload.js</c>) speaks the upload API through the
|
|
/// cookie session: requests shaped like <c>api.js</c> sends them (JSON bodies written as the page writes them, the
|
|
/// page's language, the CSRF header on every unsafe call, chunks as octet-stream), against the real archive engine.
|
|
/// </summary>
|
|
[Category("server")]
|
|
public sealed class BrowserUploadTests(TestContext context)
|
|
{
|
|
private const int Chunk = 64 * 1024;
|
|
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(30));
|
|
|
|
[Test]
|
|
public async Task TheUploadDialogFilesPhotosThroughTheCookieSession()
|
|
{
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
|
|
// What the dialog offers and checks before it hashes anything.
|
|
ServerInfo info;
|
|
using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/server/info"))
|
|
info = await response.ReadAsync(ProtocolJson.Default.ServerInfo);
|
|
var extensions = Assert.NotNull(info.Extensions);
|
|
foreach (var extension in new[] { "jpg", "jpeg", "tif", "dng", "cr3", "nef", "heic" }) Assert.Contains(extension, extensions);
|
|
Assert.True(extensions.All(e => e.Length > 0 && e == e.ToLowerInvariant() && !e.StartsWith('.')), string.Join(",", extensions));
|
|
|
|
// New sequence: the title as typed, no camera hint.
|
|
SequenceDto sequence;
|
|
using (var created = await browser.SendAsync(HttpMethod.Post, "/api/v1/sequences", Json("""{"title":"Roof","cameraHint":null}""")))
|
|
{
|
|
await created.ExpectAsync(HttpStatusCode.Created);
|
|
sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto);
|
|
}
|
|
|
|
var photos = new[] { Photo(1), Photo(2) };
|
|
var frames = new List<CompleteUploadResponse>();
|
|
for (var i = 0; i < photos.Length; i++)
|
|
{
|
|
var photo = photos[i];
|
|
var start = await StartAsync(browser, sequence.Id, $"IMG_{i + 1:0000}.JPG", photo, HttpStatusCode.Created);
|
|
var id = Assert.NotNull(start.UploadId);
|
|
Assert.Equal(Chunk, start.ChunkSize);
|
|
var count = (photo.Length + Chunk - 1) / Chunk;
|
|
Assert.True(count >= 2, $"{photo.Length} bytes make one chunk");
|
|
|
|
// putBytes sends the session's headers: a chunk without the CSRF header is refused like any other change.
|
|
using (var refused = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/0", Bytes(photo, 0), csrf: false))
|
|
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed");
|
|
|
|
// Two chunks in flight, finishing in any order.
|
|
for (var last = count - 1; last >= 0; last -= 2)
|
|
{
|
|
var pair = new[] { last, last - 1 }.Where(n => n >= 0).Select(async n =>
|
|
{
|
|
using var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/{n}", Bytes(photo, n));
|
|
return await put.ReadAsync(ProtocolJson.Default.ChunkReceived);
|
|
});
|
|
foreach (var received in await Task.WhenAll(pair)) Assert.Contains(received.Index, received.Received);
|
|
}
|
|
using var complete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/uploads/{id}/complete", Json("{}"));
|
|
var frame = await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
|
|
Assert.Equal(sequence.Id, frame.SequenceId);
|
|
frames.Add(frame);
|
|
}
|
|
var stored = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
|
|
Assert.SequenceEqual(frames.Select(f => f.FrameId).Order(), stored.Select(f => f.Id).Order());
|
|
for (var i = 0; i < photos.Length; i++)
|
|
{
|
|
var file = Path.Combine(server.Options.ArchivePath, stored.Single(f => f.Id == frames[i].FrameId).RelativePath);
|
|
await Http.BytesEqualAsync(photos[i], File.ReadAllBytesAsync(file));
|
|
}
|
|
|
|
// Picked again: already there, with its frame.
|
|
var again = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photos[0], HttpStatusCode.OK);
|
|
Assert.True(again.AlreadyExists);
|
|
Assert.Equal(frames[0].FrameId, again.FrameId);
|
|
|
|
// Deleted from the sequence: also answered as there, without a frame ("deleted before": restore it from the trash, or
|
|
// upload it again).
|
|
await DeleteAsync(server, browser, sequence.Id, frames[1].FrameId);
|
|
var deleted = await StartAsync(browser, sequence.Id, "IMG_0002.JPG", photos[1], HttpStatusCode.OK);
|
|
Assert.True(deleted.AlreadyExists);
|
|
Assert.Null(deleted.UploadId);
|
|
Assert.Null(deleted.FrameId);
|
|
|
|
// Cancel: an upload the dialog started is discarded on the server.
|
|
var cancelled = Photo(3);
|
|
var started = await StartAsync(browser, sequence.Id, "IMG_0003.JPG", cancelled, HttpStatusCode.Created);
|
|
var cancelledId = Assert.NotNull(started.UploadId);
|
|
using (var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/0", Bytes(cancelled, 0)))
|
|
await put.ExpectAsync(HttpStatusCode.OK);
|
|
using (var discard = await browser.SendAsync(HttpMethod.Delete, $"/api/v1/uploads/{cancelledId}"))
|
|
await discard.ExpectAsync(HttpStatusCode.NoContent);
|
|
using (var state = await browser.SendAsync(HttpMethod.Get, $"/api/v1/uploads/{cancelledId}"))
|
|
Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
|
|
using (var late = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{cancelledId}/chunks/1", Bytes(cancelled, 1)))
|
|
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
|
|
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(cancelledId), "the partial file is gone");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AQuotaRefusalCarriesTheMessageTheDialogShows()
|
|
{
|
|
// A quota or space refusal stops the dialog's run with the server's message, in the page's language.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk);
|
|
var browser = await Browser.SignInAsync(server);
|
|
Guid me;
|
|
using (var response = await browser.SendAsync(HttpMethod.Get, "/api/v1/auth/me")) me = (await response.ReadAsync(ProtocolJson.Default.UserDto)).Id;
|
|
using (var quota = await browser.SendAsync(HttpMethod.Patch, $"/api/v1/users/{me}", Json("""{"quotaBytes":1000}""")))
|
|
await quota.ExpectAsync(HttpStatusCode.OK);
|
|
using var start = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(null, "IMG_0001.JPG", Uploads.Photo(Chunk + 10, 7)));
|
|
var error = await start.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded");
|
|
Assert.Equal(Loc.Get("Server.Error.QuotaExceeded", CultureInfo.GetCultureInfo("it")), error.Message);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UploadAgainBringsBackAPhotoDeletedBeforeEvenAfterTheTrashIsEmptied()
|
|
{
|
|
// Owner review 2, R6 and R3: a tombstone outlives the trash and keeps sync and old clients from filing the photo
|
|
// again, but the user who picks it in the dialog and chooses Upload again gets it back into the sequence.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Roof");
|
|
var photo = Photo(11);
|
|
var sha = Uploads.Sha(photo);
|
|
// A sequence keeps one photo at least: the other is the one deleted.
|
|
var keeper = await UploadAsync(browser, sequence.Id, "IMG_0000.JPG", Photo(10));
|
|
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
|
|
await DeleteAsync(server, browser, sequence.Id, first.FrameId);
|
|
await EmptyTrashAsync(server, browser);
|
|
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone outlives the trash");
|
|
|
|
// Picked again: deleted before, nothing started.
|
|
var refused = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
|
|
Assert.True(refused is { AlreadyExists: true, UploadId: null, FrameId: null }, "answered as deleted before");
|
|
|
|
// Upload again whose completion does not repeat the restore (an older page): refused at the end, the upload dropped.
|
|
var started = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.Created, restore: true);
|
|
var dropped = Assert.NotNull(started.UploadId);
|
|
await SendChunksAsync(browser, dropped, photo);
|
|
using (var complete = await CompleteAsync(browser, dropped))
|
|
{
|
|
var error = await complete.ExpectErrorAsync(HttpStatusCode.Conflict, UploadErrors.PhotoRemoved);
|
|
Assert.Equal(Loc.Get("Server.Error.PhotoRemoved", CultureInfo.GetCultureInfo("it")), error.Message);
|
|
}
|
|
using (var state = await browser.SendAsync(HttpMethod.Get, $"/api/v1/uploads/{dropped}"))
|
|
Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State);
|
|
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(dropped), "the partial file is gone");
|
|
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "still deleted");
|
|
|
|
// Upload again, start and completion: filed into the sequence, and its tombstone is gone.
|
|
var back = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo, restore: true);
|
|
Assert.Equal(sequence.Id, back.SequenceId);
|
|
var frames = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
|
|
Assert.SequenceEqual([keeper.FrameId, back.FrameId], frames.Select(f => f.Id).Order());
|
|
Assert.Equal(sha, frames.Single(f => f.Id == back.FrameId).Sha256);
|
|
Assert.False(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone went with the upload");
|
|
var again = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
|
|
Assert.Equal(back.FrameId, again.FrameId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task UploadAgainFilesThePhotoIntoItsSequenceWhateverOtherSequencesHold()
|
|
{
|
|
// A copy in another sequence stands for an ordinary upload; one brought back on purpose goes where the user put it.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var roof = await CreateAsync(browser, "Roof");
|
|
var yard = await CreateAsync(browser, "Yard");
|
|
var photo = Photo(12);
|
|
await UploadAsync(browser, roof.Id, "IMG_0000.JPG", Photo(10));
|
|
var first = await UploadAsync(browser, roof.Id, "IMG_0001.JPG", photo);
|
|
await DeleteAsync(server, browser, roof.Id, first.FrameId);
|
|
var elsewhere = await UploadAsync(browser, yard.Id, "IMG_0001.JPG", photo);
|
|
|
|
var deleted = await StartAsync(browser, roof.Id, "IMG_0001.JPG", photo, HttpStatusCode.OK);
|
|
Assert.Null(deleted.FrameId);
|
|
var back = await UploadAsync(browser, roof.Id, "IMG_0001.JPG", photo, restore: true);
|
|
Assert.Equal(roof.Id, back.SequenceId);
|
|
Assert.NotEqual(elsewhere.FrameId, back.FrameId);
|
|
Assert.Count(1, await server.Database.Frames.ListBySequenceAsync(yard.Id));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnUploadCompletedAfterItsPhotoWasDeletedDoesNotBringItBack()
|
|
{
|
|
// Owner review 2, R6: the start checked the tombstones, but the photo can be deleted while its bytes are on the way
|
|
// (here a repair of a copy whose original is gone, deleted from the Library meanwhile).
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Roof");
|
|
var photo = Photo(13);
|
|
var sha = Uploads.Sha(photo);
|
|
var keeper = await UploadAsync(browser, sequence.Id, "IMG_0000.JPG", Photo(10));
|
|
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
|
|
await UntilIdleAsync(server);
|
|
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
|
|
File.Delete(Path.Combine(server.Options.ArchivePath, frame.RelativePath));
|
|
|
|
var repair = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.Created);
|
|
var id = Assert.NotNull(repair.UploadId);
|
|
await SendChunksAsync(browser, id, photo);
|
|
await DeleteAsync(server, browser, sequence.Id, first.FrameId);
|
|
|
|
using (var complete = await CompleteAsync(browser, id))
|
|
await complete.ExpectErrorAsync(HttpStatusCode.Conflict, UploadErrors.PhotoRemoved);
|
|
Assert.SequenceEqual([keeper.FrameId], (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
|
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone stays");
|
|
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id), "the partial file is gone");
|
|
// Completing again changes nothing: the upload is over.
|
|
using (var late = await CompleteAsync(browser, id))
|
|
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
|
|
Assert.SequenceEqual([keeper.FrameId], (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
|
}
|
|
|
|
[Test]
|
|
public async Task APhotoDeletedBetweenTheCompletionsCheckAndItsCommitIsNotFiled()
|
|
{
|
|
// The completion asks the index first; a deletion that lands after that and before the commit is caught by the
|
|
// ingest itself, which asks the sidecar's tombstones under the lease the deletion takes.
|
|
GatedIngest? gate = null;
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, s =>
|
|
{
|
|
ArchiveModule.Register(s);
|
|
s.AddSingleton<IArchiveIngest>(sp => gate = new GatedIngest(sp.GetRequiredService<ArchiveService>().Ingest));
|
|
});
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Roof");
|
|
var photo = Photo(17);
|
|
var sha = Uploads.Sha(photo);
|
|
var keeper = await UploadAsync(browser, sequence.Id, "IMG_0000.JPG", Photo(18));
|
|
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
|
|
await UntilIdleAsync(server);
|
|
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
|
|
File.Delete(Path.Combine(server.Options.ArchivePath, frame.RelativePath));
|
|
var repair = await StartAsync(browser, sequence.Id, "IMG_0001.JPG", photo, HttpStatusCode.Created);
|
|
var id = Assert.NotNull(repair.UploadId);
|
|
await SendChunksAsync(browser, id, photo);
|
|
Assert.NotNull(gate).BeforeCommit = () => DeleteAsync(server, browser, sequence.Id, first.FrameId);
|
|
|
|
using (var complete = await CompleteAsync(browser, id))
|
|
await complete.ExpectErrorAsync(HttpStatusCode.Conflict, UploadErrors.PhotoRemoved);
|
|
|
|
Assert.SequenceEqual([keeper.FrameId], (await server.Database.Frames.ListBySequenceAsync(sequence.Id)).Select(f => f.Id));
|
|
Assert.True(await server.Database.FrameRemovals.IsRemovedAsync(sequence.Id, sha), "the tombstone stays");
|
|
Assert.False(server.Services.GetRequiredService<UploadStore>().Exists(id), "the partial file is gone");
|
|
Assert.FileMissing(Path.Combine(server.Options.StagingPath, id.ToString("N") + ".jpg"));
|
|
using (var late = await CompleteAsync(browser, id))
|
|
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AStaleTombstoneBesideAFrameOfTheSameContentRefusesNoRepair()
|
|
{
|
|
// A tombstone left beside a frame that still holds the content (the other copy of a duplicate, a file put back by
|
|
// hand) does not stand for a deletion: the frame's original can still be repaired by uploading it.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Roof");
|
|
var photo = Photo(14);
|
|
var sha = Uploads.Sha(photo);
|
|
var first = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
|
|
await UntilIdleAsync(server);
|
|
await server.Database.FrameRemovals.SyncSequenceAsync(sequence.Id, [new FrameRemoval(sequence.Id, sha, null, RemovalReason.Deleted, DateTimeOffset.UtcNow)]);
|
|
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId));
|
|
var original = Path.Combine(server.Options.ArchivePath, frame.RelativePath);
|
|
File.Delete(original);
|
|
|
|
var repaired = await UploadAsync(browser, sequence.Id, "IMG_0001.JPG", photo);
|
|
Assert.Equal(first.FrameId, repaired.FrameId);
|
|
await Http.BytesEqualAsync(photo, File.ReadAllBytesAsync(original));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheDialogsCaptureTimeDatesAPhotoThatRecordsNone()
|
|
{
|
|
// Owner review 2, R3: a photo without Exif (a PNG, a JPEG stripped on export) takes the time the dialog sends, as local
|
|
// wall time, not the moment the server staged it; frames sent out of order still end up in capture order.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Timelapse");
|
|
var later = await UploadAsync(browser, sequence.Id, "frame_0002.jpg", Photo(16), "2025-05-01T06:31:00.000");
|
|
var earlier = await UploadAsync(browser, sequence.Id, "frame_0001.jpg", Photo(15), "2025-05-01T06:30:00");
|
|
|
|
var frames = await server.Database.Frames.ListBySequenceAsync(sequence.Id);
|
|
Assert.SequenceEqual([earlier.FrameId, later.FrameId], frames.OrderBy(f => f.Index).Select(f => f.Id));
|
|
Assert.Equal(new DateTime(2025, 5, 1, 6, 30, 0), frames.Single(f => f.Id == earlier.FrameId).CapturedAt);
|
|
Assert.Equal(new DateTime(2025, 5, 1, 6, 31, 0), frames.Single(f => f.Id == later.FrameId).CapturedAt);
|
|
}
|
|
|
|
[Test]
|
|
public async Task HiddenDotFilesAreRefusedLikeTheScannerLeavesThemOut()
|
|
{
|
|
// The AppleDouble "._" companion a Mac leaves on an exFAT card, and any hidden dot file, is no photo for the scanner:
|
|
// the dialog leaves them out, and the server refuses them from any client.
|
|
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk, ArchiveModule.Register);
|
|
var browser = await Browser.SignInAsync(server);
|
|
var sequence = await CreateAsync(browser, "Card");
|
|
foreach (var name in new[] { "._IMG_0001.JPG", ".IMG_0002.JPG" })
|
|
{
|
|
using var start = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(sequence.Id, name, Uploads.Photo(4096, 21)));
|
|
await start.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_file_name");
|
|
}
|
|
var upload = await server.Client.GetStringAsync("/admin/upload.js");
|
|
Assert.Contains(@"file.name.startsWith(""."")", upload);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheDialogKeepsItselfOpenAndSettlesWhatAnotherCompletionFiles()
|
|
{
|
|
// Owner review 2, R3 (upload.js): Esc is stopped at its keydown during a run, since Chromium lets a dialog's cancel be
|
|
// prevented only once per user activation; a completion refused while another one files the upload (409
|
|
// upload_state) waits for it instead of failing the photo; the same photo picked twice waits for its first copy.
|
|
await using var server = await TestServer.StartAsync(context);
|
|
var upload = await server.Client.GetStringAsync("/admin/upload.js");
|
|
Assert.True(Regex.IsMatch(upload, @"addEventListener\(""keydown"", \(e\) => \{\s*if \(running && e\.key === ""Escape""\) e\.preventDefault\(\);"), "Esc at keydown");
|
|
Assert.Contains(@"err.code === ""upload_state""", upload);
|
|
Assert.Contains(@"api.get(`/api/v1/uploads/${uploadId}`)", upload);
|
|
Assert.Contains("sameContent.get(sha256)", upload);
|
|
Assert.Contains(@"?restore=true", upload);
|
|
Assert.Contains(@"err.code === ""photo_removed""", upload);
|
|
Assert.Contains(@"import { captureTime } from ""./capture-time.js"";", upload);
|
|
var capture = await server.Client.GetStringAsync("/admin/capture-time.js");
|
|
Assert.Contains("export function exifCaptureTime(bytes)", capture);
|
|
}
|
|
|
|
/// <summary>The bootstrap administrator signed in to the panel (a cookie session over HTTPS), calling as <c>api.js</c> does.</summary>
|
|
private sealed class Browser(HttpClient client, string csrfToken)
|
|
{
|
|
public static async Task<Browser> SignInAsync(TestServer server)
|
|
{
|
|
await server.AdminAsync();
|
|
var cookies = new CookieContainer();
|
|
var client = server.NewCookieClient(cookies);
|
|
using var login = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.Password, "admin-web", Cookie: true),
|
|
ProtocolJson.Default.LoginRequest);
|
|
await login.ExpectAsync(HttpStatusCode.OK);
|
|
return new Browser(client, Assert.NotNull(cookies.GetCookies(Assert.NotNull(server.HttpsBase))["chronos_csrf"]).Value);
|
|
}
|
|
|
|
/// <summary>JSON accepted, the page's language (Italian here), and the CSRF header on every call but a GET.</summary>
|
|
public async Task<HttpResponseMessage> SendAsync(HttpMethod method, string url, HttpContent? content = null, bool csrf = true)
|
|
{
|
|
using var request = new HttpRequestMessage(method, url) { Content = content };
|
|
request.Headers.Accept.ParseAdd("application/json");
|
|
request.Headers.AcceptLanguage.ParseAdd("it");
|
|
if (method != HttpMethod.Get && csrf) request.Headers.Add("X-CSRF-Token", csrfToken);
|
|
return await client.SendAsync(request);
|
|
}
|
|
}
|
|
|
|
private static StringContent Json(string json) => new(json, Encoding.UTF8, "application/json");
|
|
|
|
/// <summary>A chunk as <c>putBytes</c> sends a <c>File.slice()</c>: its bytes, octet-stream.</summary>
|
|
private static ByteArrayContent Bytes(byte[] data, int index)
|
|
{
|
|
var content = new ByteArrayContent(Uploads.Chunk(data, Chunk, index).ToArray());
|
|
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
|
return content;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The start body upload.js writes: the capture time capture-time.js read (local wall time without an offset, or null),
|
|
/// no session key, and restore true only after the user chose Upload again on the photo's row.
|
|
/// </summary>
|
|
private static StringContent StartBody(Guid? sequenceId, string fileName, byte[] data, string? capturedAt = null, bool restore = false) =>
|
|
Json($$"""{"sequenceId":{{(sequenceId is { } id ? $"\"{id}\"" : "null")}},"fileName":"{{fileName}}","size":{{data.Length}},"sha256":"{{Uploads.Sha(data)}}","capturedAt":{{(capturedAt is null ? "null" : $"\"{capturedAt}\"")}},"sessionKey":null,"restore":{{(restore ? "true" : "false")}}}""");
|
|
|
|
private static async Task<StartUploadResponse> StartAsync(Browser browser, Guid sequenceId, string fileName, byte[] data, HttpStatusCode expected,
|
|
string? capturedAt = null, bool restore = false)
|
|
{
|
|
using var response = await browser.SendAsync(HttpMethod.Post, "/api/v1/uploads", StartBody(sequenceId, fileName, data, capturedAt, restore));
|
|
await response.ExpectAsync(expected);
|
|
return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse);
|
|
}
|
|
|
|
/// <summary>A sequence made as the dialog's New sequence makes it.</summary>
|
|
private static async Task<SequenceDto> CreateAsync(Browser browser, string title)
|
|
{
|
|
using var created = await browser.SendAsync(HttpMethod.Post, "/api/v1/sequences", Json($$"""{"title":"{{title}}","cameraHint":null}"""));
|
|
await created.ExpectAsync(HttpStatusCode.Created);
|
|
return await created.ReadAsync(ProtocolJson.Default.SequenceDto);
|
|
}
|
|
|
|
/// <summary>Every chunk of an upload the dialog started, one after the other.</summary>
|
|
private static async Task SendChunksAsync(Browser browser, Guid id, byte[] data)
|
|
{
|
|
for (var n = 0; n < (data.Length + Chunk - 1) / Chunk; n++)
|
|
{
|
|
using var put = await browser.SendAsync(HttpMethod.Put, $"/api/v1/uploads/{id}/chunks/{n}", Bytes(data, n));
|
|
await put.ExpectAsync(HttpStatusCode.OK);
|
|
}
|
|
}
|
|
|
|
/// <summary>The dialog's completion: a body of <c>{}</c> (api.post), <c>?restore=true</c> after Upload again.</summary>
|
|
private static Task<HttpResponseMessage> CompleteAsync(Browser browser, Guid id, bool restore = false) =>
|
|
browser.SendAsync(HttpMethod.Post, $"/api/v1/uploads/{id}/complete{(restore ? "?restore=true" : string.Empty)}", Json("{}"));
|
|
|
|
/// <summary>Started, sent and completed as the dialog does it; returns the frame.</summary>
|
|
private static async Task<CompleteUploadResponse> UploadAsync(Browser browser, Guid sequenceId, string fileName, byte[] data, string? capturedAt = null,
|
|
bool restore = false)
|
|
{
|
|
var start = await StartAsync(browser, sequenceId, fileName, data, HttpStatusCode.Created, capturedAt, restore);
|
|
var id = Assert.NotNull(start.UploadId);
|
|
await SendChunksAsync(browser, id, data);
|
|
using var complete = await CompleteAsync(browser, id, restore);
|
|
await complete.ExpectAsync(HttpStatusCode.OK);
|
|
return await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse);
|
|
}
|
|
|
|
/// <summary>Deletes a photo from its sequence as the Library does, once no job is working on it.</summary>
|
|
private static async Task DeleteAsync(TestServer server, Browser browser, Guid sequenceId, Guid frameId)
|
|
{
|
|
await UntilIdleAsync(server);
|
|
var current = Assert.NotNull(await server.Database.Sequences.GetAsync(sequenceId));
|
|
using var delete = await browser.SendAsync(HttpMethod.Post, $"/api/v1/sequences/{sequenceId}/frames/delete",
|
|
Json($$"""{"frameIds":["{{frameId}}"],"expectedFramesVersion":{{current.FramesVersion}},"dryRun":false}"""));
|
|
Assert.NotNull((await delete.ReadAsync(ProtocolJson.Default.FrameDeleteResultDto)).TrashEntry);
|
|
}
|
|
|
|
/// <summary>Empties the whole trash as the Trash page does: the dry run, then the job with its fingerprint.</summary>
|
|
private static async Task EmptyTrashAsync(TestServer server, Browser browser)
|
|
{
|
|
TrashEmptyPlanDto plan;
|
|
using (var dry = await browser.SendAsync(HttpMethod.Post, "/api/v1/archive/trash/empty", Json("""{"names":null,"dryRun":true,"fingerprint":null}""")))
|
|
plan = await dry.ReadAsync(ProtocolJson.Default.TrashEmptyPlanDto);
|
|
Assert.True(plan.Entries.Count > 0, "the trash holds the deleted photo");
|
|
using (var empty = await browser.SendAsync(HttpMethod.Post, "/api/v1/archive/trash/empty",
|
|
Json($$"""{"names":null,"dryRun":false,"fingerprint":"{{plan.Fingerprint}}"}""")))
|
|
await empty.ExpectAsync(HttpStatusCode.Accepted);
|
|
await UntilIdleAsync(server);
|
|
using var listed = await browser.SendAsync(HttpMethod.Get, "/api/v1/archive/trash");
|
|
Assert.Empty((await listed.ReadAsync(ProtocolJson.Default.TrashListDto)).Entries);
|
|
}
|
|
|
|
/// <summary>Waits until no job is queued or running (the thumbnails, proxies and analyses uploads queue).</summary>
|
|
private static async Task UntilIdleAsync(TestServer server)
|
|
{
|
|
var until = DateTime.UtcNow + Wait;
|
|
foreach (var state in new[] { JobState.Queued, JobState.Running, JobState.Queued, JobState.Running })
|
|
{
|
|
while ((await server.Database.Jobs.ListAsync(state)).Items.Count > 0)
|
|
{
|
|
Assert.True(DateTime.UtcNow < until, $"jobs still {state}");
|
|
await Task.Delay(50);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>The archive's ingest with a step run once before the next commit (what lands between the completion's checks and it).</summary>
|
|
private sealed class GatedIngest(IArchiveIngest inner) : IArchiveIngest
|
|
{
|
|
public Func<Task>? BeforeCommit { get; set; }
|
|
|
|
public async Task<IngestResult> CommitAsync(IngestRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
if (BeforeCommit is { } step)
|
|
{
|
|
BeforeCommit = null;
|
|
await step();
|
|
}
|
|
return await inner.CommitAsync(request, cancellationToken);
|
|
}
|
|
}
|
|
|
|
/// <summary>A JPEG of a few chunks: noise does not compress below the chunk size.</summary>
|
|
private static byte[] Photo(int seed)
|
|
{
|
|
const int width = 512, height = 384;
|
|
var pixels = new byte[width * height * 3];
|
|
new Random(seed).NextBytes(pixels);
|
|
return JpegEncoder.Encode(pixels, width, height, 3, new JpegEncodeOptions { Quality = 95 });
|
|
}
|
|
}
|