@@ -18,6 +18,7 @@ import {
1818 parseThreadSegmentFromAttachmentId ,
1919 PENDING_ATTACHMENT_THREAD_SEGMENT ,
2020 resolveAttachmentPathById ,
21+ sweepStalePendingAttachments ,
2122} from "../attachmentStore.ts" ;
2223import { resolveAttachmentRelativePath } from "../attachmentPaths.ts" ;
2324import {
@@ -32,7 +33,10 @@ import { inferImageExtension } from "../imageMime.ts";
3233
3334export const ATTACHMENT_UPLOAD_ROUTE_PREFIX = "/api/attachments/upload" ;
3435
36+ // Asset download tokens share this key, but their signed claim kind is different.
3537const SIGNING_SECRET_NAME = "asset-access-signing-key" ;
38+ const PENDING_ATTACHMENT_SWEEP_INTERVAL_MS = 15 * 60_000 ;
39+ const lastPendingSweepByDirectory = new Map < string , number > ( ) ;
3640
3741const AttachmentUploadClaims = Schema . Struct ( {
3842 version : Schema . Literal ( 1 ) ,
@@ -68,8 +72,25 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f
6872 const secret = yield * loadSigningSecret . pipe (
6973 Effect . mapError ( ( cause ) => new AttachmentUploadSigningKeyError ( { cause } ) ) ,
7074 ) ;
75+ const config = yield * ServerConfig . ServerConfig ;
76+ const nowMs = yield * Clock . currentTimeMillis ;
77+ const previousSweep = lastPendingSweepByDirectory . get ( config . attachmentsDir ) ;
78+ if (
79+ previousSweep === undefined ||
80+ nowMs - previousSweep >= PENDING_ATTACHMENT_SWEEP_INTERVAL_MS
81+ ) {
82+ lastPendingSweepByDirectory . set ( config . attachmentsDir , nowMs ) ;
83+ const swept = sweepStalePendingAttachments ( {
84+ attachmentsDir : config . attachmentsDir ,
85+ nowMs,
86+ } ) ;
87+ if ( swept . deleted > 0 ) {
88+ yield * Effect . logInfo ( "Removed expired attachment uploads." , { deleted : swept . deleted } ) ;
89+ }
90+ }
91+
7192 const attachmentId = createPendingAttachmentId ( ) ;
72- const expiresAt = ( yield * Clock . currentTimeMillis ) + ATTACHMENT_UPLOAD_URL_TTL_MS ;
93+ const expiresAt = nowMs + ATTACHMENT_UPLOAD_URL_TTL_MS ;
7394 const encodedPayload = base64UrlEncode (
7495 encodeAttachmentUploadClaims ( {
7596 version : 1 ,
0 commit comments