Repository navigation
fix(finai): clean abandoned uploads and harden imports #291
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,33 +94,51 @@ export const generateUploadUrl = mutation({ | |
| const settings = await loadFinaiSettings(ctx, user._id); | ||
| if (!settings.enabled) throw aiError('AI_DISABLED'); | ||
| if (!settings.allowAttachments) throw new Error('ATTACHMENTS_DISABLED'); | ||
| const now = Date.now(); | ||
| const ticket = crypto.randomUUID(); | ||
| const uploadId = await ctx.db.insert('finaiAttachmentUploads', { | ||
| ticket, | ||
| userId: user._id, | ||
| createdAt: now, | ||
| }); | ||
| await ctx.scheduler.runAt( | ||
| now + finaiAttachmentLimits.registerWindowMs + 60_000, | ||
| internal.ai.attachments.expireUpload, | ||
| { uploadId }, | ||
| ); | ||
| return { | ||
| uploadUrl: await ctx.storage.generateUploadUrl(), | ||
| ticket, | ||
| maxBytes: finaiAttachmentLimits.maxBytes, | ||
| perMessage: finaiAttachmentLimits.perMessage, | ||
| extensions: Object.keys(BY_EXTENSION), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Records an uploaded file for the signed-in user. Files that are too big, empty or of an | ||
| * unsupported type are deleted and reported as `{ error }` (returned, not thrown, so the | ||
| * deletion commits). Uploads that are too old or already used elsewhere are refused untouched. | ||
| */ | ||
| /** Registers a ticket's uploaded file or deletes a known invalid object. */ | ||
| export const register = mutation({ | ||
| args: { storageId: v.id('_storage'), name: v.string() }, | ||
| handler: async (ctx, { storageId, name }) => { | ||
| args: { ticket: v.string(), storageId: v.id('_storage'), name: v.string() }, | ||
| handler: async (ctx, { ticket, storageId, name }) => { | ||
| const user = await requireUser(ctx); | ||
| if (!user || user.deletedAt !== undefined) throw new Error('AUTH_REQUIRED'); | ||
| const upload = await ctx.db | ||
| .query('finaiAttachmentUploads') | ||
| .withIndex('by_ticket', (q) => q.eq('ticket', ticket)) | ||
| .unique(); | ||
| const file = await ctx.db.system.get('_storage', storageId); | ||
| if (!file) throw new Error('UPLOAD_NOT_FOUND'); | ||
| const now = Date.now(); | ||
| if ( | ||
| now - file._creationTime > finaiAttachmentLimits.registerWindowMs || | ||
| !upload || | ||
| upload.userId !== user._id || | ||
| upload.storageId !== undefined || | ||
| now - upload.createdAt > finaiAttachmentLimits.registerWindowMs || | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On a slow connection, a valid attachment is rejected once 15 minutes have elapsed since the upload URL was requested, even if the object only just reached storage. A 10 MB upload can exceed that interval on a constrained mobile network, and this check now uses the ticket creation time rather than the stored file's Useful? React with 👍 / 👎. |
||
| !file || | ||
| file._creationTime < upload.createdAt - 1000 || | ||
| (await storageInUse(ctx, storageId)) | ||
| ) | ||
| throw new Error('INVALID_UPLOAD'); | ||
| await ctx.db.patch(upload._id, { storageId }); | ||
| const fileName = cleanFileName(name); | ||
| const type = attachmentKind(fileName, file.contentType); | ||
| const error = | ||
|
|
@@ -133,6 +151,7 @@ export const register = mutation({ | |
| : null; | ||
| if (error || !type) { | ||
| await ctx.storage.delete(storageId); | ||
| await ctx.db.delete(upload._id); | ||
| return { error: error ?? 'UNSUPPORTED_FILE' } as const; | ||
| } | ||
| const id = await ctx.db.insert('finaiAttachments', { | ||
|
|
@@ -145,10 +164,28 @@ export const register = mutation({ | |
| createdAt: now, | ||
| expiresAt: now + finaiAttachmentLimits.pendingTtlMs, | ||
| }); | ||
| await ctx.db.delete(upload._id); | ||
| return { attachment: await presentAttachment(ctx, (await ctx.db.get(id))!) } as const; | ||
| }, | ||
| }); | ||
|
|
||
| /** Deletes an expired ticket and its known uploaded object if it is not in use. */ | ||
| export const expireUpload = internalMutation({ | ||
| args: { uploadId: v.id('finaiAttachmentUploads') }, | ||
| handler: async (ctx, { uploadId }) => { | ||
| const upload = await ctx.db.get(uploadId); | ||
| if (!upload) return; | ||
| if ( | ||
| upload.storageId && | ||
| !(await storageInUse(ctx, upload.storageId)) && | ||
| (await ctx.db.system.get('_storage', upload.storageId)) | ||
| ) | ||
| await ctx.storage.delete(upload.storageId); | ||
| await ctx.db.delete(uploadId); | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
| async function deleteFile(ctx: MutationCtx, row: Doc<'finaiAttachments'>) { | ||
| if (row.storageId && (await ctx.db.system.get('_storage', row.storageId))) | ||
| await ctx.storage.delete(row.storageId); | ||
|
|
||
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.
When an import fails with a known error—such as a deleted account/category or an invalid row—the action has already returned
{ status: 'failed' }, but this patch persists it asrunning.actionViewstherefore renders the card as “Saving…” untilstaleRunningMselapses and then makes it retryable without cancellation; permanent failures repeat this cycle indefinitely instead of displaying the stored error as a terminal failure. Reserve the stale-running retry path for genuinely interrupted executions or introduce an explicit retryable failure state.Useful? React with 👍 / 👎.