64ff982571
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.
29 lines
1.5 KiB
SQL
29 lines
1.5 KiB
SQL
-- Release the blob reference when an attachment row goes away.
|
|
--
|
|
-- `storage.file_attached_blobs.file_id` is `ON DELETE CASCADE`, so deleting a
|
|
-- file removes its attachment rows inside the database — invisible to Rust.
|
|
-- The lifecycle hook cannot cover this: `on_file_deleted` fires AFTER
|
|
-- `delete_file`, by which point the cascade has already run and there is
|
|
-- nothing left to read. The references would survive with no row behind them,
|
|
-- and `dedup_gc` would see a positive count forever — bytes pinned for good.
|
|
--
|
|
-- `storage.decrement_blob_ref()` already exists for exactly this, on
|
|
-- `storage.files`. It keys off `OLD.blob_hash` and is otherwise
|
|
-- table-agnostic, so it applies verbatim — and reusing it keeps the
|
|
-- manifest-first decrement contract defined in one place rather than
|
|
-- transcribed into a second trigger that can drift.
|
|
--
|
|
-- Only DELETE. Replacing a preview updates `blob_hash` in place
|
|
-- (`store_attached_blob` is ON CONFLICT DO UPDATE), and the reference to the
|
|
-- superseded blob is released there, in Rust. Adding UPDATE here would
|
|
-- double-decrement it.
|
|
|
|
CREATE OR REPLACE TRIGGER trg_file_attached_blobs_decrement_blob_ref
|
|
AFTER DELETE ON storage.file_attached_blobs
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION storage.decrement_blob_ref();
|
|
|
|
COMMENT ON TRIGGER trg_file_attached_blobs_decrement_blob_ref
|
|
ON storage.file_attached_blobs IS
|
|
'Releases the blob reference held by an attachment row. Needed because file_id is ON DELETE CASCADE, so rows vanish inside the DB where the Rust lifecycle hooks cannot see them.';
|