fix: support AVIF compress and fix hardcoded content type

This commit is contained in:
ashim-hq
2026-04-20 22:13:53 +08:00
parent 491e6fb554
commit c738f16107
4 changed files with 6 additions and 1 deletions
+1
View File
@@ -17,6 +17,7 @@ const FORMAT_MAP: Record<
gif: { format: "gif", extension: "gif", contentType: "image/gif" },
tiff: { format: "tiff", extension: "tiff", contentType: "image/tiff" },
avif: { format: "avif", extension: "avif", contentType: "image/avif" },
heif: { format: "avif", extension: "avif", contentType: "image/avif" },
};
const DEFAULT_QUALITY = 95;
+3 -1
View File
@@ -2,6 +2,7 @@ import { compress } from "@ashim/image-engine";
import type { FastifyInstance } from "fastify";
import sharp from "sharp";
import { z } from "zod";
import { resolveOutputFormat } from "../../lib/output-format.js";
import { createToolRoute } from "../tool-factory.js";
const settingsSchema = z.object({
@@ -16,6 +17,7 @@ export function registerCompress(app: FastifyInstance) {
settingsSchema,
process: async (inputBuffer, settings, filename) => {
const image = sharp(inputBuffer);
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
const compressOptions: {
quality?: number;
@@ -31,7 +33,7 @@ export function registerCompress(app: FastifyInstance) {
const result = await compress(image, compressOptions);
const buffer = await result.toBuffer();
return { buffer, filename, contentType: "image/jpeg" };
return { buffer, filename, contentType: outputFormat.contentType };
},
});
}
+1
View File
@@ -70,6 +70,7 @@ const FORMAT_MAP: Record<string, string> = {
png: "png",
webp: "webp",
avif: "avif",
heif: "avif",
tiff: "tiff",
gif: "gif",
};
@@ -6,6 +6,7 @@ const FORMAT_MAP: Record<string, string> = {
png: "png",
webp: "webp",
avif: "avif",
heif: "avif",
tiff: "tiff",
gif: "gif",
};