2026-03-22 04:31:49 +08:00
|
|
|
import { randomUUID } from "node:crypto";
|
|
|
|
|
import { writeFile } from "node:fs/promises";
|
2026-03-25 09:27:12 +08:00
|
|
|
import { basename, join } from "node:path";
|
2026-04-24 18:02:21 +08:00
|
|
|
import { blurFaces } from "@snapotter/ai";
|
|
|
|
|
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@snapotter/shared";
|
2026-03-25 09:27:12 +08:00
|
|
|
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
2026-04-26 03:22:26 +08:00
|
|
|
import sharp from "sharp";
|
2026-03-28 15:09:21 +08:00
|
|
|
import { z } from "zod";
|
2026-03-26 16:01:56 +08:00
|
|
|
import { autoOrient } from "../../lib/auto-orient.js";
|
2026-04-23 20:26:58 +08:00
|
|
|
import { formatZodErrors } from "../../lib/errors.js";
|
2026-04-18 10:23:14 +08:00
|
|
|
import { isToolInstalled } from "../../lib/feature-status.js";
|
2026-03-25 09:27:12 +08:00
|
|
|
import { validateImageBuffer } from "../../lib/file-validation.js";
|
2026-04-21 09:59:57 +08:00
|
|
|
import { decodeToSharpCompat, needsCliDecode } from "../../lib/format-decoders.js";
|
2026-04-19 19:52:23 +08:00
|
|
|
import { decodeHeic, ensureSharpCompat } from "../../lib/heic-converter.js";
|
2026-04-26 03:22:26 +08:00
|
|
|
import { resolveOutputFormat } from "../../lib/output-format.js";
|
2026-03-22 04:31:49 +08:00
|
|
|
import { createWorkspace } from "../../lib/workspace.js";
|
2026-03-23 01:41:13 +08:00
|
|
|
import { updateSingleFileProgress } from "../progress.js";
|
2026-03-28 15:09:21 +08:00
|
|
|
import { registerToolProcessFn } from "../tool-factory.js";
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-04-23 20:26:58 +08:00
|
|
|
const settingsSchema = z.object({
|
|
|
|
|
blurRadius: z.number().min(1).max(100).default(30),
|
|
|
|
|
sensitivity: z.number().min(0).max(1).default(0.5),
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-06 22:00:48 +08:00
|
|
|
/** Face detection and blurring route. */
|
2026-03-22 04:31:49 +08:00
|
|
|
export function registerBlurFaces(app: FastifyInstance) {
|
2026-03-25 09:27:12 +08:00
|
|
|
app.post("/api/v1/tools/blur-faces", async (request: FastifyRequest, reply: FastifyReply) => {
|
2026-04-18 10:23:14 +08:00
|
|
|
const toolId = "blur-faces";
|
|
|
|
|
if (!isToolInstalled(toolId)) {
|
|
|
|
|
const bundle = getBundleForTool(toolId);
|
|
|
|
|
return reply.status(501).send({
|
|
|
|
|
error: "Feature not installed",
|
|
|
|
|
code: "FEATURE_NOT_INSTALLED",
|
|
|
|
|
feature: TOOL_BUNDLE_MAP[toolId],
|
|
|
|
|
featureName: bundle?.name ?? toolId,
|
|
|
|
|
estimatedSize: bundle?.estimatedSize ?? "unknown",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
let fileBuffer: Buffer | null = null;
|
|
|
|
|
let filename = "image";
|
|
|
|
|
let settingsRaw: string | null = null;
|
|
|
|
|
let clientJobId: string | null = null;
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
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);
|
2026-03-22 04:31:49 +08:00
|
|
|
}
|
2026-03-25 09:27:12 +08:00
|
|
|
fileBuffer = Buffer.concat(chunks);
|
|
|
|
|
filename = basename(part.filename ?? "image");
|
|
|
|
|
} else if (part.fieldname === "settings") {
|
|
|
|
|
settingsRaw = part.value as string;
|
|
|
|
|
} else if (part.fieldname === "clientJobId") {
|
|
|
|
|
clientJobId = part.value as string;
|
2026-03-22 04:31:49 +08:00
|
|
|
}
|
2026-03-25 09:27:12 +08:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return reply.status(400).send({
|
|
|
|
|
error: "Failed to parse multipart request",
|
|
|
|
|
details: err instanceof Error ? err.message : String(err),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fileBuffer || fileBuffer.length === 0) {
|
|
|
|
|
return reply.status(400).send({ error: "No image file provided" });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 09:59:57 +08:00
|
|
|
const validation = await validateImageBuffer(fileBuffer, filename);
|
2026-03-25 09:27:12 +08:00
|
|
|
if (!validation.valid) {
|
|
|
|
|
return reply.status(400).send({ error: `Invalid image: ${validation.reason}` });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-04-23 20:26:58 +08:00
|
|
|
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: formatZodErrors(result.error.issues) });
|
|
|
|
|
}
|
|
|
|
|
settings = result.data;
|
|
|
|
|
} catch {
|
|
|
|
|
return reply.status(400).send({ error: "Settings must be valid JSON" });
|
|
|
|
|
}
|
2026-04-19 19:52:23 +08:00
|
|
|
|
|
|
|
|
if (validation.format === "heif") {
|
|
|
|
|
fileBuffer = await decodeHeic(fileBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 09:59:57 +08:00
|
|
|
// Decode CLI-decoded formats (RAW, TGA, PSD, EXR, HDR)
|
|
|
|
|
if (needsCliDecode(validation.format)) {
|
|
|
|
|
fileBuffer = await decodeToSharpCompat(fileBuffer, validation.format);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 20:26:58 +08:00
|
|
|
const { blurRadius, sensitivity } = settings;
|
2026-04-06 21:00:29 +08:00
|
|
|
request.log.info(
|
|
|
|
|
{
|
|
|
|
|
toolId: "blur-faces",
|
|
|
|
|
imageSize: fileBuffer.length,
|
2026-04-23 20:26:58 +08:00
|
|
|
blurRadius,
|
|
|
|
|
sensitivity,
|
2026-04-06 21:00:29 +08:00
|
|
|
},
|
|
|
|
|
"Starting face blur",
|
|
|
|
|
);
|
2026-03-26 16:01:56 +08:00
|
|
|
|
|
|
|
|
fileBuffer = await autoOrient(fileBuffer);
|
|
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
const jobId = randomUUID();
|
|
|
|
|
const workspacePath = await createWorkspace(jobId);
|
|
|
|
|
|
|
|
|
|
// Save input
|
|
|
|
|
const inputPath = join(workspacePath, "input", filename);
|
|
|
|
|
await writeFile(inputPath, fileBuffer);
|
|
|
|
|
|
|
|
|
|
// Process
|
2026-03-26 01:10:13 +08:00
|
|
|
const jobIdForProgress = clientJobId;
|
|
|
|
|
const onProgress = jobIdForProgress
|
2026-03-25 09:27:12 +08:00
|
|
|
? (percent: number, stage: string) => {
|
|
|
|
|
updateSingleFileProgress({
|
2026-03-26 01:10:13 +08:00
|
|
|
jobId: jobIdForProgress,
|
2026-03-25 09:27:12 +08:00
|
|
|
phase: "processing",
|
|
|
|
|
stage,
|
|
|
|
|
percent,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
const result = await blurFaces(
|
|
|
|
|
fileBuffer,
|
|
|
|
|
join(workspacePath, "output"),
|
|
|
|
|
{
|
2026-04-23 20:26:58 +08:00
|
|
|
blurRadius,
|
|
|
|
|
sensitivity,
|
2026-03-25 09:27:12 +08:00
|
|
|
},
|
|
|
|
|
onProgress,
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-26 03:22:26 +08:00
|
|
|
// Resolve output format to match input
|
|
|
|
|
const outputFormat = await resolveOutputFormat(fileBuffer, filename);
|
|
|
|
|
let outputBuffer = result.buffer;
|
|
|
|
|
if (outputFormat.format !== "png") {
|
|
|
|
|
outputBuffer = await sharp(result.buffer)
|
|
|
|
|
.toFormat(outputFormat.format, { quality: outputFormat.quality })
|
|
|
|
|
.toBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ext = outputFormat.format === "jpeg" ? "jpg" : outputFormat.format;
|
|
|
|
|
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_blurred.${ext}`;
|
2026-03-25 09:27:12 +08:00
|
|
|
const outputPath = join(workspacePath, "output", outputFilename);
|
2026-04-26 03:22:26 +08:00
|
|
|
await writeFile(outputPath, outputBuffer);
|
2026-03-25 09:27:12 +08:00
|
|
|
|
|
|
|
|
if (clientJobId) {
|
|
|
|
|
updateSingleFileProgress({
|
|
|
|
|
jobId: clientJobId,
|
|
|
|
|
phase: "complete",
|
|
|
|
|
percent: 100,
|
2026-03-22 04:31:49 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 09:27:12 +08:00
|
|
|
return reply.send({
|
|
|
|
|
jobId,
|
|
|
|
|
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(outputFilename)}`,
|
|
|
|
|
originalSize: fileBuffer.length,
|
2026-04-26 03:22:26 +08:00
|
|
|
processedSize: outputBuffer.length,
|
2026-03-25 09:27:12 +08:00
|
|
|
facesDetected: result.facesDetected,
|
|
|
|
|
faces: result.faces,
|
2026-04-19 19:52:23 +08:00
|
|
|
...(result.facesDetected === 0 && {
|
|
|
|
|
warning: "No faces detected in this image. Try increasing detection sensitivity.",
|
|
|
|
|
}),
|
2026-03-25 09:27:12 +08:00
|
|
|
});
|
|
|
|
|
} catch (err) {
|
2026-04-06 21:00:29 +08:00
|
|
|
request.log.error({ err, toolId: "blur-faces" }, "Face blur failed");
|
2026-03-25 09:27:12 +08:00
|
|
|
return reply.status(422).send({
|
|
|
|
|
error: "Face blur failed",
|
|
|
|
|
details: err instanceof Error ? err.message : "Unknown error",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-03-28 15:09:21 +08:00
|
|
|
|
|
|
|
|
// Register in the pipeline/batch registry so this tool can be used
|
|
|
|
|
// as a step in automation pipelines (without progress callbacks).
|
|
|
|
|
registerToolProcessFn({
|
|
|
|
|
toolId: "blur-faces",
|
|
|
|
|
settingsSchema: z.object({
|
|
|
|
|
blurRadius: z.number().min(1).max(100).default(30),
|
|
|
|
|
sensitivity: z.number().min(0).max(1).default(0.5),
|
|
|
|
|
}),
|
|
|
|
|
process: async (inputBuffer, settings, filename) => {
|
|
|
|
|
const s = settings as { blurRadius?: number; sensitivity?: number };
|
2026-04-19 19:52:23 +08:00
|
|
|
const orientedBuffer = await autoOrient(await ensureSharpCompat(inputBuffer));
|
2026-03-28 15:09:21 +08:00
|
|
|
const jobId = randomUUID();
|
|
|
|
|
const workspacePath = await createWorkspace(jobId);
|
|
|
|
|
const result = await blurFaces(orientedBuffer, join(workspacePath, "output"), {
|
|
|
|
|
blurRadius: s.blurRadius ?? 30,
|
|
|
|
|
sensitivity: s.sensitivity ?? 0.5,
|
|
|
|
|
});
|
2026-04-26 03:22:26 +08:00
|
|
|
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
|
|
|
|
|
let outputBuffer = result.buffer;
|
|
|
|
|
if (outputFormat.format !== "png") {
|
|
|
|
|
outputBuffer = await sharp(result.buffer)
|
|
|
|
|
.toFormat(outputFormat.format, { quality: outputFormat.quality })
|
|
|
|
|
.toBuffer();
|
|
|
|
|
}
|
|
|
|
|
const ext = outputFormat.format === "jpeg" ? "jpg" : outputFormat.format;
|
|
|
|
|
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_blurred.${ext}`;
|
|
|
|
|
return {
|
|
|
|
|
buffer: outputBuffer,
|
|
|
|
|
filename: outputFilename,
|
|
|
|
|
contentType: outputFormat.contentType,
|
|
|
|
|
};
|
2026-03-28 15:09:21 +08:00
|
|
|
},
|
|
|
|
|
});
|
2026-03-22 04:31:49 +08:00
|
|
|
}
|