fix(sync,upload): keep tombstones honest and let an explicit upload bring a photo back

Sync:
- A server frame that has not been hashed is known by content only once it is
  downloaded: one this PC deleted (or holds as its replacement) is now deleted
  from staging instead of filed again, which also dropped its tombstone.
- An unhashed local original that turns out to be a photo the server deleted
  is not uploaded, and the archive verify then writes the hashes into the
  index, so the row settles instead of offering Upload for good.
- A tombstone leaves a photo out of the comparison only when its side no longer
  holds that content, so a tombstone left beside a held copy does not keep the
  other side's copy missing forever.

Uploads:
- StartUploadRequest.Restore (optional, never sent by sync or older clients)
  lets an explicit user upload file a photo deleted from the sequence again,
  also after the trash was emptied; the completion repeats it with
  ?restore=true and the ingest drops the tombstone.
- The completion checks the tombstone again: a photo deleted while its bytes
  were on the way is not filed (409 photo_removed, upload cancelled); the
  protocol client reports that as not sent.
- A tombstone of content a frame of the target still holds refuses no repair.
- Hidden dot names (AppleDouble "._" files) are refused, as the archive
  scanner leaves them out.

Web upload dialog:
- Upload again on rows marked Deleted before; the tooltip no longer promises
  a trash restore that may be impossible.
- Sends a capture time: the Exif DateTimeOriginal of a JPEG or TIFF-based file
  (minimal in-house reader, capture-time.js), else the file's time, so photos
  without Exif keep their order.
- Esc is stopped at keydown during a run (Chromium allows one prevented cancel
  per activation); hidden dot files are left out; a completion refused while
  another one files the upload waits for it; the same photo picked under two
  names waits for its first copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d051bcfc351f5a08500ca864a6508576a95492a4)
This commit is contained in:
Alby96 committed 2026-10-27 12:45:33 +01:00
1 parent eb607d47aa
commit 316c182b19
18 files changed
+753 -63

No files matched your search

