feat(api): add resize, crop, rotate, convert, compress, metadata, and color tool routes

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.
This commit is contained in:
Siddharth Kumar Sah
2026-03-22 03:56:34 +08:00
parent d53f6733b8
commit 37112af779
10 changed files with 313 additions and 10 deletions
+37
View File
@@ -0,0 +1,37 @@
import { z } from "zod";
import { createToolRoute } from "../tool-factory.js";
import { compress } from "@stirling-image/image-engine";
import sharp from "sharp";
import type { FastifyInstance } from "fastify";
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 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: "image/jpeg" };
},
});
}