fix: add exotic format decoding to image-to-pdf tool

The image-to-pdf route used ensureSharpCompat (HEIC-only) instead of the
full format decode pipeline from tool-factory. Formats like FITS, PSD,
RAW, EXR, HDR, TGA, etc. passed through undecoded and crashed Sharp.

Replace with validateImageBuffer + decodeToSharpCompat to match the
standard tool pipeline.
This commit is contained in:
SnapOtter
2026-05-11 22:47:10 +08:00
parent 095e3d9488
commit 56e8cf7352
2 changed files with 21 additions and 6 deletions
+18 -3
View File
@@ -7,8 +7,10 @@ import sharp from "sharp";
import { z } from "zod";
import { autoOrient } from "../../lib/auto-orient.js";
import { formatZodErrors } from "../../lib/errors.js";
import { validateImageBuffer } from "../../lib/file-validation.js";
import { sanitizeFilename } from "../../lib/filename.js";
import { ensureSharpCompat } from "../../lib/heic-converter.js";
import { decodeToSharpCompat, needsCliDecode } from "../../lib/format-decoders.js";
import { decodeHeic } from "../../lib/heic-converter.js";
import { createWorkspace } from "../../lib/workspace.js";
const targetSizeSchema = z.object({
@@ -179,8 +181,21 @@ export function registerImageToPdf(app: FastifyInstance) {
const preparedBuffers: Buffer[] = [];
for (const file of files) {
const compatBuffer = await autoOrient(await ensureSharpCompat(file.buffer));
preparedBuffers.push(compatBuffer);
let buf = file.buffer;
const validation = await validateImageBuffer(buf, file.filename);
if (!validation.valid) {
return reply.status(400).send({ error: `Invalid image: ${validation.reason}` });
}
if (validation.format === "heif") {
buf = await decodeHeic(buf);
} else if (needsCliDecode(validation.format)) {
const fileExt = file.filename.split(".").pop()?.toLowerCase();
buf = await decodeToSharpCompat(buf, validation.format, fileExt);
}
preparedBuffers.push(await autoOrient(buf));
}
let imageBuffers: Buffer[];