-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(core): don't create pending media rows when storage can't pre-sign #2284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Hridayesh13
wants to merge
2
commits into
emdash-cms:main
from
Hridayesh13:fix/media-signed-upload-pending-rows
+97
−8
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes the media table filling up with hidden, unusable `pending` records — one per upload attempt — when storage cannot create signed upload URLs, which is always the case for local storage and for R2 accessed through a Worker binding. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/core/tests/integration/api/media-upload-url-pending.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** The signed-upload endpoint must not leave a pending media row behind when storage cannot pre-sign. */ | ||
| import { Role } from "@emdash-cms/auth"; | ||
| import type { APIContext } from "astro"; | ||
| import type { Kysely } from "kysely"; | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { POST as requestUploadUrl } from "../../../src/astro/routes/api/media/upload-url.js"; | ||
| import type { DatabaseSchema } from "../../../src/database/types.js"; | ||
| import { EmDashStorageError } from "../../../src/storage/types.js"; | ||
| import type { Storage } from "../../../src/storage/types.js"; | ||
| import { setupTestDatabase, teardownTestDatabase } from "../../utils/test-db.js"; | ||
|
|
||
| /** Storage that behaves like local storage / an R2 binding: it cannot pre-sign. */ | ||
| function storageThatCannotPresign(): Storage { | ||
| return { | ||
| getSignedUploadUrl() { | ||
| throw new EmDashStorageError( | ||
| "Local storage does not support signed upload URLs. Upload files directly through the API.", | ||
| "NOT_SUPPORTED", | ||
| ); | ||
| }, | ||
| } as unknown as Storage; | ||
| } | ||
|
|
||
| function callRoute(db: Kysely<DatabaseSchema>, storage: Storage) { | ||
| const request = new Request("http://localhost:4321/_emdash/api/media/upload-url", { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ filename: "photo.png", contentType: "image/png", size: 1024 }), | ||
| }); | ||
|
|
||
| return requestUploadUrl({ | ||
| request, | ||
| url: new URL(request.url), | ||
| params: {}, | ||
| locals: { | ||
| emdash: { db, storage, config: {} }, | ||
| user: { id: "admin-1", role: Role.ADMIN }, | ||
| }, | ||
| // eslint-disable-next-line typescript/no-unsafe-type-assertion -- minimal stub for tests | ||
| } as unknown as APIContext); | ||
| } | ||
|
|
||
| async function countMedia(db: Kysely<DatabaseSchema>, status: string): Promise<number> { | ||
| const rows = await db.selectFrom("media").select("id").where("status", "=", status).execute(); | ||
| return rows.length; | ||
| } | ||
|
|
||
| describe("POST /_emdash/api/media/upload-url with storage that cannot pre-sign", () => { | ||
| let db: Kysely<DatabaseSchema>; | ||
|
|
||
| beforeEach(async () => { | ||
| db = await setupTestDatabase(); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await teardownTestDatabase(db); | ||
| }); | ||
|
|
||
| it("answers 501 NOT_SUPPORTED so the client falls back to direct upload", async () => { | ||
| const response = await callRoute(db, storageThatCannotPresign()); | ||
|
|
||
| expect(response.status).toBe(501); | ||
| const body = (await response.json()) as { error: { code: string } }; | ||
| expect(body.error.code).toBe("NOT_SUPPORTED"); | ||
| }); | ||
|
|
||
| it("does not create a pending media row", async () => { | ||
| await callRoute(db, storageThatCannotPresign()); | ||
|
|
||
| expect(await countMedia(db, "pending")).toBe(0); | ||
| }); | ||
|
|
||
| it("does not accumulate rows across repeated attempts", async () => { | ||
| for (let i = 0; i < 5; i++) { | ||
| // oxlint-disable-next-line no-await-in-loop -- sequential on purpose: the point is that repeats don't accumulate | ||
| await callRoute(db, storageThatCannotPresign()); | ||
| } | ||
|
|
||
| const all = await db.selectFrom("media").select("id").execute(); | ||
| expect(all).toHaveLength(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[needs fixing] This comment still embeds PR-specific adapter names (
local storage / an R2 binding). That context belongs in the PR description, not in a comment that future readers will see long after the adapter list changes. Trim it to a generic, one-sentence description of what the helper does.