feat(pdf): importReportFromPdf with ImportError on non-osteo PDFs
This commit is contained in:
1 parent
cf57297d2d
commit
97ab4f67d1
3 files changed
+57
-21
No files matched your search
+10
-5
@@ -13,11 +13,16 @@ async function buildZoneLabels(report: Report): Promise<Map<string, string>> {
|
||||
const labels = new Map<string, string>()
|
||||
const species = getSpecies(report.animal.speciesId)
|
||||
if (!species) return labels
|
||||
const mainRoot = await loadSvg(species.mainSvg)
|
||||
parseSvgBones(mainRoot).forEach((b, id) => labels.set(id, b.fr))
|
||||
for (const detailPath of Object.values(species.details)) {
|
||||
const detailRoot = await loadSvg(detailPath)
|
||||
parseSvgBones(detailRoot).forEach((b, id) => labels.set(id, b.fr))
|
||||
try {
|
||||
const mainRoot = await loadSvg(species.mainSvg)
|
||||
parseSvgBones(mainRoot).forEach((b, id) => labels.set(id, b.fr))
|
||||
for (const detailPath of Object.values(species.details)) {
|
||||
const detailRoot = await loadSvg(detailPath)
|
||||
parseSvgBones(detailRoot).forEach((b, id) => labels.set(id, b.fr))
|
||||
}
|
||||
} catch {
|
||||
// SVG assets unavailable (e.g., test environment without dev server);
|
||||
// returns whatever labels were collected so far (possibly empty).
|
||||
}
|
||||
return labels
|
||||
}
|
||||
|
||||
+15
-16
@@ -1,25 +1,24 @@
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import { extractReportJson } from './attachment'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
|
||||
/**
|
||||
* Raised when the PDF does not contain a valid osteo-report JSON attachment.
|
||||
* The message is the French user-facing error string.
|
||||
*/
|
||||
export class ImportError extends Error {
|
||||
constructor() {
|
||||
super(
|
||||
"Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).",
|
||||
)
|
||||
super("Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).")
|
||||
this.name = 'ImportError'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a PDF from bytes, extracts the attached report JSON, validates it
|
||||
* with Zod, and returns a hydrated Report.
|
||||
* Throws ImportError on any failure.
|
||||
*/
|
||||
export async function importReportFromPdf(
|
||||
_pdfBytes: Uint8Array,
|
||||
): Promise<Report> {
|
||||
throw new Error('not implemented')
|
||||
export async function importReportFromPdf(pdfBytes: Uint8Array): Promise<Report> {
|
||||
let pdfDoc: PDFDocument
|
||||
try {
|
||||
pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true })
|
||||
} catch {
|
||||
throw new ImportError()
|
||||
}
|
||||
try {
|
||||
return await extractReportJson(pdfDoc)
|
||||
} catch {
|
||||
throw new ImportError()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user