@@ -0,0 +1,91 @@
// The capture time the upload dialog sends with a photo (owner review 2, R3), as local wall time without an offset
// ("2025-05-01T06:30:00", the way the desktop clients send it): the Exif DateTimeOriginal of a JPEG or of a TIFF-based file
// (TIFF, DNG and most RAW formats), else the file's modification time. The server keeps the time a photo records itself
// and uses this one only for a photo that records none (a PNG, an export stripped of its Exif data), so such photos keep
// their order instead of taking the moment they arrived. The reader is minimal: it follows one IFD pointer, checks every
// offset against the bytes it read and gives up (null) on anything it does not expect.
/** Bytes read from the start of a file: every camera writes the Exif block within them. */
const READ = 256 * 1024;
/** The capture time of a picked File; null when neither the photo nor the file gives a usable one. */
export async function captureTime(file) {
try {
const taken = exifCaptureTime(new Uint8Array(await file.slice(0, READ).arrayBuffer()));
if (taken) return taken;
} catch {
// Unreadable here: the file's own time stands in, and the server reads the photo again anyway.
}
const modified = file.lastModified;
return Number.isFinite(modified) && modified > 0 ? localIso(new Date(modified)) : null;
}
/** The Exif DateTimeOriginal of the JPEG or TIFF whose first bytes are given, as "yyyy-MM-ddTHH:mm:ss"; null when there is none. */
export function exifCaptureTime(bytes) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const size = view.byteLength;
let tiff = -1;
if (size >= 4 && view.getUint16(0) === 0xffd8) {
// JPEG: the segments before the image data, up to the APP1 that starts with "Exif\0\0".
for (let at = 2; at + 4 <= size;) {
const marker = view.getUint16(at);
if ((marker & 0xff00) !== 0xff00 || marker === 0xffda) break;
const length = view.getUint16(at + 2);
if (marker === 0xffe1 && length >= 16 && at + 10 <= size && view.getUint32(at + 4) === 0x45786966 && view.getUint16(at + 8) === 0) {
tiff = at + 10;
break;
}
if (length < 2) break;
at += 2 + length;
}
} else {
tiff = 0;
}
if (tiff < 0 || tiff + 8 > size) return null;
const order = view.getUint16(tiff);
if (order !== 0x4949 && order !== 0x4d4d) return null;
const little = order === 0x4949;
const u16 = (at) => view.getUint16(at, little);
const u32 = (at) => view.getUint32(at, little);
if (u16(tiff + 2) !== 42) return null;
// The 12-byte entry of a tag in the IFD at an offset from the TIFF header; -1 when it is not within reach.
const entry = (ifd, tag) => {
const at = tiff + ifd;
if (ifd < 8 || at + 2 > size) return -1;
const count = u16(at);
for (let i = 0; i < count; i++) {
const e = at + 2 + i * 12;
if (e + 12 > size) return -1;
if (u16(e) === tag) return e;
}
return -1;
};
const exif = entry(u32(tiff + 4), 0x8769);
if (exif < 0) return null;
const original = entry(u32(exif + 8), 0x9003);
// ASCII "YYYY:MM:DD HH:MM:SS" and its terminator, stored after the IFD.
if (original < 0 || u16(original + 2) !== 2 || u32(original + 4) < 19) return null;
const text = tiff + u32(original + 8);
if (text + 19 > size) return null;
let value = "";
for (let i = 0; i < 19; i++) value += String.fromCharCode(view.getUint8(text + i));
const m = /^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/.exec(value);
if (!m) return null;
const [year, month, day, hour, minute, second] = m.slice(1).map(Number);
// A real date and time only ("0000:00:00 00:00:00" is what cameras write without a clock): checked in UTC, where every
// wall time exists.
const check = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
if (year < 1900 || check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day ||
check.getUTCHours() !== hour || check.getUTCMinutes() !== minute || check.getUTCSeconds() !== second) return null;
return `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}`;
}
/** Local wall time with milliseconds and no offset; null outside the years a server date takes. */
export function localIso(date) {
const year = date.getFullYear();
if (!(year >= 1 && year <= 9999)) return null;
const p = (n, width = 2) => String(n).padStart(width, "0");
return `${p(year, 4)}-${p(date.getMonth() + 1)}-${p(date.getDate())}T${p(date.getHours())}:${p(date.getMinutes())}:${p(date.getSeconds())}` +
`.${p(date.getMilliseconds(), 3)}`;
}
+114 -16
View File
@@ -1,18 +1,22 @@
// Upload dialog of the admin panel (owner review 2, R3; BRIEF §14.4, §14.8): photos from this computer into an existing
// or a new sequence, chosen with the file picker or dropped on the dialog. Folders are not taken: importing a whole folder
// stays a feature of the desktop app. Each photo is hashed in a module worker (hash-worker.js, which self-tests its
// SHA-256 first and refuses to hash when a check fails), started, sent in the chunks the server does not have yet and
// completed; the server verifies the hash again before it files the photo.
// SHA-256 first and refuses to hash when a check fails), started with its capture time (capture-time.js), sent in the
// chunks the server does not have yet and completed; the server verifies the hash again before it files the photo. A
// photo deleted from the sequence before is not filed again unless the user asks for it on its row (Upload again).
import { ApiFailure } from "./api.js";
import { t, plural, applyStrings, culture } from "./i18n.js";
import { escapeHtml, attr, bar, formatBytes, formatNumber, errorText } from "./ui.js";
import { icons } from "./icons.js";
import { captureTime } from "./capture-time.js";
/** Photos sent at once, and chunks in flight for each: four requests, half of what the server takes from one account. */
const FILES_AT_ONCE = 2;
const CHUNKS_AT_ONCE = 2;
/** Retries of a call that failed for a passing reason (no answer, 429, 503), after 1, 2 and 4 seconds. */
const RETRIES = 3;
/** Checks of an upload another completion is filing, after 1, 2, 4 and then every 8 seconds: about a minute in all. */
const SETTLE_CHECKS = 10;
const SEARCH_DELAY = 250;
const LIST_LIMIT = 50;
/** States of a photo already on the server: a later run leaves it alone. */
@@ -48,6 +52,8 @@ export function openUpload({ api, sequence, onDone } = {}) {
let searchTimer = null;
let searchToken = 0;
let frame = 0;
// Per run: by SHA-256, the send of the last photo of that content, which the next copy waits for.
let sameContent = new Map();
const items = [];
const skipped = { folders: 0, formats: 0, size: 0 };
const titles = new Map();
@@ -103,6 +109,17 @@ export function openUpload({ api, sequence, onDone } = {}) {
else dialog.close();
});
list.addEventListener("click", (e) => {
// Upload again: a photo deleted from the sequence before goes back into it with the next run, on the user's word.
const again = e.target.closest('[data-act="again"]');
if (again && !running) {
const item = items.find((i) => i.key === again.closest("[data-key]").dataset.key);
if (item?.state !== "deleted") return;
item.restore = true;
setState(item, "waiting");
drawButtons();
startButton.focus();
return;
}
const button = e.target.closest('[data-act="remove"]');
if (!button || running) return;
const index = items.findIndex((i) => i.key === button.closest("[data-key]").dataset.key);
@@ -134,7 +151,12 @@ export function openUpload({ api, sequence, onDone } = {}) {
take(files, folders);
});
// Esc closes only while nothing is being sent; Cancel stops a run.
// Esc closes only while nothing is being sent; Cancel stops a run. Chromium lets a dialog's cancel be prevented only
// once per user activation, so during a run Esc is stopped at its keydown; the cancel handler stays for close requests
// that do not come from the keyboard (a back gesture).
dialog.addEventListener("keydown", (e) => {
if (running && e.key === "Escape") e.preventDefault();
});
dialog.addEventListener("cancel", (e) => {
if (running) e.preventDefault();
});
@@ -234,13 +256,17 @@ export function openUpload({ api, sequence, onDone } = {}) {
`<option disabled>${escapeHtml(t("Admin.Upload.NoMatch"))}</option>`;
}
/** Adds picked or dropped files: photos once each; folders, other files and files above the limit are counted. */
/**
* Adds picked or dropped files: photos once each; folders, other files and files above the limit are counted. Hidden
* dot files (the AppleDouble "._" companions a Mac leaves on cards among them) are left out like the archive scanner
* leaves them out, and the server refuses them too.
*/
function take(files, folders) {
skipped.folders += folders;
for (const file of files) {
const dot = file.name.lastIndexOf(".");
const extension = dot < 0 ? "" : file.name.slice(dot + 1).toLowerCase();
if (file.size === 0 || !extensions?.has(extension)) {
if (file.size === 0 || file.name.startsWith(".") || !extensions?.has(extension)) {
skipped.formats++;
continue;
}
@@ -250,14 +276,16 @@ export function openUpload({ api, sequence, onDone } = {}) {
}
const key = `${file.name}\u0000${file.size}\u0000${file.lastModified}`;
if (items.some((i) => i.key === key)) continue;
const item = { key, file, state: "waiting", hashed: 0, sent: 0, uploadId: null, message: "", row: null };
const item = { key, file, state: "waiting", hashed: 0, sent: 0, uploadId: null, restore: false, message: "", row: null };
item.row = document.createElement("li");
item.row.className = "upload-file";
item.row.dataset.key = key;
item.row.innerHTML = `<span class="name ellipsis" dir="auto" title="${attr(file.name)}">${escapeHtml(file.name)}</span>
<span class="size num muted">${escapeHtml(formatBytes(file.size))}</span><span class="status"></span>
<button class="btn icon small subtle" type="button" data-act="remove" title="${attr(t("Admin.Upload.Remove.Tip"))}"
aria-label="${attr(t("Admin.Upload.Remove", file.name))}">${icons.close}</button>`;
aria-label="${attr(t("Admin.Upload.Remove", file.name))}">${icons.close}</button>
<button class="btn icon small subtle" type="button" data-act="again" hidden title="${attr(t("Admin.Upload.Again.Tip"))}"
aria-label="${attr(t("Admin.Upload.Again", file.name))}">${icons.upload}</button>`;
list.appendChild(item.row);
items.push(item);
drawItem(item);
@@ -341,7 +369,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
}
status.innerHTML = (fraction === null ? "" : bar(fraction)) +
`<span class="state ${tone} ellipsis"${tip ? ` title="${attr(tip)}"` : ""}>${escapeHtml(text)}</span>`;
item.row.querySelector('[data-act="remove"]').hidden = !removable(item);
drawRowButtons(item);
}
/** A photo comes off the list only between runs, and only while it is not on the server. */
@@ -349,6 +377,12 @@ export function openUpload({ api, sequence, onDone } = {}) {
return !running && !FINISHED.has(item.state) && !ACTIVE.has(item.state);
}
/** Remove, or for a photo deleted from the sequence before, Upload again; both only between runs. */
function drawRowButtons(item) {
item.row.querySelector('[data-act="remove"]').hidden = !removable(item);
item.row.querySelector('[data-act="again"]').hidden = running || item.state !== "deleted";
}
/** Progress redraws at most once a frame: the worker reports every 8 MiB, chunks finish four at a time. */
function update(item) {
item.dirty = true;
@@ -400,7 +434,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
addButton.disabled = running || extensions === null;
closeButton.textContent = t(running ? "Admin.Upload.Cancel" : "Admin.Upload.Close");
closeButton.title = t(running ? "Admin.Upload.Cancel.Tip" : "Admin.Upload.Close.Tip");
for (const item of items) item.row.querySelector('[data-act="remove"]').hidden = !removable(item);
for (const item of items) drawRowButtons(item);
targetHost.querySelectorAll("input, select").forEach((field) => {
field.disabled = running;
});
@@ -419,6 +453,7 @@ export function openUpload({ api, sequence, onDone } = {}) {
showError("");
running = true;
controller = new AbortController();
sameContent = new Map();
const signal = controller.signal;
window.addEventListener("beforeunload", guard);
drawButtons();
@@ -520,7 +555,9 @@ export function openUpload({ api, sequence, onDone } = {}) {
/**
* One photo: hashed, started (the server may already have it, or have had it before it was deleted), the missing
* chunks sent two at a time, completed. True when the library changed.
* chunks sent two at a time, completed. True when the library changed. The same photo picked under another name
* waits for the first copy, which settles it for both: two lanes would otherwise complete the one upload the server
* gives them and refuse each other.
*/
async function send(item, sequenceId, signal) {
const file = item.file;
@@ -533,12 +570,32 @@ export function openUpload({ api, sequence, onDone } = {}) {
});
if (signal.aborted) throw signal.reason;
setState(item, "starting");
const earlier = sameContent.get(sha256);
let settle;
sameContent.set(sha256, new Promise((resolve) => {
settle = resolve;
}));
try {
await earlier;
if (signal.aborted) throw signal.reason;
return await sendHashed(item, sha256, sequenceId, signal);
} finally {
settle();
}
}
/** A hashed photo: started (asking to bring it back when the user chose Upload again), its chunks sent, completed. */
async function sendHashed(item, sha256, sequenceId, signal) {
const file = item.file;
const restore = item.restore;
const capturedAt = await captureTime(file);
const start = await retry(() => api.post("/api/v1/uploads", {
sequenceId, fileName: file.name, size: file.size, sha256, capturedAt: null, sessionKey: null,
sequenceId, fileName: file.name, size: file.size, sha256, capturedAt, sessionKey: null, restore,
}), signal);
if (start.alreadyExists) {
// No frame: the photo was deleted from this sequence; a restore from the trash brings it back (R6).
setState(item, start.frameId ? "exists" : "deleted");
// No frame: the photo was deleted from this sequence; a restore from the trash brings it back while the trash
// holds it, or Upload again on its row (R6).
finish(item, start.frameId ? "exists" : "deleted");
return false;
}
item.uploadId = start.uploadId;
@@ -567,16 +624,57 @@ export function openUpload({ api, sequence, onDone } = {}) {
}
};
await Promise.all(Array.from({ length: Math.min(CHUNKS_AT_ONCE, missing.length) }, lane));
if (failure !== null) throw failure;
// A chunk refused because the upload is no longer receiving: another completion of it is filing it (or it ended),
// which the completion below waits for and reports.
if (failure !== null && !filing(failure)) throw failure;
setState(item, "verifying");
await retry(() => api.post(`/api/v1/uploads/${item.uploadId}/complete`), signal);
try {
await complete(item.uploadId, restore, signal);
} catch (err) {
if (!(err instanceof ApiFailure && err.status === 409 && err.code === "photo_removed")) throw err;
// Deleted from the sequence while it was on its way: not filed, and the server dropped the upload (R6).
item.uploadId = null;
finish(item, "deleted");
return false;
}
item.uploadId = null;
setState(item, "done");
finish(item, "done");
return true;
}
/** A photo is settled on the server: a later run leaves it alone, until Upload again asks to bring it back. */
function finish(item, state) {
item.restore = false;
setState(item, state);
}
});
}
/** 409 upload_state: the upload is not receiving now, because a completion is filing it or it ended. */
function filing(err) {
return err instanceof ApiFailure && err.status === 409 && err.code === "upload_state";
}
/**
* Completes an upload (with restore, a photo deleted from the sequence meanwhile is filed anyway). A completion refused
* while another one is filing the upload (the same photo started twice, a retry after a lost answer) waits for it: once
* the upload is committed, completing again answers its frame. Only an upload that failed or was cancelled, or one that
* stays busy for about a minute, fails the photo.
*/
async function complete(uploadId, restore, signal) {
const url = `/api/v1/uploads/${uploadId}/complete${restore ? "?restore=true" : ""}`;
for (let check = 0; ; check++) {
try {
return await retry(() => api.post(url), signal);
} catch (err) {
if (signal.aborted || !filing(err) || check >= SETTLE_CHECKS) throw err;
await sleep(1000 * Math.min(2 ** check, 8), signal);
const upload = await retry(() => api.get(`/api/v1/uploads/${uploadId}`), signal);
if (upload?.state === "failed" || upload?.state === "cancelled") throw err;
}
}
}
/** What dropped: the files, and how many folders were left out (the entries API tells them apart where it exists). */
function dropped(transfer) {
const files = [];
+3 -2
View File
@@ -27,8 +27,9 @@ public static class UploadEndpoints
(await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState)));
uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CancelAsync(caller, id, context.RequestAborted)).ToResult(context, _ => Results.NoContent()));
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
// ?restore=true repeats the start's StartUploadRequest.Restore: a photo deleted from the sequence meanwhile is filed anyway.
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, bool? restore, UploadService service) =>
(await service.CompleteAsync(caller, id, restore == true)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
.Requires(Permissions.Upload);
uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
}
+14 -3
View File
@@ -56,13 +56,24 @@ public static class UploadRules
return intact.FirstOrDefault(f => f.SequenceId == target) ?? intact.FirstOrDefault();
}
/// <summary>
/// A name the archive scanner would never take as a photo (<c>ArchiveScanner</c>: hidden dot files, among them the
/// AppleDouble <c>._</c> companions a Mac leaves on exFAT cards): a frame upload refuses it, so no client files what an
/// import of the same folder would leave out.
/// </summary>
public static bool IsHiddenName(string fileName) => fileName.StartsWith('.');
/// <summary>
/// Whether <paramref name="target"/> tombstoned <paramref name="sha256"/> (owner review 2, R6): a photo deleted from that
/// sequence (or replaced by a converted file) that an upload must not bring back. The upload is answered as
/// <c>alreadyExists</c> without a frame, so a client neither sends the file nor treats the answer as a failure.
/// <c>alreadyExists</c> without a frame, so a client neither sends the file nor treats the answer as a failure. A
/// tombstone of content a frame of the target still holds (<paramref name="copies"/>: another copy of a duplicate, a
/// file put back by hand) is stale and refuses nothing, so such a frame can still be repaired.
/// </summary>
public static async Task<bool> RemovedFromTargetAsync(ChronosDatabase db, string sha256, Guid? target, CancellationToken cancellationToken) =>
target is { } sequence && await db.FrameRemovals.IsRemovedAsync(sequence, sha256, cancellationToken).ConfigureAwait(false);
public static async Task<bool> RemovedFromTargetAsync(ChronosDatabase db, string sha256, Guid? target, IReadOnlyCollection<Frame> copies,
CancellationToken cancellationToken) =>
target is { } sequence && !copies.Any(f => f.SequenceId == sequence)
&& await db.FrameRemovals.IsRemovedAsync(sequence, sha256, cancellationToken).ConfigureAwait(false);
/// <summary>
/// The sequence an upload files into: its own, or the one its session key names (the user's own key without one);
+31 -8
View File
@@ -49,7 +49,8 @@ public sealed class UploadService(
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, StartUploadRequest request, CancellationToken cancellationToken)
{
if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Fail<StartUploadResponse>(400, "invalid_file_name");
if (UploadRules.SafeFileName(request.FileName) is not { } fileName || UploadRules.IsHiddenName(fileName))
return Fail<StartUploadResponse>(400, "invalid_file_name");
if (!UploadRules.IsSha256(request.Sha256)) return Fail<StartUploadResponse>(400, "invalid_hash");
if (request.Size <= 0) return Fail<StartUploadResponse>(400, "bad_request", "size");
if (request.Size > options.MaxUploadBytes) return Fail<StartUploadResponse>(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
@@ -62,11 +63,14 @@ public sealed class UploadService(
var copies = await db.Frames.FindBySha256Async(sha, cancellationToken);
var target = await UploadRules.TargetAsync(db, archive.Id, request.SequenceId, sessionKey, caller.Id, cancellationToken);
// A photo deleted from the target sequence is not filed into it again: an old client syncing a copy that still holds
// it, or a watch folder sending it again, hears that the server has it (owner review 2, R6). A restore brings it back.
if (await UploadRules.RemovedFromTargetAsync(db, sha, target, cancellationToken))
return new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, null);
// Only a copy that is still whole stands for the upload: a client may delete its file on this answer.
if (UploadRules.StandIn(archive.Root, copies, target) is { } known)
// it, or a watch folder sending it again, hears that the server has it (owner review 2, R6). A restore from the trash
// brings it back while the trash holds it; a user who uploads it again on purpose asks with Restore, and filing it
// drops its tombstone.
var removed = await UploadRules.RemovedFromTargetAsync(db, sha, target, copies, cancellationToken);
if (removed && !request.Restore) return new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, null);
// Only a copy that is still whole stands for the upload: a client may delete its file on this answer. A photo
// brought back is filed into its sequence, whatever other sequences hold it.
if (UploadRules.StandIn(archive.Root, removed ? [] : copies, target) is { } known)
return new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, known.Id);
var resumable = await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken);
@@ -158,9 +162,11 @@ public sealed class UploadService(
/// <summary>
/// Verifies and commits a frame upload. Completing an already committed upload answers its frame again, so a client
/// may retry a completion whose response it lost. Runs to the end even if the client disconnects.
/// may retry a completion whose response it lost. Runs to the end even if the client disconnects. A photo deleted from
/// the target sequence since the upload started is not filed (409 <see cref="UploadErrors.PhotoRemoved"/>, the upload
/// cancelled) unless <paramref name="restore"/> repeats the start's <see cref="StartUploadRequest.Restore"/>.
/// </summary>
public async Task<Outcome<CompleteUploadResponse>> CompleteAsync(Caller caller, Guid id)
public async Task<Outcome<CompleteUploadResponse>> CompleteAsync(Caller caller, Guid id, bool restore = false)
{
var stopping = lifetime.ApplicationStopping;
var upload = await db.Uploads.GetAsync(id, stopping);
@@ -174,6 +180,15 @@ public sealed class UploadService(
if (!ArchiveHasRoom(upload)) return Fail<CompleteUploadResponse>(507, "insufficient_storage");
if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Committing, cancellationToken: stopping))
return Fail<CompleteUploadResponse>(409, "upload_state");
// Deleted from the target while its bytes were on the way (the start checked before): the upload does not bring the
// photo back (owner review 2, R6).
if (!restore && await RemovedFromTargetAsync(upload, stopping))
{
logger.LogInformation("Upload {Upload} ({File}) not filed: the photo was deleted from its sequence meanwhile", id, upload.FileName);
await db.Uploads.TryTransitionAsync(id, UploadState.Committing, UploadState.Cancelled, cancellationToken: CancellationToken.None);
store.Delete(id);
return Fail<CompleteUploadResponse>(409, UploadErrors.PhotoRemoved);
}
var user = await db.Users.GetAsync(caller.Id, stopping);
if (user is { QuotaBytes: > 0 } && user.UsedBytes + upload.Size > user.QuotaBytes && !await HeldByTargetAsync(upload, stopping))
@@ -305,6 +320,14 @@ public sealed class UploadService(
return copies.Any(f => f.SequenceId == target);
}
/// <summary>The sequence the upload files into tombstoned its content (<see cref="UploadRules.RemovedFromTargetAsync"/>).</summary>
private async Task<bool> RemovedFromTargetAsync(Upload upload, CancellationToken cancellationToken)
{
var target = await UploadRules.TargetAsync(db, archive.Id, upload.SequenceId, upload.SessionKey, upload.UserId, cancellationToken);
return target is not null && await UploadRules.RemovedFromTargetAsync(db, upload.Sha256, target,
await db.Frames.FindBySha256Async(upload.Sha256, cancellationToken), cancellationToken);
}
private StartUploadResponse Started(Upload upload) =>
new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null);