Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:51:05 +01:00
co-authored by Claude Fable 5.2
parent b6f9f34705
commit 97202ee03e
18 changed files with 335 additions and 128 deletions
+34 -7
View File
@@ -86,8 +86,13 @@ export type SubmitFailure =
/**
* The file could not be handed to the mail server (or the form has no
* e-mail that may carry it). The file is gone — the visitor must send it
* again. `submissionId`: the stored attempt, marked "Not sent".
* again. `submissionId`: the stored attempt, marked "Not sent". `field`
* and `file`: the File field and the file name(s), so the message shows
* at the field and names the file; `optional`: the field is not required.
*/
field?: string
file?: string
optional?: boolean
reason: 'delivery'
submissionId?: number | string
success: false
@@ -95,14 +100,16 @@ export type SubmitFailure =
| {
/** The offending field's name, when one field is at fault. */
field?: string
/** The refused file's name (file_too_large, file_type). */
/** The refused file's name (file_too_large, file_type, file_refused). */
file?: string
/** What was wrong with it. */
kind?: 'file_count' | 'file_too_large' | 'file_type' | 'invalid_format' | 'required' | 'too_long' | 'unknown_fields'
kind?: 'file_count' | 'file_refused' | 'file_too_large' | 'file_type' | 'invalid_format' | 'required' | 'too_long' | 'unknown_fields'
/** The offending field's label in the page's language — what the visitor sees. Absent when the field has none. */
label?: string
/** The limit behind file_too_large (MB) or file_count (files). */
max?: number
/** A file problem on a field that is not required — the form can be sent without the file. */
optional?: boolean
reason: 'validation'
success: false
/** Accepted types, "PDF, DOCX" (file_type). */
@@ -206,6 +213,7 @@ export async function fileFailure(payload: BasePayload, formId: string, failure:
...(failure.file ? { file: failure.file } : {}),
...(label ? { label } : {}),
...(failure.max !== undefined ? { max: failure.max } : {}),
...(failure.optional ? { optional: true } : {}),
...(failure.types ? { types: failure.types } : {}),
}
}
@@ -229,9 +237,17 @@ export async function findSubmitAttempt(payload: BasePayload, attemptKey: string
limit: 1,
where: { attemptKey: { equals: attemptKey } },
} as never)
const doc = found.docs[0] as { filesDelivery?: string; id: number | string } | undefined
const doc = found.docs[0] as { filesDelivery?: string; id: number | string; submissionFiles?: { field?: string; name?: string }[] } | undefined
if (!doc) {return null}
return doc.filesDelivery === 'failed' ? { reason: 'delivery', submissionId: doc.id, success: false } : { submissionId: doc.id, success: true }
if (doc.filesDelivery !== 'failed') {return { submissionId: doc.id, success: true }}
const kept = doc.submissionFiles ?? []
return {
reason: 'delivery',
submissionId: doc.id,
success: false,
...(kept[0]?.field ? { field: kept[0].field } : {}),
...(kept.length ? { file: kept.map((f) => f.name).join(', ') } : {}),
}
} catch {
return null
}
@@ -260,6 +276,7 @@ export async function storeSubmission({
} & Pick<SubmitFormArgs, 'formId' | 'locale' | 'payload'>): Promise<SubmitFormResult> {
const attachments = getFormAttachments(payload)
const delivery: FormFileDelivery | undefined = files.length ? { files } : undefined
const fileNames = files.map((f) => ({ name: f.name, field: f.field }))
const text = (value: unknown) =>
// checkboxGroup → "a, b" (readable in the panel and in {{field}} e-mails)
value == null ? '' : Array.isArray(value) ? value.join(', ') : typeof value === 'object' ? JSON.stringify(value) : String(value as boolean | number | string)
@@ -297,7 +314,17 @@ export async function storeSubmission({
} catch (err) {
payload.logger.error(`[ipal] Could not record the file delivery on submission ${String(submission.id)}: ${(err as Error).message}`)
}
return result.state === 'sent' ? { submissionId: submission.id, success: true } : { reason: 'delivery', submissionId: submission.id, success: false }
if (result.state === 'sent') {return { submissionId: submission.id, success: true }}
// The message belongs at the File field and names the file — never the mail server's own words.
const fileField = validation.fileFields.find((f) => f.name === fileNames[0]?.field)
return {
field: fileNames[0]?.field,
file: fileNames.map((f) => f.name).join(', '),
reason: 'delivery',
submissionId: submission.id,
success: false,
...(fileField && !fileField.required ? { optional: true } : {}),
}
} catch (err) {
payload.logger.error(`[ipal] Form submission failed: ${(err as Error).message}`)
return { reason: 'error', success: false }
@@ -345,7 +372,7 @@ export async function submitForm(args: SubmitFormArgs): Promise<SubmitFormResult
return storeSubmission({ attemptKey, formId, locale, payload, validation })
}
if (hasFiles && !canCarryFiles(validation.form)) {return { reason: 'delivery', success: false }}
if (hasFiles && !canCarryFiles(validation.form)) {return { field: Object.keys(files ?? {})[0], reason: 'delivery', success: false }}
const gate = createFileGate({ fields: validation.fileFields, options: attachments, transportMaxBytes: await mailAttachmentLimit(payload) })
const refused = await checkBufferedFiles(gate, files ?? {})
if (refused) {return fileFailure(payload, formId, refused, locale)}