WIP: form attachments (e-mail only) — core, endpoint, Graph large upload
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
0ba0014ed5
commit
6003b3d7d7
@@ -2,15 +2,24 @@ import type { PayloadEmailAdapter, SendEmailOptions } from 'payload'
|
||||
|
||||
import { type IntegrationStatus, recordIntegrationStatus } from '../integrations/status.js'
|
||||
import { getSiteIntegrations } from '../payload/index.js'
|
||||
import { graphAdapter, type GraphAdapterArgs } from './graphAdapter.js'
|
||||
import { graphAdapter, type GraphAdapterArgs, graphLargeAttachmentSupport } from './graphAdapter.js'
|
||||
import { attachmentBytes, GRAPH_INLINE_MAX_BYTES, toGraphAttachments } from './graphAttachments.js'
|
||||
import { panelSmtpAdapter, type PanelSmtpAdapterArgs } from './panelSmtpAdapter.js'
|
||||
|
||||
type TransportIntegrations = {
|
||||
emailStatus?: IntegrationStatus | null
|
||||
/** 'smtp' | 'graph' — chosen by the editor in SiteIntegrations. */
|
||||
emailTransport?: 'graph' | 'smtp' | null
|
||||
smtpHost?: null | string
|
||||
smtpPassword?: null | string
|
||||
smtpUser?: null | string
|
||||
}
|
||||
|
||||
const smtpConfigured = (s: TransportIntegrations) => Boolean(s.smtpHost && s.smtpUser && s.smtpPassword)
|
||||
|
||||
/** Graph result codes that mean "Graph cannot carry an attachment this large" — SMTP may. */
|
||||
const GRAPH_TOO_LARGE = new Set(['graph-large-off', 'graph-permission'])
|
||||
|
||||
const STATUS_FIELD = 'emailStatus'
|
||||
/** Error text for the panel: first line, no stack, capped — SMTP errors don't carry the password. */
|
||||
const describe = (err: unknown) =>
|
||||
@@ -68,8 +77,24 @@ export const mailAdapter =
|
||||
|
||||
const { payload } = deps
|
||||
|
||||
/**
|
||||
* The largest total attachment size (bytes) the current transport can
|
||||
* carry, or null when the plugin knows no ceiling (the mail server's own
|
||||
* limit still applies). Read by the forms module, so a File field never
|
||||
* promises more than can be sent: on Graph without the large-file path and
|
||||
* without SMTP settings the ceiling is GRAPH_INLINE_MAX_BYTES.
|
||||
*/
|
||||
const attachmentLimit = async (): Promise<null | number> => {
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
if ((settings.emailTransport ?? 'smtp') !== 'graph' || !graphAvailable()) {return null}
|
||||
const support = await graphLargeAttachmentSupport(args.graph)
|
||||
if (support === 'ok' || support === 'unavailable' || smtpConfigured(settings)) {return null}
|
||||
return GRAPH_INLINE_MAX_BYTES - 1
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'ipal-mail-dispatcher',
|
||||
attachmentLimit,
|
||||
defaultFromAddress: smtp.defaultFromAddress,
|
||||
defaultFromName: smtp.defaultFromName,
|
||||
|
||||
@@ -88,11 +113,24 @@ export const mailAdapter =
|
||||
)
|
||||
}
|
||||
|
||||
const transport = useGraph ? 'Microsoft Graph' : 'SMTP'
|
||||
const code = useGraph ? 'graph-error' : 'smtp-error'
|
||||
// A large attachment Graph cannot carry (switched off, or the app lacks
|
||||
// Mail.ReadWrite) goes over SMTP when the panel has SMTP settings.
|
||||
const attachments = useGraph ? toGraphAttachments(message) : []
|
||||
const large = Boolean(attachments?.length) && attachmentBytes(attachments ?? []) >= GRAPH_INLINE_MAX_BYTES
|
||||
const support = large ? await graphLargeAttachmentSupport(args.graph) : 'ok'
|
||||
const largeOverSmtp = large && support !== 'ok' && support !== 'unavailable' && smtpConfigured(settings)
|
||||
const viaGraph = useGraph && !largeOverSmtp
|
||||
|
||||
const transport = viaGraph ? 'Microsoft Graph' : 'SMTP'
|
||||
const code = viaGraph ? 'graph-error' : 'smtp-error'
|
||||
let result: unknown
|
||||
try {
|
||||
result = await (useGraph ? graph : smtp).sendEmail(message)
|
||||
result = await (viaGraph ? graph : smtp).sendEmail(message)
|
||||
// Graph found out only while sending (a token it could not read): one try over SMTP. Graph sent nothing.
|
||||
const refused = result as { code?: string; sent?: boolean } | null
|
||||
if (viaGraph && refused?.sent === false && GRAPH_TOO_LARGE.has(refused.code ?? '') && smtpConfigured(settings)) {
|
||||
result = await smtp.sendEmail(message)
|
||||
}
|
||||
} catch (err) {
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, {
|
||||
code,
|
||||
@@ -104,10 +142,10 @@ export const mailAdapter =
|
||||
|
||||
// The delegates report failure as { sent: false, error, detail? } instead
|
||||
// of throwing (a broken mailbox must not crash a form submission).
|
||||
const failed = result as { detail?: string; error?: string; sent?: boolean } | null
|
||||
const failed = result as { code?: string; detail?: string; error?: string; sent?: boolean } | null
|
||||
if (failed && typeof failed === 'object' && failed.sent === false) {
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, {
|
||||
code,
|
||||
code: GRAPH_TOO_LARGE.has(failed.code ?? '') ? 'graph-large-files' : code,
|
||||
message: `${transport}: ${describe(failed.detail ?? failed.error ?? 'not sent')}`,
|
||||
state: 'degraded',
|
||||
})
|
||||
@@ -119,7 +157,9 @@ export const mailAdapter =
|
||||
payload,
|
||||
STATUS_FIELD,
|
||||
previous,
|
||||
graphFallback
|
||||
large && support === 'no-permission'
|
||||
? { code: 'graph-large-files', state: 'degraded' }
|
||||
: graphFallback
|
||||
? {
|
||||
code: 'graph-unavailable',
|
||||
message:
|
||||
|
||||
Reference in New Issue
Block a user