Files
SnapOtter/apps/api/src/routes/tools/remove-background.ts
T
SnapOtterandGitHub 08961fcc89 fix: PDF tool QA sweep - library auto-save versioning, AI fileId threading, modality polish (#251)
* fix(pdf): never enlarge on compress, honor redact case, hide same-format convert

- compress-pdf: guard both modes so output is never larger than the input; low-DPI scans could be upsampled and grow. Falls back to the original bytes.

- doc_redact.py: caseSensitive=true now filters PyMuPDF's case-insensitive search to exact-case hits, so the toggle works instead of always over-redacting.

- convert-{document,presentation,spreadsheet}: omit the input's own format from the output dropdown; the backend already rejects same-format conversions.

Verified end-to-end against an isolated Docker stack during a full visual QA sweep of all 37 PDF tools.

* fix(ui): show real multi-file preview thumbnails per modality

The bottom multi-file preview strip rendered a raw <img src=blobUrl> for every file, so audio/video/PDF inputs showed a broken-image icon plus the filename. ThumbnailStrip now branches on FileEntry.previewKind: images use <img> (icon fallback on error), video shows a captured first frame, PDF shows a pdf.js page-1 render, and audio/other show a type icon + extension. Fixes the multi-file preview across all modalities.

Verified in the browser for image/PDF/audio/video.

* fix(modality): make pipeline, batch validation, save/upload, previews & UI modality-aware

The app grew up image-only; several paths still assumed image. They now dispatch on the tool/file modality (image/video/audio/document/file):

- pipeline /execute + /batch: validate+decode input via inputHandlerFor(modality) instead of validateImageBuffer, so PDF/audio/video/data pipelines work (were rejected 'Invalid image').

- batch: non-image inputs now get per-modality validation (ffprobe/qpdf) before the worker instead of passing through unchecked.

- files /upload, user-files /save-result + /thumbnail: accept non-image files (MIME from extension; video-poster / pdf-first-page thumbnails).

- postprocess CONTENT_TYPE_TO_EXT: cover video/audio/pdf/text/zip so output extensions are corrected for all modalities.

- worker pipeline-finalize: attach result payload to the complete SSE event so the sync-window-timeout fallback still delivers a download.

- frontend: batch-ZIP blob MIME by extension (not svg-only); modality-neutral fallback labels/filenames; 'smaller file' not 'smaller image'.

Found via a codebase-wide image-only-assumption audit. Verified: PDF/audio/video pipelines + batch now work; image paths unchanged. canBrowserPreview kept image-only by design (non-image is rendered by dedicated displayMode viewers).

* fix(pipeline): generate a modality-aware preview for pipeline results

processPipelineFinalize now derives the output content type from its extension and runs generatePreview (video poster / pdf first page / image thumb), sets previewRef on the result, and surfaces previewUrl in the /execute sync response and the SSE complete event (via buildLegacyResultPayload). Pipeline outputs get a preview like single-tool results instead of always returning previewUrl: undefined.

Verified: PDF pipeline -> previewUrl returns a valid PNG first-page render; png pipeline correctly has no previewUrl; audio/video/multi-step pipelines all 200.

* fix(worker): auto-save a new library version when processing a library file

The worker hardcoded savedFileId = undefined ('No auto-save') even though the whole versioning feature was wired around it: the frontend sends fileId for library files and reads result.savedFileId, tool-factory threads fileId into ToolJobData, and autoSaveToLibrary implements the new-version save -- but the worker never called it (dead code from the tool-first-workflow merge). processToolJob now calls autoSaveToLibrary with data.fileId; without a fileId it is a no-op, so tool-first uploads are unchanged.

Verified: processing a library PDF with fileId creates version 2 (parent linked, toolChain appended, savedFileId returned); processing without fileId saves nothing.

* fix(library): ownership check + modality-aware dimensions in autoSaveToLibrary

- Only create a new version when the requester owns the parent (parent.userId === opts.userId); prevents versioning another user's file via a known fileId.

- Dimensions are modality-aware: sharp for images, ffprobe (probeMedia) for video, null for audio/document. Previously sharp-only, so non-image versions always got null dims.

* fix(ai): thread fileId + real userId through the 16 AI tool routes

AI custom routes parsed neither the fileId multipart field nor the authenticated user (they hardcoded userId: null), so processing a library file via an AI tool never created a new version, and AI jobs were unattributed. Each route now parses fileId like clientJobId and passes getAuthUser(request)?.id as userId to enqueueToolJob.

Verified: ocr-pdf on a library PDF creates a new version (v2); the ownership check still denies cross-user versioning.
2026-06-16 15:48:07 +08:00

392 lines
15 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { mkdir, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { removeBackground } from "@snapotter/ai";
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@snapotter/shared";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { z } from "zod";
import { registerAiJobHandler } from "../../jobs/ai-handlers.js";
import { enqueueToolJob } from "../../jobs/enqueue.js";
import { autoOrient } from "../../lib/auto-orient.js";
import {
applyEffects,
BG_FORMAT_CONTENT_TYPES,
type BgOutputFormat,
} from "../../lib/bg-effects.js";
import { formatZodErrors, stripInternalPaths } from "../../lib/errors.js";
import { isToolInstalled } from "../../lib/feature-status.js";
import { validateImageBuffer } from "../../lib/file-validation.js";
import { sanitizeFilename } from "../../lib/filename.js";
import { decodeToSharpCompat, needsCliDecode } from "../../lib/format-decoders.js";
import { decodeHeic } from "../../lib/heic-converter.js";
import { getObjectBuffer, putObject } from "../../lib/object-storage.js";
import { receiveUpload } from "../../lib/upload-stream.js";
import { getAuthUser } from "../../plugins/auth.js";
import { registerToolProcessFn } from "../tool-factory.js";
const settingsSchema = z.object({
model: z.string().optional(),
backgroundType: z.enum(["transparent", "color", "gradient", "blur", "image"]).optional(),
backgroundColor: z.string().optional(),
gradientColor1: z.string().optional(),
gradientColor2: z.string().optional(),
gradientAngle: z.number().optional(),
blurEnabled: z.boolean().optional(),
blurIntensity: z.number().min(0).max(100).optional(),
shadowEnabled: z.boolean().optional(),
shadowOpacity: z.number().min(0).max(100).optional(),
outputFormat: z.enum(["png", "webp", "avif"]).optional(),
edgeRefine: z.number().int().min(0).max(3).optional(),
decontaminate: z.boolean().optional(),
});
// ── AI job handler (runs inside the BullMQ worker) ────────────────
registerAiJobHandler("remove-background", async (input, data, ctx) => {
const settings = settingsSchema.parse(data.settings);
// Phase 1: AI background removal -> transparent PNG
const transparentResult = await removeBackground(
input,
ctx.scratchDir,
{
model: settings.model,
edgeRefine: settings.edgeRefine,
decontaminate: settings.decontaminate,
},
(percent, stage) => ctx.report(percent, stage),
);
// The mask IS the transparent result; cache original for effects re-apply
const maskFilename = `${data.filename.replace(/\.[^.]+$/, "")}_mask.png`;
const originalFilename = `${data.filename.replace(/\.[^.]+$/, "")}_original.png`;
const maskUrl = `/api/v1/download/${data.jobId}/${encodeURIComponent(maskFilename)}`;
const originalUrl = `/api/v1/download/${data.jobId}/${encodeURIComponent(originalFilename)}`;
return {
buffer: transparentResult,
filename: maskFilename,
contentType: "image/png",
resultPayload: {
maskUrl,
originalUrl,
filename: data.filename,
model: settings.model,
},
extraOutputs: [{ name: originalFilename, buffer: input, contentType: "image/png" }],
};
});
/**
* AI background removal with two-phase flow:
*
* Phase 1 (POST /remove-background): Python/rembg removes background.
* Returns transparent PNG + caches mask & original for effects re-apply.
* Also returns maskUrl and originalUrl for frontend CSS preview.
*
* Phase 2 (POST /remove-background/effects): Node.js/Sharp applies effects.
* Uses cached mask + original. No AI re-run. Instant response.
* Called when user adjusts blur/shadow/background and clicks download.
*/
export function registerRemoveBackground(app: FastifyInstance) {
// ── Phase 1: Background removal ──────────────────────────────────
app.post(
"/api/v1/tools/remove-background",
async (request: FastifyRequest, reply: FastifyReply) => {
const toolId = "remove-background";
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",
});
}
const userId = getAuthUser(request)?.id ?? null;
const jobId = randomUUID();
let fileBuffer: Buffer | null = null;
let filename = "image";
let settingsRaw: string | null = null;
let clientJobId: string | null = null;
let fileId: string | null = null;
let inputKey: string | null = null;
try {
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file") {
const upload = await receiveUpload(part, jobId);
inputKey = upload.key;
filename = upload.filename;
} else if (part.fieldname === "settings") {
settingsRaw = part.value as string;
} else if (part.fieldname === "clientJobId") {
const raw = part.value as string;
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(raw)) {
clientJobId = raw;
}
} else if (part.fieldname === "fileId") {
fileId = part.value as string;
}
}
} catch (err) {
return reply.status(400).send({
error: "Failed to parse multipart request",
details: stripInternalPaths(err instanceof Error ? err.message : String(err)),
});
}
if (!inputKey) {
return reply.status(400).send({ error: "No image file provided" });
}
fileBuffer = await getObjectBuffer(inputKey);
if (!fileBuffer || fileBuffer.length === 0) {
return reply.status(400).send({ error: "No image file provided" });
}
const validation = await validateImageBuffer(fileBuffer, filename);
if (!validation.valid) {
// Orphaned uploads/<jobId>/ dir will be cleaned by T10 TTL sweeper
return reply.status(400).send({ error: `Invalid image: ${validation.reason}` });
}
let settings: z.infer<typeof settingsSchema>;
try {
const parsed = settingsRaw ? JSON.parse(settingsRaw) : {};
const result = settingsSchema.safeParse(parsed);
if (!result.success) {
// Orphaned uploads/<jobId>/ dir will be cleaned by T10 TTL sweeper
return reply
.status(400)
.send({ error: "Invalid settings", details: formatZodErrors(result.error.issues) });
}
settings = result.data;
} catch {
// Orphaned uploads/<jobId>/ dir will be cleaned by T10 TTL sweeper
return reply.status(400).send({ error: "Settings must be valid JSON" });
}
try {
// Decode HEIC/HEIF before processing
if (validation.format === "heif") {
fileBuffer = await decodeHeic(fileBuffer);
const ext = filename.match(/\.[^.]+$/)?.[0];
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
}
// Decode CLI-decoded formats (RAW, TGA, PSD, EXR, HDR)
if (needsCliDecode(validation.format)) {
fileBuffer = await decodeToSharpCompat(fileBuffer, validation.format);
const ext = filename.match(/\.[^.]+$/)?.[0];
if (ext) filename = `${filename.slice(0, -ext.length)}.png`;
}
// Auto-orient to fix EXIF rotation
fileBuffer = await autoOrient(fileBuffer);
} catch (err) {
request.log.error({ err, toolId: "remove-background" }, "Input decoding failed");
return reply.status(422).send({
error: "Background removal failed",
details: stripInternalPaths(err instanceof Error ? err.message : "Unknown error"),
});
}
// Write decoded input for the worker
const decodedKey = `uploads/${jobId}/${filename}`;
if (decodedKey !== inputKey) {
await putObject(decodedKey, fileBuffer);
inputKey = decodedKey;
} else {
await putObject(inputKey, fileBuffer);
}
const progressJobId = clientJobId || jobId;
// Enqueue on the AI pool
await enqueueToolJob({
jobId,
toolId,
userId,
pool: "ai",
inputRefs: [inputKey],
filename,
settings,
clientJobId: clientJobId ?? undefined,
fileId: fileId ?? undefined,
kind: "ai-tool",
});
// AI tools always return 202 (no sync window)
return reply.status(202).send({ jobId: progressJobId, async: true });
},
);
// ── Phase 2: Effects-only (no AI re-run) ─────────────────────────
app.post(
"/api/v1/tools/remove-background/effects",
async (request: FastifyRequest, reply: FastifyReply) => {
let settingsRaw: string | null = null;
let bgImageBuffer: Buffer | null = null;
let bgFilename = "background";
try {
const parts = request.parts();
for await (const part of parts) {
if (part.type === "file" && part.fieldname === "backgroundImage") {
const chunks: Buffer[] = [];
for await (const chunk of part.file) chunks.push(chunk);
bgImageBuffer = Buffer.concat(chunks);
bgFilename = sanitizeFilename(part.filename ?? "background");
} else if (part.type === "field" && part.fieldname === "settings") {
settingsRaw = part.value as string;
}
}
} catch (err) {
return reply.status(400).send({
error: "Failed to parse request",
details: stripInternalPaths(err instanceof Error ? err.message : String(err)),
});
}
if (!settingsRaw) {
return reply.status(400).send({ error: "No settings provided" });
}
const effectsSchema = z.object({
jobId: z.string().min(1),
filename: z.string().min(1),
backgroundType: z.enum(["transparent", "color", "gradient", "blur", "image"]).optional(),
backgroundColor: z.string().optional(),
gradientColor1: z.string().optional(),
gradientColor2: z.string().optional(),
gradientAngle: z.number().optional(),
blurEnabled: z.boolean().optional(),
blurIntensity: z.number().min(0).max(100).optional(),
shadowEnabled: z.boolean().optional(),
shadowOpacity: z.number().min(0).max(100).optional(),
outputFormat: z.enum(["png", "webp", "avif"]).optional(),
});
try {
let settings: z.infer<typeof effectsSchema>;
try {
const parsed = JSON.parse(settingsRaw);
const result = effectsSchema.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" });
}
const { jobId, filename } = settings;
const baseName = filename.replace(/\.[^.]+$/, "");
const maskKey = `outputs/${jobId}/${baseName}_mask.png`;
const originalKey = `outputs/${jobId}/${baseName}_original.png`;
const [maskBuffer, originalBuffer] = await Promise.all([
getObjectBuffer(maskKey),
getObjectBuffer(originalKey),
]);
// Decode HEIC/HEIF background image if needed
if (bgImageBuffer) {
const bgValidation = await validateImageBuffer(bgImageBuffer, bgFilename);
if (bgValidation.valid && bgValidation.format === "heif") {
bgImageBuffer = await decodeHeic(bgImageBuffer);
}
if (bgValidation.valid && needsCliDecode(bgValidation.format)) {
bgImageBuffer = await decodeToSharpCompat(bgImageBuffer, bgValidation.format);
}
}
// Apply effects using cached mask + original
const fmt = (settings.outputFormat ?? "png") as BgOutputFormat;
const resultBuffer = await applyEffects(maskBuffer, originalBuffer, {
backgroundType: settings.backgroundType,
backgroundColor: settings.backgroundColor,
gradientColor1: settings.gradientColor1,
gradientColor2: settings.gradientColor2,
gradientAngle: settings.gradientAngle,
backgroundImageBuffer: bgImageBuffer ?? undefined,
blurEnabled: settings.blurEnabled,
blurIntensity: settings.blurIntensity,
shadowEnabled: settings.shadowEnabled,
shadowOpacity: settings.shadowOpacity,
outputFormat: fmt,
});
// Save the final output
const outputFilename = `${baseName}_nobg.${fmt}`;
await putObject(`outputs/${jobId}/${outputFilename}`, resultBuffer);
return reply.send({
jobId,
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(outputFilename)}`,
processedSize: resultBuffer.length,
});
} catch (err) {
request.log.error({ err }, "Effects processing failed");
return reply.status(422).send({
error: "Effects processing failed",
details: stripInternalPaths(err instanceof Error ? err.message : "Unknown error"),
});
}
},
);
// ── Pipeline/batch registry ──────────────────────────────────────
registerToolProcessFn({
toolId: "remove-background",
settingsSchema,
process: async (inputBuffer, settings, filename, ctx) => {
const s = settings as z.infer<typeof settingsSchema>;
const orientedBuffer = await autoOrient(inputBuffer);
const scratchDir = ctx?.scratchDir ?? join(tmpdir(), "snapotter-scratch", randomUUID());
const needsCleanup = !ctx?.scratchDir;
if (needsCleanup) await mkdir(scratchDir, { recursive: true });
try {
const transparentResult = await removeBackground(orientedBuffer, scratchDir, {
model: s.model,
edgeRefine: s.edgeRefine,
decontaminate: s.decontaminate,
});
const fmt = (s.outputFormat ?? "png") as BgOutputFormat;
const resultBuffer = await applyEffects(transparentResult, orientedBuffer, {
backgroundType: s.backgroundType,
backgroundColor: s.backgroundColor,
gradientColor1: s.gradientColor1,
gradientColor2: s.gradientColor2,
gradientAngle: s.gradientAngle,
blurEnabled: s.blurEnabled,
blurIntensity: s.blurIntensity,
shadowEnabled: s.shadowEnabled,
shadowOpacity: s.shadowOpacity,
outputFormat: fmt,
});
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_nobg.${fmt}`;
return {
buffer: resultBuffer,
filename: outputFilename,
contentType: BG_FORMAT_CONTENT_TYPES[fmt],
};
} finally {
if (needsCleanup) await rm(scratchDir, { recursive: true, force: true }).catch(() => {});
}
},
});
}