Files
SnapOtter/apps/api/src/routes/tools/rotate.ts
T
Siddharth Kumar Sah 37112af779 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.
2026-03-22 03:56:34 +08:00

38 lines
1.0 KiB
TypeScript

import { z } from "zod";
import { createToolRoute } from "../tool-factory.js";
import { rotate, flip } from "@stirling-image/image-engine";
import sharp from "sharp";
import type { FastifyInstance } from "fastify";
const settingsSchema = z.object({
angle: z.number().default(0),
horizontal: z.boolean().default(false),
vertical: z.boolean().default(false),
});
export function registerRotate(app: FastifyInstance) {
createToolRoute(app, {
toolId: "rotate",
settingsSchema,
process: async (inputBuffer, settings, filename) => {
let image = sharp(inputBuffer);
// Apply rotation first
if (settings.angle !== 0) {
image = await rotate(image, { angle: settings.angle });
}
// Then apply flip/flop
if (settings.horizontal || settings.vertical) {
image = await flip(image, {
horizontal: settings.horizontal,
vertical: settings.vertical,
});
}
const buffer = await image.toBuffer();
return { buffer, filename, contentType: "image/png" };
},
});
}