mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
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:
@@ -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`;
|
||||
}
|
||||
|
||||
@@ -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`,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user