Repository navigation
fix(media): check storage against MediaLit's sealed size - #933
Conversation
MediaLit v0.8.0 converts images to WebP at seal, so the sealed asset's size can differ from the unsealed upload, and it keeps a WebP even when it is larger than the original. Uploads are still admitted at their unsealed size, so nothing that can't fit gets sealed; when the sealed asset is larger, the finalize hold is re-checked against the limit and grown, or the sealed asset is deleted and the upload refused. Rows and storage usage already use the sealed size. The memory MediaLit client can now model a seal-time conversion, and the storage tests cover a smaller WebP, a larger one that fits, and a larger one that doesn't for staff and learners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ba86658859
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (refused) return { ok: false as const, error: refused }; | ||
| await db | ||
| .update(schema.mediaUploadReservations) | ||
| .set({ bytes }) |
There was a problem hiding this comment.
Extend the hold when resizing it
If finalizeUpload takes longer than the 15-minute HOLD_MS, this update changes only bytes: usage() ignores expired reservations, and resizeHold does not verify that the hold still exists or renew its expiry. It can therefore return success without reserving space until the media row is saved, allowing a concurrent upload to pass its limit check in that gap.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ba88ad. resizeHold now upserts the hold with a fresh expiresAt (now + HOLD_MS), so a seal that outran the hold renews it, and a hold the worker already pruned is recreated. inspectAdmitSeal also resizes after every seal, not only when the asset grew, so a slow seal that shrank the file is covered too. If other uploads took the space while the hold was expired, the resize refuses and the sealed asset is deleted as before. New tests in storage.test.ts ("resizing a finalize hold") cover the renewal, the recreate after pruning, and the refusal; the first two fail against the previous code.
A seal that outlasts HOLD_MS left the hold expired, so usage() stopped counting it, and the worker could prune it, until the media row was saved. resizeHold now upserts the hold with a fresh expiry, recreating it if it was pruned. inspectAdmitSeal resizes after every seal, not only when the asset grew, so a slow seal that shrank the file is covered too, and an upload is refused when other uploads took the space in the meantime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
MediaLit v0.8.0 moved WebP conversion from upload to seal.
inspectAdmitSealadmits an upload into the school's storage using the sizeinspectUploadreports (the original, e.g. a PNG), then seals it, and the media row stores the size the seal reports (the WebP). The two now differ, and the WebP can be larger: MediaLit doesn't compare the two sizes, and its only check at seal is against its own account-wide limit, not the school's.Fix
gate.resize(resizeHoldinstorage.ts) re-checks the sealed size against the limit, leaving out the current hold, and grows the hold. It locks the school row the same wayadmitUploaddoes.deleteAsset, the hold is released, and the upload gets the usual refusal (plan_limit_reachedfor staff,upload_not_allowedfor learners).MemoryMediaLitClientgains an optionalconvertOnSealsetting so tests can model a seal that returns a different name, type, size and URL. The default is unchanged.The second
checkafter the seal sees the WebP; all three paths acceptimage/webp(staff: completeness only; learner avatar:image/*; community:isLearnerCommunityMime), so nothing changed there.Testing
storage.test.ts: a PNG that seals to a smaller WebP stores the WebP's values and usage equals its size; a larger WebP that fits is kept and counted; a larger WebP that doesn't fit is refused, deleted from MediaLit, has no row, and its hold is released; a learner whose upload grows past their budget is refused. With the re-check disabled, the last two fail.apps/api:bun run typecheckpasses;bun run test813 pass, 0 fail.bun run lintfails with 11 errors in files this PR doesn't touch; the base commit has the same counts.🤖 Generated with Claude Code