Files
SnapOtter/apps/api/src/routes/tools/compose.ts
T
Siddharth Kumar Sah 80e536bcf8 chore: remove dead code, add test infrastructure, update docs
- Delete 3 dead files: use-batch-processor.ts, use-i18n.ts, smart-crop.ts (AI package)
- Remove dead getJobProgress function and unused runPythonScript wrapper
- Remove 6 unused imports across API and web apps
- Remove unused shared types (ImageFormat, AppConfig, ApiError, HealthResponse, JobProgress)
  and constants (SUPPORTED_INPUT_FORMATS/OUTPUT_FORMATS, DEFAULT_OUTPUT_FORMAT)
- Remove unused store method (setOriginalBlobUrl) and clean AI package re-exports
- Add test infrastructure: vitest config, unit/integration/e2e tests, fixtures, screenshots
- Add Docker test infrastructure: Dockerfile.test, docker-compose.test.yml
- Add download_models.py for pre-baking AI model weights in Docker
- Add filename sanitization utility (apps/api/src/lib/filename.ts)
- Update .gitignore to exclude coverage/, *.tsbuildinfo, .superpowers/, test artifacts
- Update .dockerignore to exclude test/coverage/IDE artifacts from builds
- Update docs: remove smart crop from AI docs (uses Sharp directly), update bridge docs
2026-03-23 11:46:45 +08:00

132 lines
4.3 KiB
TypeScript

import { z } from "zod";
import sharp from "sharp";
import type { FastifyInstance } from "fastify";
import { randomUUID } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createWorkspace } from "../../lib/workspace.js";
import { sanitizeFilename } from "../../lib/filename.js";
const settingsSchema = z.object({
x: z.number().min(0).default(0),
y: z.number().min(0).default(0),
opacity: z.number().min(0).max(100).default(100),
blendMode: z
.enum([
"over", "multiply", "screen", "overlay",
"darken", "lighten", "hard-light", "soft-light",
"difference", "exclusion",
])
.default("over"),
});
export function registerCompose(app: FastifyInstance) {
app.post(
"/api/v1/tools/compose",
async (request, reply) => {
let baseBuffer: Buffer | null = null;
let overlayBuffer: Buffer | null = null;
let filename = "image";
let settingsRaw: string | null = null;
try {
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file") {
const chunks: Buffer[] = [];
for await (const chunk of part.file) {
chunks.push(chunk);
}
const buf = Buffer.concat(chunks);
if (part.fieldname === "overlay") {
overlayBuffer = buf;
} else {
baseBuffer = buf;
filename = sanitizeFilename(part.filename ?? "image");
}
} else if (part.fieldname === "settings") {
settingsRaw = part.value as string;
}
}
} catch (err) {
return reply.status(400).send({
error: "Failed to parse multipart request",
details: err instanceof Error ? err.message : String(err),
});
}
if (!baseBuffer || baseBuffer.length === 0) {
return reply.status(400).send({ error: "No base image provided" });
}
if (!overlayBuffer || overlayBuffer.length === 0) {
return reply.status(400).send({ error: "No overlay image provided" });
}
let settings: z.infer<typeof settingsSchema>;
try {
const parsed = settingsRaw ? JSON.parse(settingsRaw) : {};
const result = settingsSchema.safeParse(parsed);
if (!result.success) {
return reply.status(400).send({ error: "Invalid settings", details: result.error.issues });
}
settings = result.data;
} catch {
return reply.status(400).send({ error: "Settings must be valid JSON" });
}
try {
// Apply opacity to overlay if needed
let processedOverlay = overlayBuffer;
if (settings.opacity < 100) {
const overlayImg = sharp(overlayBuffer).ensureAlpha();
const overlayBuf = await overlayImg.toBuffer();
const overlayMeta = await sharp(overlayBuf).metadata();
const oW = overlayMeta.width ?? 100;
const oH = overlayMeta.height ?? 100;
const opacityMask = await sharp({
create: {
width: oW,
height: oH,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: settings.opacity / 100 },
},
})
.png()
.toBuffer();
processedOverlay = await sharp(overlayBuf)
.composite([{ input: opacityMask, blend: "dest-in" }])
.toBuffer();
}
const result = await sharp(baseBuffer)
.composite([{
input: processedOverlay,
top: settings.y,
left: settings.x,
blend: settings.blendMode as import("sharp").Blend,
}])
.toBuffer();
const jobId = randomUUID();
const workspacePath = await createWorkspace(jobId);
const outputPath = join(workspacePath, "output", filename);
await writeFile(outputPath, result);
return reply.send({
jobId,
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(filename)}`,
originalSize: baseBuffer.length,
processedSize: result.length,
});
} catch (err) {
return reply.status(422).send({
error: "Processing failed",
details: err instanceof Error ? err.message : "Image processing failed",
});
}
},
);
}