feat(app): export and import through @faunosteo/pdf — snapshot, archived branding and labels, browser rasterizer, imported archive kept in session, explicit rebase with messages; legacy PDF modules removed
This commit is contained in:
1 parent
20dafc0b00
commit
9bd5173a9c
43 files changed
+1492
-1989
No files matched your search
@@ -20,12 +20,14 @@ import { basename, join, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import {
|
||||
readReportDocument,
|
||||
REPORT_VERSION,
|
||||
readArchiveDocument,
|
||||
UnsupportedReportVersionError,
|
||||
} from '@faunosteo/domain'
|
||||
import { readAttachedDocument } from '../src/pdf/attachment'
|
||||
import { MAX_PDF_BYTES } from '../src/pdf/import'
|
||||
import {
|
||||
ArchiveReadError,
|
||||
MAX_PDF_BYTES,
|
||||
readAttachedDocument,
|
||||
} from '@faunosteo/pdf'
|
||||
import { parseBundledSheet } from '../src/sheets/provider'
|
||||
import { buildSheetIndex, type SheetIndex } from '../src/services/sheetIndex'
|
||||
import {
|
||||
@@ -87,23 +89,33 @@ async function compareFile(
|
||||
const bytes = new Uint8Array(readFileSync(path))
|
||||
const pdf = await PDFDocument.load(bytes, { ignoreEncryption: true })
|
||||
const raw = await readAttachedDocument(pdf)
|
||||
const version = (raw as { version: number }).version
|
||||
try {
|
||||
const outcome = readReportDocument(raw, sheet.sheet.migrationMap)
|
||||
return compareReport(raw, outcome, version !== REPORT_VERSION, sheet, args)
|
||||
const outcome = readArchiveDocument(raw, sheet.sheet.migrationMap)
|
||||
return compareReport(raw, outcome, sheet, args)
|
||||
} catch (err) {
|
||||
if (err instanceof UnsupportedReportVersionError) {
|
||||
throw new Error(`report version ${version} cannot be read`)
|
||||
// The version is a number or null: nothing of the attachment is echoed.
|
||||
throw new Error(`report version ${err.foundVersion} cannot be read`)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
/** The engine's typed read failures, worded without echoing the file. */
|
||||
const READ_FAILURES: Record<ArchiveReadError['kind'], string> = {
|
||||
invalid_pdf: 'not a readable PDF',
|
||||
no_attachment: 'no Faunosteo attachment found',
|
||||
attachment_stripped:
|
||||
'attachment stripped (the PDF is stamped as a Faunosteo archive)',
|
||||
incompatible_version: 'report version cannot be read',
|
||||
}
|
||||
|
||||
/** A missing or unreadable file is a failed entry, not a crash. */
|
||||
function failureReason(err: unknown): string {
|
||||
const code = (err as NodeJS.ErrnoException | undefined)?.code
|
||||
if (code === 'ENOENT') return 'file not found'
|
||||
if (code === 'EACCES') return 'file not readable'
|
||||
if (err instanceof ArchiveReadError) return READ_FAILURES[err.kind]
|
||||
// JSON.parse messages echo a fragment of the attachment.
|
||||
if (err instanceof SyntaxError) return 'attachment is not valid JSON'
|
||||
return err instanceof Error ? err.message : String(err)
|
||||
|
||||
Reference in new issue
Block a user