mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Seven tool route files using the createToolRoute factory, registering 10 API endpoints total (color adjustments covers 4 tool IDs). Also fixes the tool factory generic to properly infer Zod output types and clamps the compress binary-search quality to 1-100.
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { z } from "zod";
|
|
import { createToolRoute } from "../tool-factory.js";
|
|
import { convert } from "@stirling-image/image-engine";
|
|
import sharp from "sharp";
|
|
import type { FastifyInstance } from "fastify";
|
|
import { extname } from "node:path";
|
|
|
|
const FORMAT_CONTENT_TYPES: Record<string, string> = {
|
|
jpg: "image/jpeg",
|
|
png: "image/png",
|
|
webp: "image/webp",
|
|
avif: "image/avif",
|
|
tiff: "image/tiff",
|
|
gif: "image/gif",
|
|
};
|
|
|
|
const settingsSchema = z.object({
|
|
format: z.enum(["jpg", "png", "webp", "avif", "tiff", "gif"]),
|
|
quality: z.number().min(1).max(100).optional(),
|
|
});
|
|
|
|
export function registerConvert(app: FastifyInstance) {
|
|
createToolRoute(app, {
|
|
toolId: "convert",
|
|
settingsSchema,
|
|
process: async (inputBuffer, settings, filename) => {
|
|
const image = sharp(inputBuffer);
|
|
const result = await convert(image, settings);
|
|
const buffer = await result.toBuffer();
|
|
|
|
// Change filename extension to match the output format
|
|
const ext = extname(filename);
|
|
const baseName = ext ? filename.slice(0, -ext.length) : filename;
|
|
const outputFilename = `${baseName}.${settings.format}`;
|
|
|
|
const contentType =
|
|
FORMAT_CONTENT_TYPES[settings.format] || "application/octet-stream";
|
|
|
|
return { buffer, filename: outputFilename, contentType };
|
|
},
|
|
});
|
|
}
|