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 archiver from "archiver";
|
||||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
import PQueue from "p-queue";
|
import PQueue from "p-queue";
|
||||||
|
import sharp from "sharp";
|
||||||
import { env } from "../config.js";
|
import { env } from "../config.js";
|
||||||
import { autoOrient } from "../lib/auto-orient.js";
|
import { autoOrient } from "../lib/auto-orient.js";
|
||||||
import { resolveConcurrency } from "../lib/env.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 (ext) processFilename = `${processFilename.slice(0, -ext.length)}.png`;
|
||||||
}
|
}
|
||||||
if (!skipPreprocess && needsCliDecode(validation.format)) {
|
if (!skipPreprocess && needsCliDecode(validation.format)) {
|
||||||
|
try {
|
||||||
processBuffer = await decodeToSharpCompat(processBuffer, validation.format);
|
processBuffer = await decodeToSharpCompat(processBuffer, validation.format);
|
||||||
|
} catch {
|
||||||
|
await sharp(processBuffer).metadata();
|
||||||
|
}
|
||||||
const ext = processFilename.match(/\.[^.]+$/)?.[0];
|
const ext = processFilename.match(/\.[^.]+$/)?.[0];
|
||||||
if (ext) processFilename = `${processFilename.slice(0, -ext.length)}.png`;
|
if (ext) processFilename = `${processFilename.slice(0, -ext.length)}.png`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,18 +155,29 @@ export async function fileRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
if (needsCliDecode(validation.format)) {
|
if (needsCliDecode(validation.format)) {
|
||||||
try {
|
try {
|
||||||
buffer = await decodeToSharpCompat(buffer, validation.format);
|
buffer = await decodeToSharpCompat(buffer, validation.format);
|
||||||
|
} catch {
|
||||||
|
// CLI decoder unavailable -- try Sharp directly as fallback for preview
|
||||||
|
try {
|
||||||
|
await sharp(buffer).metadata();
|
||||||
} catch {
|
} catch {
|
||||||
return reply.status(422).send({
|
return reply.status(422).send({
|
||||||
error: `Failed to decode ${validation.format.toUpperCase()} file`,
|
error: `Failed to decode ${validation.format.toUpperCase()} file`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const webp = await sharp(buffer)
|
const webp = await sharp(buffer)
|
||||||
.resize(1200, 1200, { fit: "inside", withoutEnlargement: true })
|
.resize(1200, 1200, { fit: "inside", withoutEnlargement: true })
|
||||||
.webp({ quality: 80 })
|
.webp({ quality: 80 })
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
return reply.header("Content-Type", "image/webp").send(webp);
|
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,8 +198,9 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
|||||||
try {
|
try {
|
||||||
const fileExt = filename.split(".").pop()?.toLowerCase();
|
const fileExt = filename.split(".").pop()?.toLowerCase();
|
||||||
fileBuffer = await decodeToSharpCompat(fileBuffer, validation.format, fileExt);
|
fileBuffer = await decodeToSharpCompat(fileBuffer, validation.format, fileExt);
|
||||||
const ext = filename.match(/\.[^.]+$/)?.[0];
|
} catch {
|
||||||
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
|
try {
|
||||||
|
await sharp(fileBuffer).metadata();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return reply.status(422).send({
|
return reply.status(422).send({
|
||||||
error: `Failed to decode ${validation.format.toUpperCase()} file`,
|
error: `Failed to decode ${validation.format.toUpperCase()} file`,
|
||||||
@@ -207,6 +208,9 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const ext = filename.match(/\.[^.]+$/)?.[0];
|
||||||
|
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
|
||||||
|
}
|
||||||
|
|
||||||
// Sanitize SVG input to prevent XXE, SSRF, and script injection
|
// Sanitize SVG input to prevent XXE, SSRF, and script injection
|
||||||
const isSvg = validation.format === "svg";
|
const isSvg = validation.format === "svg";
|
||||||
|
|||||||
Reference in New Issue
Block a user