import type {SuperagentNativeMediaItem} from './useSuperagentAttachmentPicker'; import { SUPERAGENT_ATTACHMENT_SIZE_LIMIT_MB, SUPERAGENT_SUPPORTED_ATTACHMENT_EXTENSIONS, } from './mediaUtils'; export function validateNativeAttachment(item: SuperagentNativeMediaItem, name: string, extension: string) { if (!SUPERAGENT_SUPPORTED_ATTACHMENT_EXTENSIONS.includes(extension as typeof SUPERAGENT_SUPPORTED_ATTACHMENT_EXTENSIONS[number])) { throw new Error(`File type "${extension || 'unknown'}" is not supported.`); } const limitMb = SUPERAGENT_ATTACHMENT_SIZE_LIMIT_MB[extension] ?? 5; if (typeof item.size === 'number' && item.size > limitMb * 1024 * 1024) { throw new Error(`File "${name}" exceeds the maximum size of ${limitMb}MB.`); } } export function getUploadName(item: SuperagentNativeMediaItem, index: number) { const baseName = item.name || getNameFromUri(item.uri) || `attachment-${index + 1}`; if (getExtension(baseName)) { return baseName; } const extension = getExtension(item.uri) || getExtensionForMimeType(item.mimeType) || '.jpg'; return `${baseName}${extension}`; } export function getExtension(value?: string) { const cleanValue = String(value || '').split('?')[0].toLowerCase(); const index = cleanValue.lastIndexOf('.'); return index >= 0 ? cleanValue.slice(index) : ''; } export function getMimeType(item: SuperagentNativeMediaItem, extension: string) { if (item.mimeType) { return item.mimeType; } switch (extension) { case '.jpg': case '.jpeg': return 'image/jpeg'; case '.png': return 'image/png'; case '.webp': return 'image/webp'; case '.pdf': return 'application/pdf'; case '.csv': return 'text/csv'; case '.html': return 'text/html'; case '.txt': return 'text/plain'; case '.xlsx': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; case '.docx': return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; case '.json': return 'application/json'; case '.md': return 'text/markdown'; default: return 'application/octet-stream'; } } function getNameFromUri(uri?: string) { if (!uri) { return null; } const cleanUri = uri.split('?')[0]; const name = cleanUri.split('/').filter(Boolean).pop(); if (!name || name.startsWith('ph:')) { return null; } try { return decodeURIComponent(name); } catch { return name; } } function getExtensionForMimeType(mimeType?: string) { switch (mimeType) { case 'image/jpeg': return '.jpg'; case 'image/png': return '.png'; case 'image/webp': return '.webp'; case 'application/pdf': return '.pdf'; case 'text/plain': return '.txt'; case 'text/html': return '.html'; case 'text/csv': return '.csv'; case 'application/json': return '.json'; case 'text/markdown': return '.md'; default: return null; } }