mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3063] [Issue]: Chat-gallery and global-gallery uploads orphan a partial file when the upload stream is truncated or aborted #1629
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1629
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3063
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
Both the chat-gallery upload (
POST /api/gallery/:chatId/upload) and the global-gallery upload (POST /api/global-gallery/upload) stream the request body to a file withawait pipeline(data.file, createWriteStream(filePath))and do NOT wrap that pipeline in a try/catch that deletes the partial file on failure. If the upload stream errors mid-write — the client aborts the connection, or the file exceeds the 256 MB@fastify/multipartfileSizelimit so the stream is truncated — the pipeline rejects, the request 500s, and a partially-written file is left stranded on disk with no DB row. For the chat-gallery case, gallery-recovery re-adopts it as a full image only if the chat still exists (and even then writes a corrupt "valid" image); otherwise it is a permanent orphan the API cannot delete. The sibling routescustom-emojis.routes.ts:103-114andcustom-stickers.routes.ts:107-118DO wrap the same pipeline in try/catch + cleanup, confirming this is an inconsistency.How to reproduce (try it yourself)
POSTa multipart upload to/api/gallery/<existingChatId>/uploadwith a.pngfile part whose body is larger than 256 MB (or simply close the TCP connection partway through sending the body).data/gallery/<chatId>/.You'll see: the request fails (500 or aborted) and a truncated
<id>.pngremains on disk with no correspondingchat_imagesrecord. The same applies to/api/global-gallery/upload, leaving a stray file indata/gallery/global/.Expected: a failed/aborted upload deletes the partial file and returns 400 (or 413 when the multipart layer truncated an oversize stream).
Where it is
packages/server/src/routes/gallery.routes.ts:396—await pipeline(...)with no cleanup anywhere in the handler.packages/server/src/routes/global-gallery.routes.ts:112—await pipeline(...)sits OUTSIDE the DB-only rollback try (lines 121-141), which only unlinks on acreateImagefailure, not a pipeline rejection.app.ts:45,67-71(MAX_UPLOAD_BYTES = 256 MB). Recovery:gallery-recovery.ts:29-31,47-64.What's happening
The
await pipeline(...)that writes the uploaded bytes is not guarded by a try/catch that unlinks the just-created file on stream error.createWriteStreamcreates the file before bytes flow, so a mid-stream error strands a partial file. global-gallery rolls back only on a DB failure; gallery has no rollback at all.Suggested fix
Wrap the pipeline call in its OWN try/catch in BOTH routes, unlinking the partial file and returning 413 on truncation else 400. Do NOT reuse the global-gallery DB-rollback try.
gallery.routes.ts, replace line 396:
global-gallery.routes.ts, replace line 112 with the same block (using a "global gallery" log message).
existsSync,unlinkSync, andloggerare already imported in both files. Leave the existing DB-rollback try/catch (global-gallery lines 121-141) exactly as-is.Regression avoided: the original suggestion offered moving the pipeline INTO the global-gallery DB-rollback try, but that catch returns a hardcoded 500 'Failed to save image metadata', which would mislabel a 413/400 transport failure. A dedicated pipeline try/catch keeps the two failure modes distinct. The happy path is unchanged (pipeline resolves -> falls through), and the client hook already treats any non-2xx upload as a failure via
Promise.allSettled, so returning 400/413 instead of an uncaught 500 is strictly better. A plainer variant that always returns 400 (matching custom-emojis verbatim) is also acceptable; the.truncatedcheck just gives the more accurate 413.AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Claiming this for the current staging sweep in draft PR #3064: https://github.com/Pasta-Devs/Marinara-Engine/pull/3064
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3064 on branch
codex/issue-sweep-3054-3063(commit3ae8ca37). Closing this issue as addressed; the PR remains open for staging review/merge.