/** * Browser previews of Word and PowerPoint uploads, so the original can be read next to the * extracted text. The result is a self-contained HTML page (pictures inlined as data URIs) * that the server sends with a sandboxing Content-Security-Policy. */ import { zipSync } from 'fflate'; import mammoth from 'mammoth'; import { pptxSlidesForPreview, WEB_IMAGE_MIME } from './pptx.ts'; import { ZipReader, type ZipLimits } from './zip.ts'; const LIMITS: ZipLimits = { maxEntryBytes: 64 * 1024 * 1024, maxTotalBytes: 300 * 1024 * 1024 }; const IMAGE_BUDGET = 40 * 1024 * 1024; const escapeHtml = (text: string) => text.replace(/[&<>"']/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[ch]!); const STYLE = ` :root { color-scheme: light dark; --ink: #1b1a17; --ink-2: #57534a; --line: #e4e0d6; --paper: #f7f5ef; --surface: #fffefa; } @media (prefers-color-scheme: dark) { :root { --ink: #ece9e1; --ink-2: #a8a396; --line: #34322d; --paper: #161512; --surface: #1e1d19; } } * { box-sizing: border-box; } body { margin: 0; padding: 20px; background: var(--paper); color: var(--ink); font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif; } img { max-width: 100%; height: auto; } .page { max-width: 820px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px 32px; } .page h1, .page h2, .page h3 { line-height: 1.25; } .page table { border-collapse: collapse; width: 100%; } .page td, .page th { border: 1px solid var(--line); padding: 4px 8px; vertical-align: top; } .slide { max-width: 900px; margin: 0 auto 18px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 18px 22px; } .slide-no { font: 600 11px/1 ui-monospace, Menlo, Consolas, monospace; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2); } .slide h2 { margin: 8px 0 10px; font-size: 20px; } .block { white-space: pre-wrap; margin: 0 0 8px; } .pics { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; } .pics img { max-height: 420px; border-radius: 8px; border: 1px solid var(--line); background: #fff; } .notes { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); color: var(--ink-2); font-size: 13.5px; white-space: pre-wrap; } .muted { color: var(--ink-2); font-size: 13px; } `; function page(title: string, body: string): string { return `${escapeHtml(title)}${body}`; } function pptxHtml(bytes: Uint8Array, title: string): string { const slides = pptxSlidesForPreview(bytes, LIMITS, IMAGE_BUDGET); const body = slides .map((s, i) => { const blocks = s.blocks.map((b) => `

${escapeHtml(b.replace(/^#{1,6}\s+/, ''))}

`).join(''); const pics = s.images.length ? `
${s.images.map((img) => ``).join('')}
` : ''; const skipped = s.skippedImages ? `

${s.skippedImages} picture${s.skippedImages === 1 ? '' : 's'} on this slide can't be shown here.

` : ''; const notes = s.notes ? `
Notes\n${escapeHtml(s.notes)}
` : ''; return `
Slide ${i + 1}
${s.title ? `

${escapeHtml(s.title)}

` : ''}${blocks}${pics}${skipped}${notes}
`; }) .join(''); return page(title, body || '

This deck has no slides.

'); } async function docxHtml(bytes: Uint8Array, title: string): Promise { // Re-pack through the size-limited reader (as extraction does), keeping pictures a browser can show. const zip = new ZipReader(bytes, LIMITS); const files: Record = {}; let budget = IMAGE_BUDGET; for (const entry of zip.entries) { const name = entry.name; if (name.endsWith('/')) continue; const isXml = /\.(xml|rels)$/i.test(name) && !/^(customXml|docProps\/thumbnail)/i.test(name); const isImage = /^word\/media\//i.test(name) && WEB_IMAGE_MIME[name.slice(name.lastIndexOf('.') + 1).toLowerCase()]; if (!isXml && !isImage) continue; const data = zip.read(name); if (!data) continue; if (isImage) { if (data.byteLength > budget) continue; budget -= data.byteLength; } files[name] = data; } const packed = zipSync(files, { level: 0 }); const converted = await mammoth.convertToHtml( { buffer: Buffer.from(packed.buffer, packed.byteOffset, packed.byteLength) }, { ignoreEmptyParagraphs: true, convertImage: mammoth.images.imgElement(async (image) => { if (!Object.values(WEB_IMAGE_MIME).includes(image.contentType)) return { src: '' }; return { src: `data:${image.contentType};base64,${await image.read('base64')}` }; }), }, ); // Links stay as text: the preview never navigates anywhere. const html = converted.value.replace(/]*>/gi, '').replace(/]*>/gi, ''); return page(title, `
${html || '

This document has no content to show.

'}
`); } export async function officePreviewHtml(kind: 'docx' | 'pptx', bytes: Uint8Array, title: string): Promise { return kind === 'pptx' ? pptxHtml(bytes, title) : docxHtml(bytes, title); } /** Served with the preview: no scripts, no network, only inline styles and data-URI pictures. */ export const PREVIEW_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'; frame-ancestors 'self'; sandbox";