// Client-side attachment checks, ported from a Golem15 SPA. They mirror // golem15.chat's ChatAttachmentPolicy so a refused file never uploads; the // server's checks (RejectBanned, the per-file and body limits) stay // authoritative. The error copy defaults to the SPA's Polish; the layer // passes its localized messages. import type { ChatLimits } from './types' /** golem15.chat BannedExts: script-capable types (stored XSS). */ export const BANNED_EXTENSIONS: readonly string[] = ['svg', 'svgz', 'html', 'htm', 'xhtml', 'xml'] /** golem15.chat BannedMIMEs. */ export const BANNED_MIME_TYPES: readonly string[] = ['image/svg+xml', 'text/html', 'application/xhtml+xml', 'text/xml', 'application/xml'] /** What checkFiles needs of a File. */ export interface FileLike { name: string size: number type: string } export interface FileCheck { accepted: F[] /** The error lines, one per kind (too-large names each file). */ errors: string[] } /** A byte limit in MB for the copy: 62914560 → "60", 1572864 → "1,5". */ export function mbLabel(bytes: number): string { const mb = bytes / (1024 * 1024) if (Number.isInteger(mb)) return String(mb) return (Math.floor(mb * 10) / 10).toFixed(1).replace('.', ',') } /** True for SVG, HTML and XML, by extension or MIME type. */ export function isBanned(file: FileLike): boolean { const dot = file.name.lastIndexOf('.') const ext = (dot >= 0 ? file.name.slice(dot + 1) : file.name).toLowerCase() if (BANNED_EXTENSIONS.includes(ext)) return true const mime = (file.type || '').split(';')[0].trim().toLowerCase() return BANNED_MIME_TYPES.includes(mime) } /** The error lines of checkFiles. */ export interface FileCheckMessages { tooLarge(name: string, mb: string): string overTotal(mb: string): string banned(): string tooMany(max: number): string } const PL_MESSAGES: FileCheckMessages = { tooLarge: (name, mb) => `Plik „${name}” jest za duży. Limit to ${mb} MB.`, overTotal: (mb) => `Załączniki jednej wiadomości mogą mieć razem najwyżej ${mb} MB.`, banned: () => 'Plików SVG, HTML i XML nie można wysyłać na czacie.', tooMany: (max) => `Możesz dołączyć najwyżej ${max} plików do jednej wiadomości.`, } /** * Splits picked files into those that may upload and the error lines for * the rest: banned types, files that would take the message's attachments * above limits.max_bytes in total (stagedBytes is what is already staged), * and anything over limits.max_files counting the chips already staged. */ export function checkFiles( files: readonly F[], limits: Pick, stagedCount: number, stagedBytes = 0, messages: FileCheckMessages = PL_MESSAGES, ): FileCheck { const accepted: F[] = [] const errors: string[] = [] let banned = false let tooMany = false let overTotal = false let total = stagedBytes for (const f of files) { if (isBanned(f)) { banned = true continue } if (limits.max_bytes > 0 && f.size > limits.max_bytes) { errors.push(messages.tooLarge(f.name, mbLabel(limits.max_bytes))) continue } if (stagedCount + accepted.length >= limits.max_files) { tooMany = true continue } if (limits.max_bytes > 0 && total + f.size > limits.max_bytes) { overTotal = true continue } total += f.size accepted.push(f) } if (overTotal) errors.unshift(messages.overTotal(mbLabel(limits.max_bytes))) if (banned) errors.unshift(messages.banned()) if (tooMany) errors.unshift(messages.tooMany(limits.max_files)) return { accepted, errors } }