mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
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({
|
|
mode: z.enum(["quality", "targetSize"]).default("quality"),
|
|
quality: z.number().min(1).max(100).optional(),
|
|
targetSizeKb: z.number().positive().optional(),
|
|
});
|
|
|
|
export function registerCompress(app: FastifyInstance) {
|
|
createToolRoute(app, {
|
|
toolId: "compress",
|
|
settingsSchema,
|
|
process: async (inputBuffer, settings, filename) => {
|
|
const image = sharp(inputBuffer);
|
|
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
|
|
|
|
const compressOptions: {
|
|
quality?: number;
|
|
targetSizeBytes?: number;
|
|
} = {};
|
|
|
|
if (settings.mode === "targetSize" && settings.targetSizeKb) {
|
|
// Convert KB to bytes for the engine
|
|
compressOptions.targetSizeBytes = settings.targetSizeKb * 1024;
|
|
} else {
|
|
compressOptions.quality = settings.quality ?? 80;
|
|
}
|
|
|
|
const result = await compress(image, compressOptions);
|
|
const buffer = await result.toBuffer();
|
|
return { buffer, filename, contentType: outputFormat.contentType };
|
|
},
|
|
});
|
|
}
|