feat(thumbnails): uploaded previews survive a copy

Completes step 9. The PUT wrote `ext-{file_id}.jpg` and nothing else —
keyed by file id, on local disk. No copy path duplicates it and no other
instance can see it, so a copied file lost the preview its owner
uploaded. Silently: the server falls back to rendering one from the
source, or to 204 for a PDF, which has no render path at all. A
user-supplied preview is not derivable from the content, so once lost it
is gone.

The PUT now also records a storage.file_attached_blobs row, which
copy_file_satellites already duplicates, so both copy paths carry it.
Best-effort: the sidecar has already succeeded by then and the user can
see their thumbnail, so failing the request would report an error for an
operation that visibly worked.

Read path consults attachments ahead of every content-derived tier: an
uploaded preview is an explicit choice about THIS file and must beat
anything rendered from its content. Cached under the per-file key — a
content key would leak those bytes to every other file sharing the
content, which is the poisoning the file-keyed table exists to prevent.

store_attached_blob is ON CONFLICT DO UPDATE, unlike its derived twin:
re-uploading a preview is a deliberate replacement, where a re-derived
thumbnail is the same bytes again. The superseded blob's reference is
released, or it would be pinned forever with nothing pointing at it.

Deletion goes through a trigger, not a hook. file_id is ON DELETE
CASCADE, and on_file_deleted fires AFTER delete_file — by then the
cascade has run and there is nothing left to enumerate. This matters
most for folder deletion, where PG cascades folders to files to
attachments and Rust never sees the rows at all. storage.decrement_blob_ref
keys off OLD.blob_hash and is otherwise table-agnostic, so it is reused
verbatim rather than transcribed into a second trigger that can drift.
DELETE only: a replacement updates in place and is handled in Rust, so
adding UPDATE would double-decrement.

Extracted read_blob_to_bytes, shared by the attached and derived tiers —
the only difference between them is which table produced the hash.

tests/api/attached_thumbnail_copy.hurl guards it. The file is red and
the uploaded thumbnail is green, so a render could never produce the
uploaded bytes; the pre-upload render is captured first and required to
change, which stops three identical renders from satisfying the
byte-equality. Then both copy paths must serve the upload, and after the
original is purged and GC runs, both copies must still serve it — each
holds its own reference, because the rows are duplicated rather than
shared.
This commit is contained in:
Edouard Vanbelle
2026-08-25 07:39:57 +02:00
parent fac82fea23
commit 64ff982571
6 changed files with 511 additions and 24 deletions
+47 -5
View File
@@ -726,15 +726,57 @@ impl FileHandler {
return AppError::from(err).into_response();
}
// Validate, re-encode to WebP, and store
match thumbnail_service
// Validate, re-encode, and store the per-file sidecar.
let stored = match thumbnail_service
.store_external_thumbnail(&id, thumb_size.into(), body)
.await
{
Ok(_) => StatusCode::CREATED.into_response(),
Err(err) => AppError::internal_error(format!("Failed to store thumbnail: {}", err))
.into_response(),
Ok(bytes) => bytes,
Err(err) => {
return AppError::internal_error(format!("Failed to store thumbnail: {}", err))
.into_response();
}
};
// Also record it as a file-keyed attachment.
//
// The sidecar above is `ext-{file_id}.jpg` on local disk, which no
// copy path duplicates and no other instance can see. Without this
// row a copied file loses the preview its owner uploaded — falling
// back to a rendered thumbnail, or to nothing at all for a PDF, which
// has no server-side render path. `copy_file_satellites` duplicates
// the row, so the copy inherits the bytes.
//
// File-keyed, never content-keyed: these bytes are the uploader's
// claim about THIS file, and sharing them across files with identical
// content is the poisoning vector `storage.file_attached_blobs`
// exists to prevent.
//
// Best-effort: the sidecar already succeeded, so the user has their
// thumbnail. Failing the request here would report an error for an
// operation that visibly worked.
if let Err(e) = state
.core
.dedup_service
.store_attached_blob(
&id,
"preview",
thumb_size.dir_name(),
"image/jpeg",
stored,
auth_user.id,
)
.await
{
tracing::warn!(
target: "oxicloud::dedup",
error = %e,
file_id = %id,
"failed to record attached thumbnail; sidecar written, copies will not inherit it"
);
}
StatusCode::CREATED.into_response()
}
// ═══════════════════════════════════════════════════════════════════════