import { getTextAttachmentType } from './attachment-upload.js'; export type AttachmentDeliveryMode = 'inline_native' | 'reference_only'; export type AttachmentArchiveType = 'gz' | 'tgz' | 'zip'; const ARCHIVE_MIME_TYPES_BY_TYPE: Record> = { gz: new Set(['application/gzip', 'application/x-gzip']), tgz: new Set([ 'application/tar+gzip', 'application/x-compressed-tar', 'application/x-gtar', 'application/x-tar+gzip', 'application/x-tgz' ]), zip: new Set(['application/x-zip', 'application/x-zip-compressed', 'application/zip']) }; type AttachmentTypeInput = { fileName?: string; mimeType?: string | null; }; function normalizeMimeType(mimeType?: string | null): string { return mimeType?.trim().toLowerCase() ?? ''; } function normalizeFileName(fileName?: string): string { return fileName?.trim().toLowerCase() ?? ''; } function hasAnyFileName(input: AttachmentTypeInput): boolean { return normalizeFileName(input.fileName).length > 0; } export function detectAttachmentArchiveType(input: AttachmentTypeInput): AttachmentArchiveType | null { const fileName = normalizeFileName(input.fileName); if (fileName.endsWith('.tar.gz') || fileName.endsWith('.tgz')) { return 'tgz'; } if (fileName.endsWith('.zip')) { return 'zip'; } if (fileName.endsWith('.gzip') || fileName.endsWith('.gz')) { return 'gz'; } const mimeType = normalizeMimeType(input.mimeType); if (!mimeType) { return null; } if (ARCHIVE_MIME_TYPES_BY_TYPE.zip.has(mimeType)) { return 'zip'; } if (ARCHIVE_MIME_TYPES_BY_TYPE.tgz.has(mimeType)) { return 'tgz'; } if (ARCHIVE_MIME_TYPES_BY_TYPE.gz.has(mimeType)) { return 'gz'; } return null; } export function classifyAttachmentDelivery(input: AttachmentTypeInput): AttachmentDeliveryMode { if (detectAttachmentArchiveType(input)) { return 'reference_only'; } const mimeType = normalizeMimeType(input.mimeType); if (mimeType.startsWith('image/') || mimeType === 'application/pdf') { return 'inline_native'; } if ( getTextAttachmentType({ fileName: input.fileName, mimeType }) !== null ) { return 'inline_native'; } return 'reference_only'; } export function isSupportedAttachmentUploadType(input: AttachmentTypeInput): boolean { if (detectAttachmentArchiveType(input)) { return true; } if (!hasAnyFileName(input) && !normalizeMimeType(input.mimeType)) { return false; } return classifyAttachmentDelivery(input) === 'inline_native'; }