import { DrawingError, Opened, mmPerUnit } from './drawing-file'; import { isDwg } from './dwg'; import { CancelledError, DrawingJob, openOffPage } from './open-client'; import { PdfOptions, PdfPage, makePdf, planPage } from './pdf'; import { importUrl } from '../shared/import-url'; import { vendored } from '../shared/vendor'; /** * Many DWG and DXF files at once, to PDF or to DXF, packed in one ZIP with * their folders kept. Each file is opened the same way as a single one (in * the worker), and the ZIP is written as it goes with fflate's streaming Zip * (MIT, served from this site), its pieces gathered into a Blob after each * file, so a file's bytes are let go once added and memory stays bounded. */ export const MAX_BATCH_FILES = 500; export const MAX_BATCH_BYTES = 200 * 1024 * 1024; const FFLATE = vendored('fflate@0.8.3/+esm'); interface FflateStream { Zip: new (cb: (err: Error | null, chunk: Uint8Array, final: boolean) => void) => { add(file: unknown): void; end(): void }; ZipPassThrough: new (name: string) => { push(data: Uint8Array, final?: boolean): void }; ZipDeflate: new (name: string, opts?: { level: number }) => { push(data: Uint8Array, final?: boolean): void }; } export interface BatchItem { file: File; /** The folder it was in, inside what was dropped or chosen ("Plans/Level 1"), or ''. */ folder: string; status: 'waiting' | 'working' | 'done' | 'skipped'; /** Why it was skipped. */ note?: string; /** Its name in the ZIP. */ out?: string; } export function batchAccepts(name: string): boolean { return /\.(dwg|dxf)$/i.test(name); } /** "Plans/Level 1/House.pdf", with a number added when two files would land on the same name. */ export function outName(item: { file: File; folder: string }, taken: Set, ext: string): string { const safe = (s: string) => s.replace(/[\\:*?"<>|\x00-\x1f]/g, '_').replace(/^\.+$/, '_'); const folder = item.folder.split('/').filter((p) => p && p !== '.' && p !== '..').map(safe).join('/'); const base = safe(item.file.name.replace(/\.[^.]+$/, '') || 'drawing'); for (let n = 1; ; n++) { const name = `${folder ? folder + '/' : ''}${base}${n > 1 ? ` (${n})` : ''}.${ext}`; if (!taken.has(name.toLowerCase())) { taken.add(name.toLowerCase()); return name; } } } /** Every DWG and DXF under dropped folders and files, with its folder; others are only counted. */ export async function walkEntries(entries: FileSystemEntry[]): Promise<{ found: { file: File; folder: string }[]; others: number; capped: boolean }> { const found: { file: File; folder: string }[] = []; const count = { others: 0, capped: false }; const walk = async (entry: FileSystemEntry, folder: string): Promise => { if (entry.isFile) { if (!batchAccepts(entry.name)) count.others++; else if (found.length >= MAX_BATCH_FILES) count.capped = true; else found.push({ file: await new Promise((res, rej) => (entry as FileSystemFileEntry).file(res, rej)), folder }); return; } if (!entry.isDirectory) return; const inner = folder ? `${folder}/${entry.name}` : entry.name; const reader = (entry as FileSystemDirectoryEntry).createReader(); // readEntries hands a large folder over in batches of about 100; keep asking until it is empty. for (;;) { const batch = await new Promise((res, rej) => reader.readEntries(res, rej)); if (!batch.length) break; for (const b of batch) await walk(b, inner); } }; for (const e of entries) await walk(e, ''); return { found, ...count }; } export interface BatchRun { target: 'pdf' | 'dxf'; pdf: PdfOptions; /** For a PDF: the tab each drawing was saved on, or every tab. */ content: 'tab' | 'all'; /** Set to stop after the file being worked on. */ stopped: () => boolean; /** The job opening the current file, so Stop can end it. */ onJob: (job: DrawingJob | null) => void; /** Something changed in the list. */ update: () => void; } /** The pages of one drawing's PDF, or why it cannot be made. */ export function pdfPages(opened: Opened, o: PdfOptions, content: 'tab' | 'all'): PdfPage[] | { skip: string } { const sheets = content === 'all' ? opened.scene.sheets : [opened.scene.sheets[opened.scene.initial]]; const pages: PdfPage[] = []; for (const sheet of sheets) { const plan = planPage(sheet, o); if ('error' in plan) return { skip: `${sheets.length > 1 ? `${sheet.name}: ` : ''}${plan.error}` }; pages.push({ sheet, plan }); } return pages; } /** Converts each file in turn into one ZIP; null when nothing was converted. */ export async function runBatch(items: BatchItem[], run: BatchRun): Promise { const lib = (await importUrl(FFLATE)) as FflateStream; let zipped = new Blob([], { type: 'application/zip' }); let pending: Uint8Array[] = []; let zipError: Error | null = null; const zip = new lib.Zip((err, chunk) => { if (err) zipError = err; else pending.push(chunk); }); const gather = () => { if (!pending.length) return; zipped = new Blob([zipped, ...(pending as BlobPart[])], { type: 'application/zip' }); pending = []; }; const taken = new Set(); let added = 0; for (const item of items) { if (run.stopped()) break; item.status = 'working'; run.update(); try { const out = await convertOne(item.file, run); if (run.stopped()) { item.status = 'waiting'; break; } if ('skip' in out) { item.status = 'skipped'; item.note = out.skip; } else { item.out = outName(item, taken, run.target); // A PDF is compressed already; a DXF is text and shrinks several times over. const entry = run.target === 'pdf' ? new lib.ZipPassThrough(item.out) : new lib.ZipDeflate(item.out, { level: 6 }); zip.add(entry); entry.push(out.bytes, true); gather(); if (zipError) throw zipError; added++; item.status = 'done'; } } catch (err) { if ((err as Error) === zipError) throw err; if (err instanceof CancelledError || run.stopped()) { item.status = 'waiting'; break; } item.status = 'skipped'; item.note = err instanceof DrawingError ? shortReason(err.message) : 'could not be converted'; } finally { run.onJob(null); run.update(); } } if (!added) return null; zip.end(); gather(); if (zipError) throw zipError; return zipped; } async function convertOne(file: File, run: BatchRun): Promise<{ bytes: Uint8Array } | { skip: string }> { if (file.size > MAX_BATCH_BYTES) return { skip: `larger than ${MAX_BATCH_BYTES / 1024 / 1024} MB` }; const bytes = new Uint8Array(await file.arrayBuffer()); if (run.target === 'dxf' && !isDwg(bytes)) return { skip: /\.dxf$/i.test(file.name) ? 'already a DXF' : 'not a DWG file' }; const job = openOffPage(bytes, file.name, () => {}); run.onJob(job); const opened = await job.result; if (run.target === 'dxf') return opened.dxf ? { bytes: opened.dxf } : { skip: 'not a DWG file' }; // Each drawing's scale is worked from its own units; the chosen ones stand in for a drawing that does not say. const pages = pdfPages(opened, { ...run.pdf, unitMm: mmPerUnit(opened.units) || run.pdf.unitMm }, run.content); if ('skip' in pages) return pages; const hidden = new Set([...opened.layers].filter(([, l]) => !l.visible).map(([n]) => n)); const blob = await makePdf(pages, opened.scene.sheets[0].space, run.pdf, hidden, file.name.replace(/\.[^.]+$/, '')); return { bytes: new Uint8Array(await blob.arrayBuffer()) }; } /** A single-file message ("House.dwg is a binary DXF, ...") cut down to the reason, for the list. */ function shortReason(message: string): string { return message.replace(/^.*? (is|has|could) /, (m, verb) => (verb === 'could' ? 'could ' : verb === 'has' ? 'has ' : '')).replace(/\.\s.*$/, '').replace(/\.$/, ''); }