fix: ICO needs CLI decode, AVIF compress missing options, remove JXL output

- ICO: Sharp cannot decode ICO files. Added ImageMagick-based ICO decoder
  that extracts the largest embedded image. Added ICO to CLI_DECODED_FORMATS
  and SERVER_PREVIEW_EXTENSIONS. Removed from BROWSER_PREVIEWABLE sets.
- AVIF compress: Sharp's AVIF encoder requires effort option. Added
  formatOpts() helper that supplies effort:4 for AVIF format.
- JXL output: Docker's bundled libvips lacks the JXL encoder plugin.
  Removed JXL as a convert output target to avoid guaranteed failures.
  JXL remains fully supported as an input format.
This commit is contained in:
ashim-hq
2026-04-21 10:20:08 +08:00
parent 2df0fe026e
commit dc9160746e
9 changed files with 84 additions and 14 deletions
+1 -1
View File
@@ -67,7 +67,7 @@ export interface ValidationError {
const RAW_EXTENSIONS = new Set(["dng", "cr2", "nef", "arw", "orf", "rw2"]);
/** Formats that Sharp cannot decode natively — skip dimension check. */
const CLI_DECODED_FORMATS = new Set(["raw", "tga", "psd", "exr", "hdr"]);
const CLI_DECODED_FORMATS = new Set(["raw", "ico", "tga", "psd", "exr", "hdr"]);
/**
* Check whether a file extension corresponds to a Camera RAW format.
+26 -1
View File
@@ -8,7 +8,7 @@ import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
/** Formats that need external CLI tools (not decodable by Sharp). */
const CLI_DECODED_FORMATS = new Set(["raw", "tga", "psd", "exr", "hdr"]);
const CLI_DECODED_FORMATS = new Set(["raw", "ico", "tga", "psd", "exr", "hdr"]);
export function needsCliDecode(format: string): boolean {
return CLI_DECODED_FORMATS.has(format);
@@ -22,6 +22,8 @@ export async function decodeToSharpCompat(buffer: Buffer, format: string): Promi
switch (format) {
case "raw":
return decodeRaw(buffer);
case "ico":
return decodeIco(buffer);
case "psd":
return decodePsd(buffer);
case "tga":
@@ -57,6 +59,29 @@ function magickArgs(cmd: string, args: string[]): string[] {
return cmd === "magick" ? ["convert", ...args] : args;
}
// ── ICO decoder ────────────────────────────────────────────────
async function decodeIco(buffer: Buffer): Promise<Buffer> {
const cmd = await findMagickCmd();
const id = randomUUID();
const inputPath = join(tmpdir(), `ico-in-${id}.ico`);
const outputPath = join(tmpdir(), `ico-out-${id}.png`);
try {
await writeFile(inputPath, buffer);
// ICO contains multiple sizes; extract the largest by sorting
await execFileAsync(
cmd,
magickArgs(cmd, [`${inputPath}[-1]`, `png:${outputPath}`]),
{ timeout: 120_000 },
);
return await readFile(outputPath);
} finally {
await rm(inputPath, { force: true }).catch(() => {});
await rm(outputPath, { force: true }).catch(() => {});
}
}
// ── RAW decoder (ImageMagick with LibRaw delegate) ─────────────
async function decodeRaw(buffer: Buffer): Promise<Buffer> {
-1
View File
@@ -295,7 +295,6 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
"image/svg+xml",
"image/bmp",
"image/avif",
"image/x-icon",
]);
let previewUrl: string | undefined;
if (!BROWSER_PREVIEWABLE.has(result.contentType)) {
+1 -2
View File
@@ -16,11 +16,10 @@ const FORMAT_CONTENT_TYPES: Record<string, string> = {
gif: "image/gif",
heic: "image/heic",
heif: "image/heif",
jxl: "image/jxl",
};
const settingsSchema = z.object({
format: z.enum(["jpg", "png", "webp", "avif", "tiff", "gif", "heic", "heif", "jxl"]),
format: z.enum(["jpg", "png", "webp", "avif", "tiff", "gif", "heic", "heif"]),
quality: z.number().min(1).max(100).optional(),
});