fix: fall back to Sharp when CLI decoders are unavailable

DNG and FITS previews showed "Preview not available" because their CLI
decoders (ExifTool/ImageMagick) were not installed. Sharp can read both
formats natively (DNG is TIFF-based, FITS via libvips fitsload).

Added Sharp fallback to the preview endpoint, batch processing, and
tool factory: when decodeToSharpCompat throws, try sharp(buffer).metadata()
before returning 422. If Sharp can read the buffer, processing continues
without the CLI decoder.
This commit is contained in:
SnapOtter
2026-05-11 18:00:59 +08:00
parent 7d9612784e
commit 2acf0ea48a
3 changed files with 36 additions and 16 deletions
+6 -1
View File
@@ -13,6 +13,7 @@ import { getBundleForTool, TOOL_BUNDLE_MAP } from "@snapotter/shared";
import archiver from "archiver";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import PQueue from "p-queue";
import sharp from "sharp";
import { env } from "../config.js";
import { autoOrient } from "../lib/auto-orient.js";
import { resolveConcurrency } from "../lib/env.js";
@@ -169,7 +170,11 @@ export async function registerBatchRoutes(app: FastifyInstance): Promise<void> {
if (ext) processFilename = `${processFilename.slice(0, -ext.length)}.png`;
}
if (!skipPreprocess && needsCliDecode(validation.format)) {
processBuffer = await decodeToSharpCompat(processBuffer, validation.format);
try {
processBuffer = await decodeToSharpCompat(processBuffer, validation.format);
} catch {
await sharp(processBuffer).metadata();
}
const ext = processFilename.match(/\.[^.]+$/)?.[0];
if (ext) processFilename = `${processFilename.slice(0, -ext.length)}.png`;
}
+19 -8
View File
@@ -156,17 +156,28 @@ export async function fileRoutes(app: FastifyInstance): Promise<void> {
try {
buffer = await decodeToSharpCompat(buffer, validation.format);
} catch {
return reply.status(422).send({
error: `Failed to decode ${validation.format.toUpperCase()} file`,
});
// CLI decoder unavailable -- try Sharp directly as fallback for preview
try {
await sharp(buffer).metadata();
} catch {
return reply.status(422).send({
error: `Failed to decode ${validation.format.toUpperCase()} file`,
});
}
}
}
const webp = await sharp(buffer)
.resize(1200, 1200, { fit: "inside", withoutEnlargement: true })
.webp({ quality: 80 })
.toBuffer();
return reply.header("Content-Type", "image/webp").send(webp);
try {
const webp = await sharp(buffer)
.resize(1200, 1200, { fit: "inside", withoutEnlargement: true })
.webp({ quality: 80 })
.toBuffer();
return reply.header("Content-Type", "image/webp").send(webp);
} catch {
return reply.status(422).send({
error: `Failed to generate preview for ${validation.format.toUpperCase()} file`,
});
}
});
}
+11 -7
View File
@@ -198,14 +198,18 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
try {
const fileExt = filename.split(".").pop()?.toLowerCase();
fileBuffer = await decodeToSharpCompat(fileBuffer, validation.format, fileExt);
const ext = filename.match(/\.[^.]+$/)?.[0];
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
} catch (err) {
return reply.status(422).send({
error: `Failed to decode ${validation.format.toUpperCase()} file`,
details: err instanceof Error ? err.message : String(err),
});
} catch {
try {
await sharp(fileBuffer).metadata();
} catch (err) {
return reply.status(422).send({
error: `Failed to decode ${validation.format.toUpperCase()} file`,
details: err instanceof Error ? err.message : String(err),
});
}
}
const ext = filename.match(/\.[^.]+$/)?.[0];
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
}
// Sanitize SVG input to prevent XXE, SSRF, and script injection