mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
QA + image-tool depth pass: codec/eraser/PDF fixes, modality renames, 13 image tools deepened (#249)
* fix(media): mux container-correct codecs in video tools Video tools hardcoded H.264 (and AAC) while keeping the input's container extension, so a .webm input produced an invalid file (ffmpeg exit 234: H.264 cannot be muxed into WebM). Add shared videoEncodeArgsForContainer/audioEncodeArgsForContainer helpers (vp9+opus for webm, theora+vorbis for ogv, h264+aac otherwise) and apply them across 14 tools; re-encode audio to AAC in burn-subtitles (forced mp4). Adds a webm regression test for change-fps. * fix(eraser): recover Object Eraser when its progress SSE drops The eraser used a bespoke EventSource with no recovery, so a dropped SSE left the UI stuck at ~25% forever even though the backend job had finished and saved its result. Add a resilient subscription (reconnect on tab refocus, which replays the cached terminal frame; 5-minute stall timeout) mirroring the standard processor's PR #203/#204 recovery. * feat(ui): rename the Documents modality to PDF and Data to Files Updates modality display names, the home-page tabs, the tool-page breadcrumb, and the homePage.documents/data + modalities labels across all 21 locales. URL slugs are unchanged for link stability. * feat(compress-pdf): add quality and target-size compression modes Mirror the image Compress tool: a quality slider (1-100) and a target file size, replacing the screen/ebook/printer preset. Adds gsCompressPdfQuality to doc-engine (quality maps to image downsample DPI, the dominant size lever for PDFs); target-size binary-searches the DPI for the highest quality under the target. The frontend reuses the shared CompressControls component, so no new translation strings are needed. * feat(ocr-pdf): show the PDF preview and extracted text side by side ocr-pdf fell back to the image viewer, which cannot render a PDF, so the right pane showed 'Preview not available' and the extracted text was only a download. It now uses a custom results view (custom-results display mode) rendering the input PDF via pdf.js (DocumentView gains an inputOnly prop, since the tool's output is a .txt) next to the extracted OCR text, with a copy button. * feat(ui): link the modality breadcrumb to its tools tab The modality segment of the tool breadcrumb (PDF, Image, Video, Audio, Files) is now a link to /?modality=<tab>. The home page reads the param, activates the matching tab, and cleans the URL, so it returns to the existing Tools page filtered to that modality without a new page. Handles the file modality whose tab key is 'data'. * feat(circle-crop): add zoom/offset framing, border, background, and output size Upgrade the circle-crop tool from a bare centered crop into a framing and styling tool. New settings (all backward-compatible with the old empty payload): - zoom (1-5x) plus offsetX/offsetY (0-1) to control how tight the circle is and where it sits in the source image - borderWidth (0-200px) plus borderColor for an optional ring - background: transparent (clear corners) or a hex fill - outputSize for a square output; omitted keeps native size The settings panel gains an inline draggable circular preview that mirrors the framing live, a zoom slider, a border slider with color, a transparent/color background toggle, and an output-size field. Adds an integration test covering output size, border, and a solid background. * feat(image-tools): flesh out five thin tools (gif-webp, histogram, favicon, color-palette, lqip) Tier A of the image-tool depth pass. Each of these was as bare as the old circle-crop (empty settings, opaque or invisible output). Now: - gif-webp: quality, lossless, and resize-percent controls; shows before/after size - histogram: returns full per-channel bins + stats; the settings panel renders an inline interactive histogram with R/G/B/Luma toggles, linear/log scale, and a mean/median/stdev readout (server PNG still downloadable) - favicon: background fill, padding, corner-radius, theme color, and a per-size checklist, with a live preview grid; the route applies the styling and honors the size filter - color-palette: count (2-16) and hex/rgb/hsl format controls, median-cut extraction, a palette strip, and CSS/JSON export - lqip-placeholder: blur/pixelate/solid strategies, format and quality; the output panel now surfaces the data URI with copy plus HTML/CSS snippets and a preview (previously the deliverable was never shown) Also expose resultPayload from useToolProcessor so a tool can render the route's extra result fields (histogram bins, lqip data URI) in its own panel. Updates the five integration tests to cover the new settings. * feat(image-tools): deepen five thin tools (duotone, vignette, pixelate, background-replace, blur-background) Tier B of the image-tool depth pass. - duotone: preset palettes, an intensity slider that blends the duotone with the original, and a true live duotone preview (a self-contained grayscale + lighten/darken overlay so the pane filter cannot wash it out) - vignette: radius, softness, roundness, and center-x/y controls driving a rebuilt radial gradient, with a matching live overlay - pixelate: a selection mode that exposes the route's region support via a draggable box over the image plus width/height sliders, so a face or plate can be pixelated in isolation - background-replace: gradient backgrounds, edge feather, and webp output on top of the existing solid color; now shown before/after - blur-background: edge feather and webp output; now shown before/after The live previews for duotone and vignette needed onImageStyle to mount the overlay branch in image-viewer. The duotone intensity blend and both AI tools' edge feather were rewritten to splice the alpha channel through raw buffers; joinChannel did not reliably re-tag the merged channel as alpha and a raw-without-encoder buffer broke the next decode. Updates the five integration tests. * fix(data): rename Files modality to Data + 20 Data-tool bug fixes (#247) * fix(ui): restore the Data modality name (revert Files rename) The 'file' modality reverts to the 'Data' label in modality.ts, the home-page tab, and the tools.data + documentsAndFiles i18n keys across all 21 locales. The separate Documents to PDF rename is kept. The URL slug was already /data, so name and slug realign; the tool breadcrumb follows modality.ts automatically. * fix(create-zip): require at least two files before enabling submit create-zip enabled its submit button with a single file, but the backend rejects fewer than two files ('Zipping needs at least two files'), producing a 422 error. Gate the button on files.length >= 2 to match the sibling merge-csvs tool. Found during the Data-modality QA sweep. * fix(data): resolve 17 bugs found in a deeper Data-tool review Crashes (threw an internal error on otherwise-valid input): - csv-json: a primitive JSON array like [1,2,3] threw "Unable to serialize"; now a clear error. - json-xml: a null or primitive JSON root crashed the XML builder; now a clear 4xx. - yaml-json: an empty or comment-only YAML returned undefined and threw on Buffer.from; now emits null. Data loss / wrong output: - csv-json: nested objects rendered as "[object Object]" (now serialized to JSON); heterogeneous objects dropped columns (now the union of all keys). - xml-to-csv: leaked fast-xml-parser markers ("@_" on attributes, "#text") into CSV headers (now cleaned); a single-record XML failed to tabulate (now a 1-row table); heterogeneous records dropped columns (now the union of all keys). - csv-excel: xlsx date cells were rendered in the server timezone via Date.toString (now ISO 8601, round-trippable). - create-zip and extract-zip: filename/basename collisions overwrote zip entries and silently lost a file; dedup now checks generated names and guarantees uniqueness. - chart-maker: negative values produced invalid/degenerate SVG that Sharp silently dropped; now rejected with a clear message. Empty output / validation: - split-csv: a header-only CSV produced an empty zip; now errors with "No data rows to split". - extract-zip: a directory-only zip produced an empty zip; now errors with "No extractable files found". - create-zip and merge-csvs: a single-file request fell through to the worker and returned 422; the factory now supports minInputs and returns 400 pre-enqueue. UI: - review-panel: the result card showed "Saved +X%" when the output grew; the savings row now appears only when the file is actually smaller (Original/Processed sizes always shown). Found via two adversarial code-review passes over the 10 Data routes. All 24 fix + regression checks pass against a fresh Docker stack on :1359. * fix(data): clean 400 for unsafe-zip entries; drop header on split keepHeader=false - tool-factory: add an opt-in preValidate hook that runs after input prep and before enqueue. Throwing InputValidationError there returns its statusCode (400) instead of the worker's generic 422. BullMQ loses the error class across the job boundary, so InputValidationErrors thrown in the worker cannot be mapped to their status; pre-enqueue validation can. - extract-zip: validate entry paths via preValidate, rejecting path-traversal and absolute-path archives (and unreadable/corrupt zips) with a clear 400. The processV2 guards remain as defense-in-depth for the pipeline/batch path. - split-csv: keepHeader=false now drops the header (parts contain only data rows) instead of keeping it as the first data row of part-1. Verified against a fresh Docker stack: unsafe / absolute / corrupt zips -> 400, normal zip still 200; split keepHeader=false drops the header while true repeats it in each part. No regressions across 51 fix + scenario checks. * feat(image-tools): deepen image-pad and sprite-sheet, fix sprite-sheet multi-file submit Tier C of the image-tool depth pass. - image-pad: a custom W:H ratio alongside the presets, a background mode (solid color, transparent, or an Instagram-style blurred cover fill), and an extra padding margin. The settings panel gains a real live preview of the padded canvas (it previously declared live-preview but rendered nothing) via onImageStyle + onImageOverlay. - sprite-sheet: PNG/WebP/JPEG output with a quality control, and the coordinate map it already computes is now returned and surfaced as Copy CSS (per-frame background-position rules) and Copy JSON exports. Also fix a pre-existing sprite-sheet bug: with more than one image the panel called processAllFiles, fanning out to the per-file batch route (422). It now calls processFiles, which packs all images into a single sheet request (it is a MULTI_FILE tool). Updates both integration tests. * fix(media): preserve source sample rate after loudnorm (#243) ffmpeg's loudnorm filter runs internally at 192 kHz and emits 192 kHz unless the chain resamples back. normalize-audio and video-loudnorm therefore produced 192 kHz output (4.3x larger files) regardless of the input rate. Append aresample to restore the input's sample rate. runMediaTool now exposes the input audio sample rate to its args callback. * fix(color-palette): collapse solid-color images to one swatch The median-cut bucket selector started bestRange at -1, so a uniform bucket (range 0) still satisfied the > comparison and kept splitting, yielding N identical swatches for a solid-color image. Start at 0 so only buckets with real color spread are split. * fix(lint): annotate implicit-any lets in saml and user-files biome noImplicitAnyLet flagged the bare let in saml.ts (profile) and user-files.ts (stream); add derived type annotations (type-only, no behavior change). Latent on main via the turbo lint cache; surfaced when the Data changes busted the apps/api lint cache.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { probeMedia, runFfmpeg } from "@snapotter/media-engine";
|
||||
import { probeMedia, resolveEncoder, runFfmpeg } from "@snapotter/media-engine";
|
||||
import type { ToolProcessCtxV2 } from "../routes/tool-factory.js";
|
||||
|
||||
const EXT_VIDEO_CONTENT_TYPES: Record<string, string> = {
|
||||
@@ -15,6 +15,40 @@ export function videoContentType(ext: string): string {
|
||||
return EXT_VIDEO_CONTENT_TYPES[ext.toLowerCase()] || "video/mp4";
|
||||
}
|
||||
|
||||
/**
|
||||
* Video encode args valid for the given OUTPUT container extension.
|
||||
* WebM accepts only VP8/VP9/AV1, OGV only Theora; everything else
|
||||
* (mp4/mov/mkv/avi/ts) gets H.264. Hardcoding H.264 into a preserved
|
||||
* non-mp4 container is a real bug: ffmpeg cannot mux H.264 into WebM and
|
||||
* fails the header write ("Invalid argument", exit 234). Arg sets mirror
|
||||
* the tested convert-video encoders.
|
||||
*/
|
||||
export function videoEncodeArgsForContainer(ext: string): string[] {
|
||||
const lower = ext.toLowerCase();
|
||||
if (lower === ".webm") {
|
||||
return ["-c:v", resolveEncoder("vp9"), "-crf", "30", "-b:v", "0", "-row-mt", "1"];
|
||||
}
|
||||
if (lower === ".ogv" || lower === ".ogg") {
|
||||
// Theora has no HW-accel path; use libtheora directly.
|
||||
return ["-c:v", "libtheora", "-q:v", "7"];
|
||||
}
|
||||
return ["-c:v", resolveEncoder("h264"), "-crf", "20", "-preset", "medium", "-pix_fmt", "yuv420p"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Audio encode args valid for the given OUTPUT container, for tools that
|
||||
* must (re-)encode the audio stream (tempo/reverse/loudnorm/replace).
|
||||
* WebM needs Opus, OGV needs Vorbis, everything else gets AAC. When the
|
||||
* source audio stream is untouched and the container is preserved, use
|
||||
* ["-c:a", "copy"] directly instead -- it is always valid there.
|
||||
*/
|
||||
export function audioEncodeArgsForContainer(ext: string): string[] {
|
||||
const lower = ext.toLowerCase();
|
||||
if (lower === ".webm") return ["-c:a", resolveEncoder("opus")];
|
||||
if (lower === ".ogv" || lower === ".ogg") return ["-c:a", "libvorbis"];
|
||||
return ["-c:a", resolveEncoder("aac")];
|
||||
}
|
||||
|
||||
const EXT_AUDIO_CONTENT_TYPES: Record<string, string> = {
|
||||
".mp3": "audio/mpeg",
|
||||
".wav": "audio/wav",
|
||||
|
||||
@@ -95,7 +95,7 @@ export async function registerSaml(app: FastifyInstance): Promise<void> {
|
||||
const saml = getSamlInstance();
|
||||
const audit = auditFromRequest(request);
|
||||
|
||||
let profile;
|
||||
let profile: Awaited<ReturnType<typeof saml.validatePostResponseAsync>>["profile"];
|
||||
try {
|
||||
const result = await saml.validatePostResponseAsync(request.body as Record<string, string>);
|
||||
profile = result.profile;
|
||||
|
||||
@@ -73,6 +73,16 @@ export interface ToolRouteConfig<T> {
|
||||
* inputRefs in arrival order.
|
||||
*/
|
||||
maxInputs?: number;
|
||||
/** Minimum number of file parts required (default 1). Fewer returns HTTP 400. */
|
||||
minInputs?: number;
|
||||
/**
|
||||
* Optional pre-enqueue validation hook. Receives the prepared input buffers
|
||||
* and validated settings; throw InputValidationError to reject with its
|
||||
* statusCode (default 400) before any job is enqueued (vs a worker 422).
|
||||
*/
|
||||
preValidate?: (ctx: {
|
||||
inputs: { filename: string; buffer: Buffer }[];
|
||||
}) => Promise<void> | void;
|
||||
/**
|
||||
* Per-position input kind overrides for mixed-input tools (e.g. video +
|
||||
* subtitle). Input i validates with kind inputKinds[Math.min(i, len-1)].
|
||||
@@ -110,6 +120,10 @@ export interface ToolRouteConfig<T> {
|
||||
export interface AnyToolRouteConfig {
|
||||
toolId: string;
|
||||
maxInputs?: number;
|
||||
minInputs?: number;
|
||||
preValidate?: (ctx: {
|
||||
inputs: { filename: string; buffer: Buffer }[];
|
||||
}) => Promise<void> | void;
|
||||
inputKinds?: ("video" | "audio" | "image" | "subtitle")[];
|
||||
settingsSchema: z.ZodType<unknown, z.ZodTypeDef, unknown>;
|
||||
process: (
|
||||
@@ -215,6 +229,7 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
||||
|
||||
const jobId = randomUUID();
|
||||
const maxInputs = config.maxInputs ?? 1;
|
||||
const minInputs = config.minInputs ?? 1;
|
||||
let filename = "image";
|
||||
let settingsRaw: string | null = null;
|
||||
let fileId: string | null = null;
|
||||
@@ -306,6 +321,14 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
||||
return reply.status(400).send({ error: "No image file provided" });
|
||||
}
|
||||
|
||||
// Require the tool's minimum number of files (e.g. create-zip / merge-csvs
|
||||
// need 2). Returns 400 pre-enqueue instead of a 422 from the worker.
|
||||
if (received.length < minInputs) {
|
||||
return reply.status(400).send({
|
||||
error: `This tool needs at least ${minInputs} files`,
|
||||
});
|
||||
}
|
||||
|
||||
const reportProgress = (percent: number, stage?: string) => {
|
||||
if (!clientJobId) return;
|
||||
updateSingleFileProgress({
|
||||
@@ -361,6 +384,7 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
||||
|
||||
// Prepare all files through the modality input handler
|
||||
const inputRefs: string[] = [];
|
||||
const preparedInputs: { filename: string; buffer: Buffer }[] = [];
|
||||
for (let i = 0; i < received.length; i++) {
|
||||
const upload = received[i];
|
||||
let fileBuffer = await getObjectBuffer(upload.key);
|
||||
@@ -404,6 +428,10 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
||||
if (i === 0) {
|
||||
filename = fname;
|
||||
}
|
||||
|
||||
if (config.preValidate) {
|
||||
preparedInputs.push({ filename: fname, buffer: fileBuffer });
|
||||
}
|
||||
}
|
||||
|
||||
reportProgress(15, "Preparing...");
|
||||
@@ -430,6 +458,22 @@ export function createToolRoute<T>(app: FastifyInstance, config: ToolRouteConfig
|
||||
return reply.status(400).send({ error: "Settings must be valid JSON" });
|
||||
}
|
||||
|
||||
// Optional tool-specific pre-enqueue validation (e.g. zip-entry safety).
|
||||
// Throwing InputValidationError here returns its statusCode (400) before
|
||||
// any job is enqueued, instead of a generic 422 from the worker.
|
||||
if (config.preValidate) {
|
||||
try {
|
||||
await config.preValidate({ inputs: preparedInputs });
|
||||
} catch (err) {
|
||||
if (err instanceof InputValidationError) {
|
||||
const body: Record<string, string> = { error: err.message };
|
||||
if (err.details) body.details = err.details;
|
||||
return reply.status(err.statusCode).send(body);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Guard: check if the tool's AI feature bundle is installed
|
||||
const bundleId = TOOL_BUNDLE_MAP[config.toolId];
|
||||
if (bundleId && !isToolInstalled(config.toolId)) {
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const TARGETS = {
|
||||
@@ -76,14 +81,7 @@ export function registerAspectPad(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
`pad=${cw}:${ch}:(ow-iw)/2:(oh-ih)/2:color=${c}`,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
outPath,
|
||||
|
||||
@@ -2,11 +2,12 @@ import { randomUUID } from "node:crypto";
|
||||
import { removeBackground } from "@snapotter/ai";
|
||||
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@snapotter/shared";
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import sharp from "sharp";
|
||||
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 { compositeOnColor } from "../../lib/bg-effects.js";
|
||||
import { compositeOnColor, createGradientBackground } 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";
|
||||
@@ -14,13 +15,43 @@ import { decodeToSharpCompat, needsCliDecode } from "../../lib/format-decoders.j
|
||||
import { decodeHeic } from "../../lib/heic-converter.js";
|
||||
import { receiveUpload } from "../../lib/upload-stream.js";
|
||||
|
||||
const HEX_RE = /^#[0-9a-fA-F]{6}$/;
|
||||
|
||||
const settingsSchema = z.object({
|
||||
color: z
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#ffffff"),
|
||||
backgroundType: z.enum(["color", "gradient"]).default("color"),
|
||||
color: z.string().regex(HEX_RE).default("#ffffff"),
|
||||
gradientColor1: z.string().regex(HEX_RE).optional(),
|
||||
gradientColor2: z.string().regex(HEX_RE).optional(),
|
||||
gradientAngle: z.number().int().min(0).max(360).default(180),
|
||||
feather: z.number().int().min(0).max(20).default(0),
|
||||
format: z.enum(["png", "webp"]).default("png"),
|
||||
});
|
||||
|
||||
/**
|
||||
* Soften the alpha edges of a subject PNG by blurring its alpha channel.
|
||||
* Keeps RGB intact; only the transparency boundary gets smoothed.
|
||||
*/
|
||||
async function featherEdges(subjectBuffer: Buffer, radius: number): Promise<Buffer> {
|
||||
// Read the subject as raw RGBA plus a separately-blurred copy of its alpha,
|
||||
// then overwrite the alpha channel in place. joinChannel does not reliably
|
||||
// re-tag the merged channel as alpha, so we splice the raw bytes directly.
|
||||
const { data: rgba, info } = await sharp(subjectBuffer)
|
||||
.ensureAlpha()
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
const { data: blurredAlpha } = await sharp(subjectBuffer)
|
||||
.extractChannel(3)
|
||||
.blur(radius)
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
for (let p = 3, a = 0; p < rgba.length; p += 4, a++) {
|
||||
rgba[p] = blurredAlpha[a];
|
||||
}
|
||||
return sharp(rgba, { raw: { width: info.width, height: info.height, channels: 4 } })
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
// -- AI job handler (runs inside the BullMQ worker) --
|
||||
registerAiJobHandler("background-replace", async (input, data, ctx) => {
|
||||
const settings = settingsSchema.parse(data.settings);
|
||||
@@ -33,17 +64,46 @@ registerAiJobHandler("background-replace", async (input, data, ctx) => {
|
||||
ctx.report(Math.min(scaled, 80), stage);
|
||||
});
|
||||
|
||||
ctx.report(85, "Compositing on color");
|
||||
// Feather alpha edges before compositing
|
||||
let subject = subjectPng;
|
||||
if (settings.feather > 0) {
|
||||
ctx.report(82, "Feathering edges");
|
||||
subject = await featherEdges(subjectPng, settings.feather);
|
||||
}
|
||||
|
||||
const result = await compositeOnColor(subjectPng, settings.color);
|
||||
ctx.report(85, "Compositing background");
|
||||
|
||||
let composited: Buffer;
|
||||
if (settings.backgroundType === "gradient") {
|
||||
const meta = await sharp(subject).metadata();
|
||||
if (!meta.width || !meta.height) throw new Error("Cannot read subject dimensions");
|
||||
const gradBg = await createGradientBackground(
|
||||
meta.width,
|
||||
meta.height,
|
||||
settings.gradientColor1 ?? "#ffffff",
|
||||
settings.gradientColor2 ?? "#000000",
|
||||
settings.gradientAngle,
|
||||
);
|
||||
composited = await sharp(gradBg)
|
||||
.composite([{ input: subject, blend: "over" }])
|
||||
.png()
|
||||
.toBuffer();
|
||||
} else {
|
||||
composited = await compositeOnColor(subject, settings.color);
|
||||
}
|
||||
|
||||
// Encode to requested output format
|
||||
const fmt = settings.format;
|
||||
const result =
|
||||
fmt === "webp" ? await sharp(composited).webp({ lossless: true }).toBuffer() : composited;
|
||||
|
||||
const base = data.filename.replace(/\.[^.]+$/, "");
|
||||
const outName = `${base}_bg.png`;
|
||||
const outName = `${base}_bg.${fmt}`;
|
||||
|
||||
return {
|
||||
buffer: result,
|
||||
filename: outName,
|
||||
contentType: "image/png",
|
||||
contentType: fmt === "webp" ? "image/webp" : "image/png",
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
|
||||
import { removeBackground } from "@snapotter/ai";
|
||||
import { getBundleForTool, TOOL_BUNDLE_MAP } from "@snapotter/shared";
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
import { registerAiJobHandler } from "../../jobs/ai-handlers.js";
|
||||
import { enqueueToolJob } from "../../jobs/enqueue.js";
|
||||
@@ -16,6 +17,8 @@ import { receiveUpload } from "../../lib/upload-stream.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
intensity: z.number().int().min(1).max(100).default(50),
|
||||
feather: z.number().int().min(0).max(20).default(0),
|
||||
format: z.enum(["png", "webp"]).default("png"),
|
||||
});
|
||||
|
||||
// -- AI job handler (runs inside the BullMQ worker) --
|
||||
@@ -32,15 +35,45 @@ registerAiJobHandler("blur-background", async (input, data, ctx) => {
|
||||
|
||||
ctx.report(85, "Blurring background");
|
||||
|
||||
const result = await blurBackground(input, subjectPng, settings.intensity);
|
||||
// If feather > 0, soften the subject alpha edge before compositing. Read the
|
||||
// subject as raw RGBA plus a separately-blurred copy of its alpha and
|
||||
// overwrite the alpha bytes in place; joinChannel does not reliably re-tag the
|
||||
// merged channel as alpha.
|
||||
let compositeSubject = subjectPng;
|
||||
if (settings.feather > 0) {
|
||||
const { data: rgba, info } = await sharp(subjectPng)
|
||||
.ensureAlpha()
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
const { data: blurredAlpha } = await sharp(subjectPng)
|
||||
.extractChannel(3)
|
||||
.blur(settings.feather)
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
for (let p = 3, a = 0; p < rgba.length; p += 4, a++) {
|
||||
rgba[p] = blurredAlpha[a];
|
||||
}
|
||||
compositeSubject = await sharp(rgba, {
|
||||
raw: { width: info.width, height: info.height, channels: 4 },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
const blurred = await blurBackground(input, compositeSubject, settings.intensity);
|
||||
|
||||
// Encode in the requested output format
|
||||
const fmt = settings.format;
|
||||
const base = data.filename.replace(/\.[^.]+$/, "");
|
||||
const outName = `${base}_blurbg.png`;
|
||||
const outName = `${base}_blurbg.${fmt}`;
|
||||
const contentType = fmt === "webp" ? "image/webp" : "image/png";
|
||||
const result =
|
||||
fmt === "webp" ? await sharp(blurred).webp({ lossless: true }).toBuffer() : blurred; // blurBackground already returns PNG
|
||||
|
||||
return {
|
||||
buffer: result,
|
||||
filename: outName,
|
||||
contentType: "image/png",
|
||||
contentType,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const TARGETS = {
|
||||
@@ -77,14 +82,7 @@ export function registerBlurPad(app: FastifyInstance) {
|
||||
"[v]",
|
||||
"-map",
|
||||
"0:a?",
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
outPath,
|
||||
|
||||
@@ -57,7 +57,7 @@ export function registerBurnSubtitles(app: FastifyInstance) {
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
"-c:a",
|
||||
"copy",
|
||||
resolveEncoder("aac"),
|
||||
outPath,
|
||||
],
|
||||
info.durationS,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { extname } from "node:path";
|
||||
import { resolveEncoder } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
@@ -28,14 +31,7 @@ export function registerChangeFps(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
`fps=${settings.fps}`,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
out,
|
||||
|
||||
@@ -223,6 +223,11 @@ export function registerChartMaker(app: FastifyInstance) {
|
||||
throw new Error("Column 2 must be numeric");
|
||||
}
|
||||
}
|
||||
// Negative values render as invalid/degenerate SVG (negative bar heights,
|
||||
// backward pie arcs that Sharp silently drops); reject with a clear message.
|
||||
if (data.some((point) => point.value < 0)) {
|
||||
throw new Error("Chart values must be zero or greater");
|
||||
}
|
||||
|
||||
let svg: string;
|
||||
switch (settings.kind) {
|
||||
|
||||
@@ -3,44 +3,97 @@ import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({});
|
||||
const settingsSchema = z.object({
|
||||
// Framing: zoom (>=1 crops tighter) + where the circle sits in the image (0..1).
|
||||
zoom: z.number().min(1).max(5).default(1),
|
||||
offsetX: z.number().min(0).max(1).default(0.5),
|
||||
offsetY: z.number().min(0).max(1).default(0.5),
|
||||
// Styling.
|
||||
borderWidth: z.number().int().min(0).max(200).default(0),
|
||||
borderColor: z
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#ffffff"),
|
||||
// "transparent" leaves the corners clear; a hex fills them.
|
||||
background: z
|
||||
.string()
|
||||
.regex(/^(transparent|#[0-9a-fA-F]{6})$/)
|
||||
.default("transparent"),
|
||||
// Final output dimension in px (square). Omitted = native size.
|
||||
outputSize: z.number().int().min(16).max(4096).optional(),
|
||||
});
|
||||
|
||||
function hexToRgb(hex: string): { r: number; g: number; b: number } {
|
||||
return {
|
||||
r: Number.parseInt(hex.slice(1, 3), 16),
|
||||
g: Number.parseInt(hex.slice(3, 5), 16),
|
||||
b: Number.parseInt(hex.slice(5, 7), 16),
|
||||
};
|
||||
}
|
||||
|
||||
export function registerCircleCrop(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "circle-crop",
|
||||
settingsSchema,
|
||||
process: async (inputBuffer, _settings, filename) => {
|
||||
process: async (inputBuffer, settings, filename) => {
|
||||
const meta = await sharp(inputBuffer).metadata();
|
||||
const w = meta.width ?? 1;
|
||||
const h = meta.height ?? 1;
|
||||
const d = Math.min(w, h);
|
||||
const W = meta.width ?? 1;
|
||||
const H = meta.height ?? 1;
|
||||
|
||||
// Extract centered square
|
||||
const left = Math.floor((w - d) / 2);
|
||||
const top = Math.floor((h - d) / 2);
|
||||
// The circle's bounding square, derived from zoom + offsets.
|
||||
let d = Math.round(Math.min(W, H) / settings.zoom);
|
||||
d = Math.max(8, Math.min(d, W, H));
|
||||
let left = Math.round((W - d) * settings.offsetX);
|
||||
let top = Math.round((H - d) * settings.offsetY);
|
||||
left = Math.max(0, Math.min(left, W - d));
|
||||
top = Math.max(0, Math.min(top, H - d));
|
||||
|
||||
const bw = Math.min(settings.borderWidth, Math.floor(d / 2));
|
||||
const canvas = d + 2 * bw;
|
||||
|
||||
// Extract the square, mask it to a circle.
|
||||
const squareBuf = await sharp(inputBuffer)
|
||||
.extract({ left, top, width: d, height: d })
|
||||
.toBuffer();
|
||||
|
||||
// Create SVG circle mask
|
||||
const r = d / 2;
|
||||
const mask = Buffer.from(
|
||||
`<svg width="${d}" height="${d}"><circle cx="${r}" cy="${r}" r="${r}" fill="white"/></svg>`,
|
||||
const circleMask = Buffer.from(
|
||||
`<svg width="${d}" height="${d}"><circle cx="${d / 2}" cy="${d / 2}" r="${d / 2}" fill="#fff"/></svg>`,
|
||||
);
|
||||
|
||||
// Composite with dest-in blend to mask
|
||||
const buffer = await sharp(squareBuf)
|
||||
const imgCircle = await sharp(squareBuf)
|
||||
.ensureAlpha()
|
||||
.composite([{ input: await sharp(mask).resize(d, d).toBuffer(), blend: "dest-in" }])
|
||||
.composite([{ input: circleMask, blend: "dest-in" }])
|
||||
.png()
|
||||
.toBuffer();
|
||||
|
||||
// Compose: background, optional border ring, then the circular image.
|
||||
const bg =
|
||||
settings.background === "transparent"
|
||||
? { r: 0, g: 0, b: 0, alpha: 0 }
|
||||
: { ...hexToRgb(settings.background), alpha: 1 };
|
||||
const layers: sharp.OverlayOptions[] = [];
|
||||
if (bw > 0) {
|
||||
const ring = Buffer.from(
|
||||
`<svg width="${canvas}" height="${canvas}"><circle cx="${canvas / 2}" cy="${canvas / 2}" r="${d / 2 + bw}" fill="${settings.borderColor}"/></svg>`,
|
||||
);
|
||||
layers.push({ input: ring, left: 0, top: 0 });
|
||||
}
|
||||
layers.push({ input: imgCircle, left: bw, top: bw });
|
||||
|
||||
let out = await sharp({
|
||||
create: { width: canvas, height: canvas, channels: 4, background: bg },
|
||||
})
|
||||
.composite(layers)
|
||||
.png()
|
||||
.toBuffer();
|
||||
|
||||
if (settings.outputSize) {
|
||||
out = await sharp(out)
|
||||
.resize(settings.outputSize, settings.outputSize, { fit: "fill" })
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
const base = filename.replace(/\.[^.]+$/, "");
|
||||
return {
|
||||
buffer,
|
||||
filename: `${base}_circle.png`,
|
||||
contentType: "image/png",
|
||||
};
|
||||
return { buffer: out, filename: `${base}_circle.png`, contentType: "image/png" };
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,53 +1,160 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
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 { decompressSvgz, sanitizeSvg } from "../../lib/svg-sanitize.js";
|
||||
|
||||
/**
|
||||
* Simple k-means-like color quantization to extract dominant colors.
|
||||
*/
|
||||
function extractColors(pixels: Buffer, channelCount: number, maxColors: number): string[] {
|
||||
// Build frequency map of quantized colors
|
||||
const colorMap = new Map<string, number>();
|
||||
const settingsSchema = z
|
||||
.object({
|
||||
count: z.number().int().min(2).max(16).default(8),
|
||||
format: z.enum(["hex", "rgb", "hsl"]).default("hex"),
|
||||
})
|
||||
.default({});
|
||||
|
||||
for (let i = 0; i < pixels.length; i += channelCount) {
|
||||
// Quantize to reduce noise (round to nearest 16)
|
||||
const r = Math.min(Math.round(pixels[i] / 16) * 16, 255);
|
||||
const g = Math.min(Math.round(pixels[i + 1] / 16) * 16, 255);
|
||||
const b = Math.min(Math.round(pixels[i + 2] / 16) * 16, 255);
|
||||
const key = `${r},${g},${b}`;
|
||||
colorMap.set(key, (colorMap.get(key) ?? 0) + 1);
|
||||
// ── Color format helpers ─────────────────────────────────────────
|
||||
|
||||
function rgbToHex(r: number, g: number, b: number): string {
|
||||
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function rgbToRgbString(r: number, g: number, b: number): string {
|
||||
return `rgb(${r}, ${g}, ${b})`;
|
||||
}
|
||||
|
||||
function rgbToHsl(r: number, g: number, b: number): string {
|
||||
const rn = r / 255;
|
||||
const gn = g / 255;
|
||||
const bn = b / 255;
|
||||
const max = Math.max(rn, gn, bn);
|
||||
const min = Math.min(rn, gn, bn);
|
||||
const l = (max + min) / 2;
|
||||
if (max === min) return `hsl(0, 0%, ${Math.round(l * 100)}%)`;
|
||||
const d = max - min;
|
||||
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
let h = 0;
|
||||
if (max === rn) h = ((gn - bn) / d + (gn < bn ? 6 : 0)) / 6;
|
||||
else if (max === gn) h = ((bn - rn) / d + 2) / 6;
|
||||
else h = ((rn - gn) / d + 4) / 6;
|
||||
return `hsl(${Math.round(h * 360)}, ${Math.round(s * 100)}%, ${Math.round(l * 100)}%)`;
|
||||
}
|
||||
|
||||
function formatColor(r: number, g: number, b: number, fmt: "hex" | "rgb" | "hsl"): string {
|
||||
if (fmt === "rgb") return rgbToRgbString(r, g, b);
|
||||
if (fmt === "hsl") return rgbToHsl(r, g, b);
|
||||
return rgbToHex(r, g, b);
|
||||
}
|
||||
|
||||
// ── Median-cut quantization ──────────────────────────────────────
|
||||
|
||||
interface ColorBucket {
|
||||
pixels: Array<[number, number, number]>;
|
||||
}
|
||||
|
||||
function rangeOfChannel(pixels: Array<[number, number, number]>, ch: 0 | 1 | 2): number {
|
||||
let min = 255;
|
||||
let max = 0;
|
||||
for (const px of pixels) {
|
||||
if (px[ch] < min) min = px[ch];
|
||||
if (px[ch] > max) max = px[ch];
|
||||
}
|
||||
return max - min;
|
||||
}
|
||||
|
||||
// Sort by frequency and pick top colors
|
||||
const sorted = [...colorMap.entries()].sort((a, b) => b[1] - a[1]);
|
||||
function medianCut(
|
||||
pixels: Array<[number, number, number]>,
|
||||
maxColors: number,
|
||||
): Array<{ r: number; g: number; b: number; count: number }> {
|
||||
if (pixels.length === 0) return [];
|
||||
|
||||
// Filter similar colors (merge colors within distance 40)
|
||||
const results: Array<{ r: number; g: number; b: number; count: number }> = [];
|
||||
for (const [key, count] of sorted) {
|
||||
const [r, g, b] = key.split(",").map(Number);
|
||||
const tooClose = results.some(
|
||||
(c) => Math.abs(c.r - r) + Math.abs(c.g - g) + Math.abs(c.b - b) < 48,
|
||||
);
|
||||
if (!tooClose) {
|
||||
results.push({ r, g, b, count });
|
||||
const buckets: ColorBucket[] = [{ pixels }];
|
||||
|
||||
// Split until we have enough buckets or can't split further
|
||||
while (buckets.length < maxColors) {
|
||||
// Pick the bucket with the widest channel range. bestRange starts at 0 so a
|
||||
// uniform bucket (range 0) is never chosen -- otherwise a solid-color image
|
||||
// would keep splitting into identical swatches.
|
||||
let bestIdx = -1;
|
||||
let bestRange = 0;
|
||||
let bestCh: 0 | 1 | 2 = 0;
|
||||
|
||||
for (let i = 0; i < buckets.length; i++) {
|
||||
if (buckets[i].pixels.length < 2) continue;
|
||||
for (const ch of [0, 1, 2] as const) {
|
||||
const r = rangeOfChannel(buckets[i].pixels, ch);
|
||||
if (r > bestRange) {
|
||||
bestRange = r;
|
||||
bestIdx = i;
|
||||
bestCh = ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (results.length >= maxColors) break;
|
||||
|
||||
if (bestIdx === -1) break; // nothing left to split
|
||||
|
||||
const bucket = buckets[bestIdx];
|
||||
bucket.pixels.sort((a, b) => a[bestCh] - b[bestCh]);
|
||||
const mid = Math.floor(bucket.pixels.length / 2);
|
||||
buckets.splice(
|
||||
bestIdx,
|
||||
1,
|
||||
{ pixels: bucket.pixels.slice(0, mid) },
|
||||
{ pixels: bucket.pixels.slice(mid) },
|
||||
);
|
||||
}
|
||||
|
||||
return results.map(({ r, g, b }) => {
|
||||
const hex = `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
||||
return hex;
|
||||
});
|
||||
// Average each bucket to get representative colors, sorted by population
|
||||
return buckets
|
||||
.filter((b) => b.pixels.length > 0)
|
||||
.map((b) => {
|
||||
let rSum = 0;
|
||||
let gSum = 0;
|
||||
let bSum = 0;
|
||||
for (const px of b.pixels) {
|
||||
rSum += px[0];
|
||||
gSum += px[1];
|
||||
bSum += px[2];
|
||||
}
|
||||
const n = b.pixels.length;
|
||||
return {
|
||||
r: Math.round(rSum / n),
|
||||
g: Math.round(gSum / n),
|
||||
b: Math.round(bSum / n),
|
||||
count: n,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => b.count - a.count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract dominant colors via median-cut quantization.
|
||||
*/
|
||||
function extractColors(
|
||||
pixels: Buffer,
|
||||
channelCount: number,
|
||||
maxColors: number,
|
||||
fmt: "hex" | "rgb" | "hsl",
|
||||
): { colors: string[]; hex: string[] } {
|
||||
const pxArray: Array<[number, number, number]> = [];
|
||||
for (let i = 0; i < pixels.length; i += channelCount) {
|
||||
pxArray.push([pixels[i], pixels[i + 1], pixels[i + 2]]);
|
||||
}
|
||||
|
||||
const representatives = medianCut(pxArray, maxColors);
|
||||
|
||||
return {
|
||||
colors: representatives.map((c) => formatColor(c.r, c.g, c.b, fmt)),
|
||||
hex: representatives.map((c) => rgbToHex(c.r, c.g, c.b)),
|
||||
};
|
||||
}
|
||||
|
||||
export function registerColorPalette(app: FastifyInstance) {
|
||||
app.post("/api/v1/tools/color-palette", async (request, reply) => {
|
||||
let fileBuffer: Buffer | null = null;
|
||||
let filename = "image";
|
||||
let rawSettings: string | undefined;
|
||||
|
||||
try {
|
||||
const parts = request.parts();
|
||||
@@ -59,6 +166,8 @@ export function registerColorPalette(app: FastifyInstance) {
|
||||
}
|
||||
fileBuffer = Buffer.concat(chunks);
|
||||
filename = sanitizeFilename(part.filename ?? "image");
|
||||
} else if (part.fieldname === "settings") {
|
||||
rawSettings = part.value as string;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -72,6 +181,18 @@ export function registerColorPalette(app: FastifyInstance) {
|
||||
return reply.status(400).send({ error: "No image file provided" });
|
||||
}
|
||||
|
||||
// Parse and validate settings
|
||||
let parsed: { count: number; format: "hex" | "rgb" | "hsl" };
|
||||
try {
|
||||
const json = rawSettings ? JSON.parse(rawSettings) : {};
|
||||
parsed = settingsSchema.parse(json);
|
||||
} catch (err) {
|
||||
return reply.status(400).send({
|
||||
error: "Invalid settings",
|
||||
details: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const validation = await validateImageBuffer(fileBuffer, filename);
|
||||
if (!validation.valid) {
|
||||
@@ -113,18 +234,19 @@ export function registerColorPalette(app: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
|
||||
// Resize to small image for analysis
|
||||
// Resize to small image for analysis (100x100 for better sampling)
|
||||
const raw = await sharp(fileBuffer)
|
||||
.resize(50, 50, { fit: "fill" })
|
||||
.resize(100, 100, { fit: "fill" })
|
||||
.removeAlpha()
|
||||
.raw()
|
||||
.toBuffer();
|
||||
|
||||
const colors = extractColors(raw, 3, 8);
|
||||
const { colors, hex } = extractColors(raw, 3, parsed.count, parsed.format);
|
||||
|
||||
return reply.send({
|
||||
filename,
|
||||
colors,
|
||||
hex,
|
||||
count: colors.length,
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { copyFile, stat, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { gsCompressPdf } from "@snapotter/doc-engine";
|
||||
import { gsCompressPdfQuality } from "@snapotter/doc-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
// Mirrors the image "compress" tool: compress by a quality slider or to a
|
||||
// target file size. For PDFs the size lever is image downsampling resolution
|
||||
// (DPI), so quality 1..100 maps onto a DPI range and target-size binary-
|
||||
// searches that DPI.
|
||||
const settingsSchema = z.object({
|
||||
preset: z.enum(["screen", "ebook", "printer"]).default("ebook"),
|
||||
mode: z.enum(["quality", "targetSize"]).default("quality"),
|
||||
quality: z.number().int().min(1).max(100).optional(),
|
||||
targetSizeKb: z.number().positive().optional(),
|
||||
});
|
||||
|
||||
const MIN_DPI = 20;
|
||||
const MAX_DPI = 300;
|
||||
const qualityToDpi = (q: number) => Math.round(MIN_DPI + ((q - 1) / 99) * (MAX_DPI - MIN_DPI));
|
||||
|
||||
export function registerCompressPdf(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "compress-pdf",
|
||||
@@ -22,12 +32,41 @@ export function registerCompressPdf(app: FastifyInstance) {
|
||||
const base = input.filename.replace(/\.[^.]+$/, "");
|
||||
const inPath = join(ctx.scratchDir, `in-${input.filename.replace(/[^A-Za-z0-9._-]/g, "_")}`);
|
||||
await writeFile(inPath, input.buffer);
|
||||
|
||||
const outPath = join(ctx.scratchDir, `${base}_compressed.pdf`);
|
||||
ctx.report(10, "Compressing");
|
||||
await gsCompressPdf(inPath, outPath, settings.preset);
|
||||
ctx.report(90, "Done");
|
||||
|
||||
if (settings.mode === "targetSize" && settings.targetSizeKb) {
|
||||
// Binary-search the DPI for the highest quality that still fits the
|
||||
// target. Output size is monotonic in DPI, so the search converges.
|
||||
const targetBytes = settings.targetSizeKb * 1024;
|
||||
let lo = MIN_DPI;
|
||||
let hi = MAX_DPI;
|
||||
let bestPath: string | null = null;
|
||||
for (let i = 0; i < 6 && lo <= hi; i++) {
|
||||
const dpi = Math.round((lo + hi) / 2);
|
||||
const candidate = join(ctx.scratchDir, `cand-${dpi}.pdf`);
|
||||
ctx.report(10 + i * 13, "Compressing");
|
||||
await gsCompressPdfQuality(inPath, candidate, dpi);
|
||||
const size = (await stat(candidate)).size;
|
||||
if (size <= targetBytes) {
|
||||
bestPath = candidate;
|
||||
lo = dpi + 1; // fits: try higher quality
|
||||
} else {
|
||||
hi = dpi - 1; // too big: compress harder
|
||||
}
|
||||
}
|
||||
if (!bestPath) {
|
||||
// Target unreachable (e.g. a text-only PDF below the floor); fall
|
||||
// back to the most aggressive compression we can do.
|
||||
bestPath = join(ctx.scratchDir, "cand-min.pdf");
|
||||
await gsCompressPdfQuality(inPath, bestPath, MIN_DPI);
|
||||
}
|
||||
await copyFile(bestPath, outPath);
|
||||
} else {
|
||||
ctx.report(10, "Compressing");
|
||||
await gsCompressPdfQuality(inPath, outPath, qualityToDpi(settings.quality ?? 75));
|
||||
}
|
||||
|
||||
ctx.report(95, "Done");
|
||||
return {
|
||||
scratchPath: outPath,
|
||||
filename: `${base}_compressed.pdf`,
|
||||
|
||||
@@ -12,6 +12,7 @@ export function registerCreateZip(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "create-zip",
|
||||
maxInputs: 50,
|
||||
minInputs: 2,
|
||||
settingsSchema,
|
||||
process: async () => {
|
||||
throw new Error("create-zip is v2-only");
|
||||
@@ -21,20 +22,22 @@ export function registerCreateZip(app: FastifyInstance) {
|
||||
throw new InputValidationError("Zipping needs at least two files");
|
||||
}
|
||||
|
||||
// Deduplicate filenames: name-1.ext, name-2.ext on collision
|
||||
const usedNames = new Map<string, number>();
|
||||
// Deduplicate output names: append -1, -2, ... until unique. Checking the
|
||||
// generated name (not just the input name) avoids collisions when an input
|
||||
// is literally named like a generated one (e.g. file.txt, file.txt, file-1.txt).
|
||||
const usedNames = new Set<string>();
|
||||
const entryNames: string[] = [];
|
||||
for (const input of ctx.inputs) {
|
||||
const ext = extname(input.filename);
|
||||
const base = input.filename.slice(0, input.filename.length - ext.length) || "file";
|
||||
const key = input.filename.toLowerCase();
|
||||
const count = usedNames.get(key) ?? 0;
|
||||
if (count === 0) {
|
||||
entryNames.push(input.filename);
|
||||
} else {
|
||||
entryNames.push(`${base}-${count}${ext}`);
|
||||
let name = input.filename;
|
||||
let n = 1;
|
||||
while (usedNames.has(name.toLowerCase())) {
|
||||
name = `${base}-${n}${ext}`;
|
||||
n++;
|
||||
}
|
||||
usedNames.set(key, count + 1);
|
||||
usedNames.add(name.toLowerCase());
|
||||
entryNames.push(name);
|
||||
}
|
||||
|
||||
const zipPath = join(ctx.scratchDir, "archive.zip");
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
@@ -47,14 +52,7 @@ export function registerCropVideo(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
`crop=${settings.width}:${settings.height}:${settings.x}:${settings.y}`,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
outPath,
|
||||
|
||||
@@ -39,7 +39,9 @@ export function registerCsvExcel(app: FastifyInstance) {
|
||||
ws.eachRow((row) => {
|
||||
const cells: string[] = [];
|
||||
row.eachCell({ includeEmpty: true }, (cell) => {
|
||||
cells.push(cell.text);
|
||||
// cell.text renders dates via Date.toString() (timezone-dependent and
|
||||
// not round-trippable); emit ISO 8601 for Date values instead.
|
||||
cells.push(cell.value instanceof Date ? cell.value.toISOString() : cell.text);
|
||||
});
|
||||
rows.push(cells);
|
||||
});
|
||||
|
||||
@@ -21,11 +21,31 @@ export function registerCsvJson(app: FastifyInstance) {
|
||||
const lower = input.filename.toLowerCase();
|
||||
|
||||
if (lower.endsWith(".json")) {
|
||||
const data: unknown = JSON.parse(input.buffer.toString("utf8"));
|
||||
let data: unknown;
|
||||
try {
|
||||
data = JSON.parse(input.buffer.toString("utf8"));
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
throw new Error(`Not valid JSON: ${msg.split("\n")[0]}`);
|
||||
}
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error("JSON input must be an array of objects to convert to CSV");
|
||||
}
|
||||
const csv = Papa.unparse(data as Record<string, unknown>[]);
|
||||
if (data.some((r) => r === null || typeof r !== "object" || Array.isArray(r))) {
|
||||
throw new Error("JSON array elements must be objects to convert to CSV");
|
||||
}
|
||||
// Flatten nested objects/arrays to JSON strings (Papa would otherwise emit
|
||||
// "[object Object]"), and pass the union of all keys so columns appearing
|
||||
// only in later rows are not dropped.
|
||||
const flattened = (data as Record<string, unknown>[]).map((row) => {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(row)) {
|
||||
out[k] = v !== null && typeof v === "object" ? JSON.stringify(v) : v;
|
||||
}
|
||||
return out;
|
||||
});
|
||||
const columns = Array.from(new Set(flattened.flatMap((row) => Object.keys(row))));
|
||||
const csv = Papa.unparse(flattened, { columns });
|
||||
return {
|
||||
buffer: Buffer.from(csv, "utf8"),
|
||||
filename: `${base}.csv`,
|
||||
|
||||
@@ -9,6 +9,7 @@ const hexColor = z.string().regex(/^#[0-9a-fA-F]{6}$/);
|
||||
const settingsSchema = z.object({
|
||||
shadow: hexColor.default("#1e3a8a"),
|
||||
highlight: hexColor.default("#fbbf24"),
|
||||
intensity: z.number().int().min(0).max(100).default(100),
|
||||
});
|
||||
|
||||
function parseHex(hex: string) {
|
||||
@@ -26,6 +27,7 @@ export function registerDuotone(app: FastifyInstance) {
|
||||
process: async (inputBuffer, settings, filename) => {
|
||||
const a = parseHex(settings.shadow);
|
||||
const b = parseHex(settings.highlight);
|
||||
const k = settings.intensity / 100;
|
||||
|
||||
// Duotone math: output = shadow + (highlight - shadow) * luminance
|
||||
// .linear(multipliers, offsets) with per-channel arrays
|
||||
@@ -40,7 +42,34 @@ export function registerDuotone(app: FastifyInstance) {
|
||||
.toColourspace("srgb")
|
||||
.toBuffer();
|
||||
|
||||
const buf = await sharp(grayBuf).linear(multipliers, offsets).toBuffer();
|
||||
let buf = await sharp(grayBuf).linear(multipliers, offsets).toBuffer();
|
||||
|
||||
// Blend duotone with original when intensity < 100. Both buffers are read
|
||||
// through the same removeAlpha + sRGB pipeline so they share channel count
|
||||
// and length, and the blended raw buffer is re-encoded to PNG so the final
|
||||
// toFormat() step can decode it again.
|
||||
if (k < 1) {
|
||||
const origRaw = await sharp(inputBuffer)
|
||||
.removeAlpha()
|
||||
.toColourspace("srgb")
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
const duo = await sharp(buf).removeAlpha().toColourspace("srgb").raw().toBuffer();
|
||||
const pixels = origRaw.data;
|
||||
const blended = Buffer.alloc(pixels.length);
|
||||
for (let i = 0; i < pixels.length; i++) {
|
||||
blended[i] = Math.round(pixels[i] * (1 - k) + duo[i] * k);
|
||||
}
|
||||
buf = await sharp(blended, {
|
||||
raw: {
|
||||
width: origRaw.info.width,
|
||||
height: origRaw.info.height,
|
||||
channels: origRaw.info.channels as 1 | 2 | 3 | 4,
|
||||
},
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
|
||||
const buffer = await sharp(buf)
|
||||
|
||||
@@ -53,22 +53,22 @@ function readEntryBuffer(zipfile: ZipFile, entry: Entry): Promise<Buffer> {
|
||||
});
|
||||
}
|
||||
|
||||
/** Deduplicate basenames: name-1.ext, name-2.ext on collision. */
|
||||
/** Deduplicate basenames: name-1.ext, name-2.ext until each output is unique. */
|
||||
function deduplicateNames(names: string[]): string[] {
|
||||
const usedNames = new Map<string, number>();
|
||||
const usedNames = new Set<string>();
|
||||
const result: string[] = [];
|
||||
for (const raw of names) {
|
||||
const name = basename(raw);
|
||||
const ext = extname(name);
|
||||
const base = name.slice(0, name.length - ext.length) || "file";
|
||||
const key = name.toLowerCase();
|
||||
const count = usedNames.get(key) ?? 0;
|
||||
if (count === 0) {
|
||||
result.push(name);
|
||||
} else {
|
||||
result.push(`${base}-${count}${ext}`);
|
||||
let candidate = name;
|
||||
let n = 1;
|
||||
while (usedNames.has(candidate.toLowerCase())) {
|
||||
candidate = `${base}-${n}${ext}`;
|
||||
n++;
|
||||
}
|
||||
usedNames.set(key, count + 1);
|
||||
usedNames.add(candidate.toLowerCase());
|
||||
result.push(candidate);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -77,6 +77,39 @@ export function registerExtractZip(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "extract-zip",
|
||||
settingsSchema,
|
||||
// Reject path-traversal / absolute-path entries pre-enqueue with a clean 400.
|
||||
// (yauzl also blocks them, but only as a generic worker 422; the processV2
|
||||
// guards below remain as defense-in-depth for the pipeline/batch path.)
|
||||
preValidate: async ({ inputs }) => {
|
||||
const buf = inputs[0]?.buffer;
|
||||
if (!buf) return;
|
||||
let entries: Entry[];
|
||||
try {
|
||||
entries = await collectEntries(await openZipBuffer(buf));
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
if (/relative path|absolute path/i.test(msg)) {
|
||||
throw new InputValidationError(
|
||||
"This archive contains an unsafe entry path (path traversal or absolute) and was rejected",
|
||||
);
|
||||
}
|
||||
throw new InputValidationError(
|
||||
"Could not read the archive; it may be corrupt or not a valid .zip",
|
||||
);
|
||||
}
|
||||
for (const e of entries) {
|
||||
const name = e.fileName;
|
||||
if (
|
||||
name.startsWith("/") ||
|
||||
name.startsWith("\\") ||
|
||||
name.split(/[/\\]/).some((s) => s === "..")
|
||||
) {
|
||||
throw new InputValidationError(
|
||||
"This archive contains an unsafe entry path (path traversal or absolute) and was rejected",
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
process: async () => {
|
||||
throw new Error("extract-zip is v2-only");
|
||||
},
|
||||
@@ -101,6 +134,10 @@ export function registerExtractZip(app: FastifyInstance) {
|
||||
fileEntries.push(entry);
|
||||
}
|
||||
|
||||
if (fileEntries.length === 0) {
|
||||
throw new InputValidationError("No extractable files found in the archive");
|
||||
}
|
||||
|
||||
// Guard: entry count
|
||||
if (fileEntries.length > MAX_ENTRIES) {
|
||||
throw new InputValidationError("Too many entries");
|
||||
|
||||
@@ -16,7 +16,21 @@ import { encodeMultiIco, hasMagick } from "../../lib/format-encoders.js";
|
||||
import { decodeHeic } from "../../lib/heic-converter.js";
|
||||
import { decompressSvgz, sanitizeSvg } from "../../lib/svg-sanitize.js";
|
||||
|
||||
const settingsSchema = z.object({}).passthrough();
|
||||
const settingsSchema = z.object({
|
||||
background: z
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.optional(),
|
||||
padding: z.number().int().min(0).max(40).default(0),
|
||||
radius: z.number().int().min(0).max(50).default(0),
|
||||
sizes: z.array(z.number().int()).optional(),
|
||||
themeColor: z
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#ffffff"),
|
||||
});
|
||||
|
||||
type FaviconSettings = z.infer<typeof settingsSchema>;
|
||||
|
||||
const FAVICON_SIZES = [
|
||||
{ name: "favicon-16x16.png", size: 16, format: "png" as const },
|
||||
@@ -27,6 +41,38 @@ const FAVICON_SIZES = [
|
||||
{ name: "android-chrome-512x512.png", size: 512, format: "png" as const },
|
||||
];
|
||||
|
||||
/** Build a single icon at the given pixel size with styling applied. */
|
||||
async function buildIcon(source: Buffer, size: number, settings: FaviconSettings): Promise<Buffer> {
|
||||
const inset = settings.padding > 0 ? Math.round((size * settings.padding) / 100) : 0;
|
||||
const contentSize = Math.max(1, size - 2 * inset);
|
||||
|
||||
let pipeline = sharp(source).resize(contentSize, contentSize, { fit: "cover" });
|
||||
|
||||
if (settings.background) {
|
||||
pipeline = pipeline.flatten({ background: settings.background });
|
||||
}
|
||||
|
||||
if (inset > 0) {
|
||||
pipeline = pipeline.extend({
|
||||
top: inset,
|
||||
bottom: inset,
|
||||
left: inset,
|
||||
right: inset,
|
||||
background: settings.background || { r: 0, g: 0, b: 0, alpha: 0 },
|
||||
});
|
||||
}
|
||||
|
||||
if (settings.radius > 0) {
|
||||
const rx = Math.round((size * settings.radius) / 100);
|
||||
const mask = Buffer.from(
|
||||
`<svg width="${size}" height="${size}"><rect x="0" y="0" width="${size}" height="${size}" rx="${rx}" ry="${rx}" fill="white"/></svg>`,
|
||||
);
|
||||
pipeline = pipeline.ensureAlpha().composite([{ input: mask, blend: "dest-in" }]);
|
||||
}
|
||||
|
||||
return pipeline.png().toBuffer();
|
||||
}
|
||||
|
||||
interface UploadedFile {
|
||||
buffer: Buffer;
|
||||
filename: string;
|
||||
@@ -135,6 +181,8 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
});
|
||||
}
|
||||
|
||||
let settings: FaviconSettings = { padding: 0, radius: 0, themeColor: "#ffffff" };
|
||||
|
||||
if (settingsRaw) {
|
||||
try {
|
||||
const parsed = JSON.parse(settingsRaw);
|
||||
@@ -144,6 +192,7 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
.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" });
|
||||
}
|
||||
@@ -152,6 +201,9 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
try {
|
||||
const jobId = randomUUID();
|
||||
const isSingleFile = decodedFiles.length === 1;
|
||||
const filteredSizes = settings.sizes
|
||||
? FAVICON_SIZES.filter((s) => settings.sizes?.includes(s.size))
|
||||
: FAVICON_SIZES;
|
||||
|
||||
reply.hijack();
|
||||
reply.raw.writeHead(200, {
|
||||
@@ -168,11 +220,8 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
const stem = sanitizeFilename(file.filename).replace(/\.[^.]+$/, "");
|
||||
const prefix = isSingleFile ? "" : `${stem}/`;
|
||||
|
||||
for (const icon of FAVICON_SIZES) {
|
||||
const buffer = await sharp(file.buffer)
|
||||
.resize(icon.size, icon.size, { fit: "cover" })
|
||||
.png()
|
||||
.toBuffer();
|
||||
for (const icon of filteredSizes) {
|
||||
const buffer = await buildIcon(file.buffer, icon.size, settings);
|
||||
archive.append(buffer, { name: `${prefix}${icon.name}` });
|
||||
}
|
||||
|
||||
@@ -187,10 +236,7 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
try {
|
||||
for (const sz of icoSizes) {
|
||||
const pngPath = join(tmpdir(), `favicon-${icoId}-${sz}.png`);
|
||||
const buf = await sharp(file.buffer)
|
||||
.resize(sz, sz, { fit: "cover" })
|
||||
.png()
|
||||
.toBuffer();
|
||||
const buf = await buildIcon(file.buffer, sz, settings);
|
||||
await writeFile(pngPath, buf);
|
||||
icoPaths.push(pngPath);
|
||||
}
|
||||
@@ -203,31 +249,44 @@ export function registerFavicon(app: FastifyInstance) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const ico32 = await sharp(file.buffer).resize(32, 32, { fit: "cover" }).png().toBuffer();
|
||||
const ico32 = await buildIcon(file.buffer, 32, settings);
|
||||
archive.append(ico32, { name: `${prefix}favicon.ico` });
|
||||
}
|
||||
|
||||
const manifestIcons = filteredSizes
|
||||
.filter((s) => s.size === 192 || s.size === 512)
|
||||
.map((s) => ({
|
||||
src: `/${s.name}`,
|
||||
sizes: `${s.size}x${s.size}`,
|
||||
type: "image/png",
|
||||
}));
|
||||
const manifest = {
|
||||
name: stem,
|
||||
short_name: stem,
|
||||
icons: [
|
||||
{ src: "/android-chrome-192x192.png", sizes: "192x192", type: "image/png" },
|
||||
{ src: "/android-chrome-512x512.png", sizes: "512x512", type: "image/png" },
|
||||
],
|
||||
theme_color: "#ffffff",
|
||||
background_color: "#ffffff",
|
||||
icons: manifestIcons,
|
||||
theme_color: settings.themeColor,
|
||||
background_color: settings.themeColor,
|
||||
display: "standalone",
|
||||
};
|
||||
archive.append(JSON.stringify(manifest, null, 2), { name: `${prefix}manifest.json` });
|
||||
|
||||
const htmlSnippet = `<!-- Favicons -->
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
|
||||
<link rel="icon" type="image/png" sizes="48x48" href="/favicon-48x48.png">
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
`;
|
||||
archive.append(htmlSnippet, { name: `${prefix}favicon-snippet.html` });
|
||||
const snippetLines = ["<!-- Favicons -->"];
|
||||
for (const s of filteredSizes) {
|
||||
if (s.name.startsWith("android-chrome")) continue;
|
||||
if (s.name === "apple-touch-icon.png") {
|
||||
snippetLines.push(
|
||||
`<link rel="apple-touch-icon" sizes="${s.size}x${s.size}" href="/${s.name}">`,
|
||||
);
|
||||
} else {
|
||||
snippetLines.push(
|
||||
`<link rel="icon" type="image/png" sizes="${s.size}x${s.size}" href="/${s.name}">`,
|
||||
);
|
||||
}
|
||||
}
|
||||
snippetLines.push('<link rel="manifest" href="/manifest.json">');
|
||||
archive.append(`${snippetLines.join("\n")}\n`, {
|
||||
name: `${prefix}favicon-snippet.html`,
|
||||
});
|
||||
}
|
||||
|
||||
if (skippedFiles.length > 0) {
|
||||
|
||||
@@ -5,13 +5,17 @@ import { z } from "zod";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({});
|
||||
const settingsSchema = z.object({
|
||||
quality: z.number().int().min(1).max(100).default(80),
|
||||
lossless: z.boolean().default(false),
|
||||
resizePercent: z.number().int().min(10).max(100).default(100),
|
||||
});
|
||||
|
||||
export function registerGifWebp(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "gif-webp",
|
||||
settingsSchema,
|
||||
process: async (inputBuffer, _settings, filename) => {
|
||||
process: async (inputBuffer, settings, filename) => {
|
||||
const ext = extname(filename).toLowerCase();
|
||||
|
||||
// Route-level extension guard: image modality has no 415 gate
|
||||
@@ -19,10 +23,23 @@ export function registerGifWebp(app: FastifyInstance) {
|
||||
throw new InputValidationError("Only GIF and WebP inputs are supported");
|
||||
}
|
||||
|
||||
let pipeline = sharp(inputBuffer, { animated: true });
|
||||
|
||||
// Apply resize when below 100%
|
||||
if (settings.resizePercent < 100) {
|
||||
const meta = await sharp(inputBuffer, { animated: true }).metadata();
|
||||
const origW = meta.width ?? 1;
|
||||
const target = Math.round(origW * (settings.resizePercent / 100));
|
||||
pipeline = pipeline.resize(target);
|
||||
}
|
||||
|
||||
const base = filename.replace(/\.[^.]+$/, "");
|
||||
|
||||
if (ext === ".gif") {
|
||||
// GIF -> WebP (preserving animation)
|
||||
const buffer = await sharp(inputBuffer, { animated: true }).webp().toBuffer();
|
||||
const base = filename.replace(/\.[^.]+$/, "");
|
||||
const buffer = await pipeline
|
||||
.webp({ quality: settings.quality, lossless: settings.lossless })
|
||||
.toBuffer();
|
||||
return {
|
||||
buffer,
|
||||
filename: `${base}.webp`,
|
||||
@@ -31,8 +48,8 @@ export function registerGifWebp(app: FastifyInstance) {
|
||||
}
|
||||
|
||||
// WebP -> GIF (preserving animation)
|
||||
const buffer = await sharp(inputBuffer, { animated: true }).gif().toBuffer();
|
||||
const base = filename.replace(/\.[^.]+$/, "");
|
||||
// Note: quality and lossless are WebP-only; GIF uses a fixed palette.
|
||||
const buffer = await pipeline.gif().toBuffer();
|
||||
return {
|
||||
buffer,
|
||||
filename: `${base}.gif`,
|
||||
|
||||
@@ -3,7 +3,30 @@ import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({});
|
||||
const settingsSchema = z
|
||||
.object({
|
||||
scale: z.enum(["linear", "log"]).default("linear"),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
function medianFromBins(bins: Uint32Array, total: number): number {
|
||||
const half = total / 2;
|
||||
let cumulative = 0;
|
||||
for (let i = 0; i < 256; i++) {
|
||||
cumulative += bins[i];
|
||||
if (cumulative >= half) return i;
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
|
||||
function stdevFromBins(bins: Uint32Array, mean: number, total: number): number {
|
||||
let sumSqDiff = 0;
|
||||
for (let i = 0; i < 256; i++) {
|
||||
const diff = i - mean;
|
||||
sumSqDiff += diff * diff * bins[i];
|
||||
}
|
||||
return Math.round(Math.sqrt(sumSqDiff / total) * 100) / 100;
|
||||
}
|
||||
|
||||
export function registerHistogram(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
@@ -22,14 +45,16 @@ export function registerHistogram(app: FastifyInstance) {
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
// Build 256-bin histograms per channel in a single pass
|
||||
// Build 256-bin histograms per channel + luminance in a single pass
|
||||
const rBins = new Uint32Array(256);
|
||||
const gBins = new Uint32Array(256);
|
||||
const bBins = new Uint32Array(256);
|
||||
const lumBins = new Uint32Array(256);
|
||||
|
||||
let rSum = 0;
|
||||
let gSum = 0;
|
||||
let bSum = 0;
|
||||
let lumSum = 0;
|
||||
const pixelCount = data.length / 3;
|
||||
|
||||
for (let i = 0; i < data.length; i += 3) {
|
||||
@@ -42,9 +67,12 @@ export function registerHistogram(app: FastifyInstance) {
|
||||
rSum += r;
|
||||
gSum += g;
|
||||
bSum += b;
|
||||
const lum = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
|
||||
lumBins[lum]++;
|
||||
lumSum += lum;
|
||||
}
|
||||
|
||||
// Find max bin value for normalization
|
||||
// Find max bin value for normalization (RGB only for the PNG)
|
||||
let maxBin = 0;
|
||||
let rMax = 0;
|
||||
let gMax = 0;
|
||||
@@ -58,6 +86,35 @@ export function registerHistogram(app: FastifyInstance) {
|
||||
if (bBins[i] > bMax) bMax = bBins[i];
|
||||
}
|
||||
|
||||
// Per-channel statistics
|
||||
const rMean = Math.round(rSum / pixelCount);
|
||||
const gMean = Math.round(gSum / pixelCount);
|
||||
const bMean = Math.round(bSum / pixelCount);
|
||||
const lumMean = Math.round(lumSum / pixelCount);
|
||||
|
||||
const stats = {
|
||||
r: {
|
||||
mean: rMean,
|
||||
median: medianFromBins(rBins, pixelCount),
|
||||
stdev: stdevFromBins(rBins, rSum / pixelCount, pixelCount),
|
||||
},
|
||||
g: {
|
||||
mean: gMean,
|
||||
median: medianFromBins(gBins, pixelCount),
|
||||
stdev: stdevFromBins(gBins, gSum / pixelCount, pixelCount),
|
||||
},
|
||||
b: {
|
||||
mean: bMean,
|
||||
median: medianFromBins(bBins, pixelCount),
|
||||
stdev: stdevFromBins(bBins, bSum / pixelCount, pixelCount),
|
||||
},
|
||||
lum: {
|
||||
mean: lumMean,
|
||||
median: medianFromBins(lumBins, pixelCount),
|
||||
stdev: stdevFromBins(lumBins, lumSum / pixelCount, pixelCount),
|
||||
},
|
||||
};
|
||||
|
||||
// Render a 512x320 SVG with three semi-transparent polylines
|
||||
const svgW = 512;
|
||||
const svgH = 320;
|
||||
@@ -92,16 +149,16 @@ export function registerHistogram(app: FastifyInstance) {
|
||||
filename: `${base}_histogram.png`,
|
||||
contentType: "image/png",
|
||||
resultPayload: {
|
||||
mean: {
|
||||
r: Math.round(rSum / pixelCount),
|
||||
g: Math.round(gSum / pixelCount),
|
||||
b: Math.round(bSum / pixelCount),
|
||||
},
|
||||
max: {
|
||||
r: rMax,
|
||||
g: gMax,
|
||||
b: bMax,
|
||||
bins: {
|
||||
r: Array.from(rBins),
|
||||
g: Array.from(gBins),
|
||||
b: Array.from(bBins),
|
||||
lum: Array.from(lumBins),
|
||||
},
|
||||
stats,
|
||||
// Backward-compat fields
|
||||
mean: { r: rMean, g: gMean, b: bMean },
|
||||
max: { r: rMax, g: gMax, b: bMax },
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@@ -5,11 +5,15 @@ import { resolveOutputFormat } from "../../lib/output-format.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
target: z.enum(["16:9", "9:16", "1:1", "4:3", "3:4"]).default("1:1"),
|
||||
target: z.enum(["16:9", "9:16", "1:1", "4:3", "3:4", "custom"]).default("1:1"),
|
||||
ratioW: z.number().int().min(1).max(100).default(1),
|
||||
ratioH: z.number().int().min(1).max(100).default(1),
|
||||
background: z.enum(["color", "transparent", "blur"]).default("color"),
|
||||
color: z
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#ffffff"),
|
||||
padding: z.number().int().min(0).max(50).default(0),
|
||||
});
|
||||
|
||||
/** Compute canvas dimensions for the given target aspect ratio. */
|
||||
@@ -51,25 +55,72 @@ export function registerImagePad(app: FastifyInstance) {
|
||||
const w = meta.width ?? 1;
|
||||
const h = meta.height ?? 1;
|
||||
|
||||
const { cw, ch } = canvasFor(w, h, settings.target);
|
||||
const c = parseHex(settings.color);
|
||||
// Resolve target ratio -- custom uses ratioW:ratioH
|
||||
const ratioStr =
|
||||
settings.target === "custom" ? `${settings.ratioW}:${settings.ratioH}` : settings.target;
|
||||
|
||||
const padTop = Math.floor((ch - h) / 2);
|
||||
const padBottom = ch - h - padTop;
|
||||
const padLeft = Math.floor((cw - w) / 2);
|
||||
const padRight = cw - w - padLeft;
|
||||
const { cw, ch } = canvasFor(w, h, ratioStr);
|
||||
|
||||
const buf = await sharp(inputBuffer)
|
||||
.extend({
|
||||
top: padTop,
|
||||
bottom: padBottom,
|
||||
left: padLeft,
|
||||
right: padRight,
|
||||
background: { r: c.r, g: c.g, b: c.b, alpha: 1 },
|
||||
})
|
||||
.toBuffer();
|
||||
// Extra uniform padding margin (% of the canvas larger side)
|
||||
const margin =
|
||||
settings.padding > 0 ? Math.round((Math.max(cw, ch) * settings.padding) / 100) : 0;
|
||||
const finalW = cw + margin * 2;
|
||||
const finalH = ch + margin * 2;
|
||||
|
||||
const padTop = Math.floor((finalH - h) / 2);
|
||||
const padBottom = finalH - h - padTop;
|
||||
const padLeft = Math.floor((finalW - w) / 2);
|
||||
const padRight = finalW - w - padLeft;
|
||||
|
||||
let buf: Buffer;
|
||||
|
||||
if (settings.background === "blur") {
|
||||
// Instagram-style: blurred cover fill + sharp original composited on top
|
||||
const blurred = await sharp(inputBuffer)
|
||||
.resize(finalW, finalH, { fit: "cover" })
|
||||
.blur(20)
|
||||
.png()
|
||||
.toBuffer();
|
||||
buf = await sharp(blurred)
|
||||
.composite([{ input: inputBuffer, top: padTop, left: padLeft }])
|
||||
.png()
|
||||
.toBuffer();
|
||||
} else if (settings.background === "transparent") {
|
||||
buf = await sharp(inputBuffer)
|
||||
.ensureAlpha()
|
||||
.extend({
|
||||
top: padTop,
|
||||
bottom: padBottom,
|
||||
left: padLeft,
|
||||
right: padRight,
|
||||
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
||||
})
|
||||
.png()
|
||||
.toBuffer();
|
||||
} else {
|
||||
const c = parseHex(settings.color);
|
||||
buf = await sharp(inputBuffer)
|
||||
.extend({
|
||||
top: padTop,
|
||||
bottom: padBottom,
|
||||
left: padLeft,
|
||||
right: padRight,
|
||||
background: { r: c.r, g: c.g, b: c.b, alpha: 1 },
|
||||
})
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
// Transparent forces PNG to preserve alpha; otherwise detect from input
|
||||
const forcePng = settings.background === "transparent";
|
||||
const outputFormat = forcePng
|
||||
? {
|
||||
format: "png" as const,
|
||||
extension: "png",
|
||||
contentType: "image/png",
|
||||
quality: 95,
|
||||
}
|
||||
: await resolveOutputFormat(inputBuffer, filename);
|
||||
|
||||
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
|
||||
const buffer = await sharp(buf)
|
||||
.toFormat(outputFormat.format, { quality: outputFormat.quality })
|
||||
.toBuffer();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { XMLBuilder, XMLParser } from "fast-xml-parser";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
@@ -34,14 +35,22 @@ export function registerJsonXml(app: FastifyInstance) {
|
||||
}
|
||||
|
||||
// json -> xml
|
||||
const data: unknown = JSON.parse(text);
|
||||
// Wrap in a root element when the top level is an array or has
|
||||
// multiple keys, so the XML is well-formed with a single root.
|
||||
let data: unknown;
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (err: unknown) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
throw new InputValidationError(`Not valid JSON: ${msg.split("\n")[0]}`);
|
||||
}
|
||||
// The builder needs an object/array root; primitives (null, string, number,
|
||||
// boolean) crash builder.build or produce per-character garbage XML.
|
||||
if (data === null || typeof data !== "object") {
|
||||
throw new InputValidationError("JSON must be an object or array to convert to XML");
|
||||
}
|
||||
// Wrap in a root element when the top level is an array or has multiple
|
||||
// keys, so the XML is well-formed with a single root.
|
||||
const wrapped =
|
||||
Array.isArray(data) ||
|
||||
(typeof data === "object" && data !== null && Object.keys(data).length !== 1)
|
||||
? { root: data }
|
||||
: data;
|
||||
Array.isArray(data) || Object.keys(data).length !== 1 ? { root: data } : data;
|
||||
const builder = new XMLBuilder({ format: settings.pretty, ignoreAttributes: false });
|
||||
const xml = builder.build(wrapped) as string;
|
||||
return {
|
||||
|
||||
@@ -6,8 +6,29 @@ import { createToolRoute } from "../tool-factory.js";
|
||||
const settingsSchema = z.object({
|
||||
width: z.number().int().min(4).max(64).default(16),
|
||||
blur: z.number().min(0).max(20).default(2),
|
||||
strategy: z.enum(["blur", "pixelate", "solid"]).default("blur"),
|
||||
format: z.enum(["webp", "png", "jpeg"]).default("webp"),
|
||||
quality: z.number().int().min(1).max(100).default(50),
|
||||
});
|
||||
|
||||
const MIME: Record<string, string> = {
|
||||
webp: "image/webp",
|
||||
png: "image/png",
|
||||
jpeg: "image/jpeg",
|
||||
};
|
||||
|
||||
const EXT: Record<string, string> = {
|
||||
webp: ".webp",
|
||||
png: ".png",
|
||||
jpeg: ".jpg",
|
||||
};
|
||||
|
||||
function encode(pipeline: sharp.Sharp, fmt: string, q: number): sharp.Sharp {
|
||||
if (fmt === "jpeg") return pipeline.jpeg({ quality: q });
|
||||
if (fmt === "png") return pipeline.png();
|
||||
return pipeline.webp({ quality: q });
|
||||
}
|
||||
|
||||
export function registerLqipPlaceholder(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "lqip-placeholder",
|
||||
@@ -20,27 +41,47 @@ export function registerLqipPlaceholder(app: FastifyInstance) {
|
||||
const inputBuffer = ctx.inputs[0].buffer;
|
||||
const filename = ctx.inputs[0].filename;
|
||||
|
||||
let pipeline = sharp(inputBuffer).resize(settings.width);
|
||||
let pipeline: sharp.Sharp;
|
||||
|
||||
if (settings.blur > 0) {
|
||||
pipeline = pipeline.blur(settings.blur);
|
||||
if (settings.strategy === "solid") {
|
||||
const pixel = await sharp(inputBuffer).resize(1, 1).raw().toBuffer();
|
||||
pipeline = sharp({
|
||||
create: {
|
||||
width: settings.width,
|
||||
height: settings.width,
|
||||
channels: 3,
|
||||
background: { r: pixel[0], g: pixel[1], b: pixel[2] },
|
||||
},
|
||||
});
|
||||
} else if (settings.strategy === "pixelate") {
|
||||
pipeline = sharp(inputBuffer).resize(settings.width, null, {
|
||||
kernel: sharp.kernel.nearest,
|
||||
});
|
||||
} else {
|
||||
pipeline = sharp(inputBuffer).resize(settings.width);
|
||||
if (settings.blur > 0) {
|
||||
pipeline = pipeline.blur(settings.blur);
|
||||
}
|
||||
}
|
||||
|
||||
const buffer = await pipeline.webp({ quality: 50 }).toBuffer();
|
||||
|
||||
const buffer = await encode(pipeline, settings.format, settings.quality).toBuffer();
|
||||
const meta = await sharp(buffer).metadata();
|
||||
const dataUri = `data:image/webp;base64,${buffer.toString("base64")}`;
|
||||
const mime = MIME[settings.format];
|
||||
const dataUri = `data:${mime};base64,${buffer.toString("base64")}`;
|
||||
|
||||
const base = filename.replace(/\.[^.]+$/, "");
|
||||
return {
|
||||
buffer,
|
||||
filename: `${base}_lqip.webp`,
|
||||
contentType: "image/webp",
|
||||
filename: `${base}_lqip${EXT[settings.format]}`,
|
||||
contentType: mime,
|
||||
resultPayload: {
|
||||
dataUri,
|
||||
width: meta.width ?? settings.width,
|
||||
height: meta.height ?? 0,
|
||||
bytes: buffer.length,
|
||||
strategy: settings.strategy,
|
||||
html: `<img src="${dataUri}" />`,
|
||||
css: `background-image:url('${dataUri}');background-size:cover;background-position:center;`,
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@@ -10,6 +10,7 @@ export function registerMergeCsvs(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "merge-csvs",
|
||||
maxInputs: 20,
|
||||
minInputs: 2,
|
||||
settingsSchema,
|
||||
process: async () => {
|
||||
throw new Error("merge-csvs is v2-only");
|
||||
|
||||
@@ -30,12 +30,19 @@ export function registerPixelate(app: FastifyInstance) {
|
||||
let buf: Buffer;
|
||||
|
||||
if (settings.region) {
|
||||
const r = settings.region;
|
||||
// Validate region bounds
|
||||
if (r.left + r.width > w || r.top + r.height > h) {
|
||||
// Reject if origin is completely outside image bounds
|
||||
if (settings.region.left >= w || settings.region.top >= h) {
|
||||
throw new InputValidationError("Region exceeds image bounds");
|
||||
}
|
||||
|
||||
// Clamp region dimensions to image edges (handles rounding from normalized coords)
|
||||
const r = {
|
||||
left: settings.region.left,
|
||||
top: settings.region.top,
|
||||
width: Math.min(settings.region.width, w - settings.region.left),
|
||||
height: Math.min(settings.region.height, h - settings.region.top),
|
||||
};
|
||||
|
||||
// Extract region, pixelate it, composite back
|
||||
const rw = Math.max(1, Math.round(r.width / bs));
|
||||
const rh = Math.max(1, Math.round(r.height / bs));
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { basename, extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
audioEncodeArgsForContainer,
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
@@ -54,8 +59,7 @@ export function registerReplaceAudio(app: FastifyInstance) {
|
||||
"1:a:0",
|
||||
"-c:v",
|
||||
"copy",
|
||||
"-c:a",
|
||||
resolveEncoder("aac"),
|
||||
...audioEncodeArgsForContainer(ext),
|
||||
"-shortest",
|
||||
outPath,
|
||||
];
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { extname } from "node:path";
|
||||
import { resolveEncoder } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const PRESET_HEIGHTS: Record<string, number> = {
|
||||
@@ -53,14 +56,7 @@ export function registerResizeVideo(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
`scale=${w}:${h}:flags=lanczos`,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
out,
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
audioEncodeArgsForContainer,
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
@@ -41,16 +47,8 @@ export function registerReverseVideo(app: FastifyInstance) {
|
||||
"reverse",
|
||||
"-af",
|
||||
"areverse",
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
"-c:a",
|
||||
resolveEncoder("aac"),
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
...audioEncodeArgsForContainer(origExt),
|
||||
outPath,
|
||||
];
|
||||
} else {
|
||||
@@ -60,14 +58,7 @@ export function registerReverseVideo(app: FastifyInstance) {
|
||||
"-vf",
|
||||
"reverse",
|
||||
"-an",
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
outPath,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { extname } from "node:path";
|
||||
import { resolveEncoder } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const VF_MAP: Record<string, string> = {
|
||||
@@ -36,14 +39,7 @@ export function registerRotateVideo(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
VF_MAP[settings.transform],
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
out,
|
||||
|
||||
@@ -36,8 +36,13 @@ export function registerSplitCsv(app: FastifyInstance) {
|
||||
throw new Error("CSV file is empty");
|
||||
}
|
||||
|
||||
const header = settings.keepHeader ? allRows[0] : null;
|
||||
const dataRows = settings.keepHeader ? allRows.slice(1) : allRows;
|
||||
// Always treat row 0 as the header; keepHeader only controls whether it is
|
||||
// repeated into each part (false = parts contain data rows only).
|
||||
const header = allRows[0];
|
||||
const dataRows = allRows.slice(1);
|
||||
if (dataRows.length === 0) {
|
||||
throw new Error("No data rows to split");
|
||||
}
|
||||
|
||||
// Chunk data rows
|
||||
const chunks: string[][][] = [];
|
||||
@@ -48,7 +53,7 @@ export function registerSplitCsv(app: FastifyInstance) {
|
||||
// Write part files to scratch
|
||||
const partPaths: string[] = [];
|
||||
for (let i = 0; i < chunks.length; i++) {
|
||||
const rows = header ? [header, ...chunks[i]] : chunks[i];
|
||||
const rows = settings.keepHeader ? [header, ...chunks[i]] : chunks[i];
|
||||
const csv = Papa.unparse(rows);
|
||||
const partPath = join(ctx.scratchDir, `part-${i + 1}.csv`);
|
||||
await writeFile(partPath, csv, "utf8");
|
||||
|
||||
@@ -11,6 +11,8 @@ const settingsSchema = z.object({
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#ffffff"),
|
||||
format: z.enum(["png", "webp", "jpeg"]).default("png"),
|
||||
quality: z.number().int().min(1).max(100).default(90),
|
||||
});
|
||||
|
||||
function parseHex(hex: string) {
|
||||
@@ -78,23 +80,47 @@ export function registerSpriteSheet(app: FastifyInstance) {
|
||||
frames.push({ index: i, left, top, width: cellW, height: cellH });
|
||||
}
|
||||
|
||||
const buffer = await sharp({
|
||||
let pipeline = sharp({
|
||||
create: {
|
||||
width: canvasW,
|
||||
height: canvasH,
|
||||
channels: 4,
|
||||
background: { r: bg.r, g: bg.g, b: bg.b, alpha: 1 },
|
||||
},
|
||||
})
|
||||
.composite(composites)
|
||||
.png()
|
||||
.toBuffer();
|
||||
}).composite(composites);
|
||||
|
||||
const fmt = settings.format;
|
||||
let filename: string;
|
||||
let contentType: string;
|
||||
if (fmt === "webp") {
|
||||
pipeline = pipeline.webp({ quality: settings.quality });
|
||||
filename = "sprite.webp";
|
||||
contentType = "image/webp";
|
||||
} else if (fmt === "jpeg") {
|
||||
pipeline = pipeline.jpeg({ quality: settings.quality });
|
||||
filename = "sprite.jpg";
|
||||
contentType = "image/jpeg";
|
||||
} else {
|
||||
pipeline = pipeline.png();
|
||||
filename = "sprite.png";
|
||||
contentType = "image/png";
|
||||
}
|
||||
|
||||
const buffer = await pipeline.toBuffer();
|
||||
|
||||
return {
|
||||
buffer,
|
||||
filename: "sprite.png",
|
||||
contentType: "image/png",
|
||||
resultPayload: { frames },
|
||||
filename,
|
||||
contentType,
|
||||
resultPayload: {
|
||||
frames,
|
||||
cols,
|
||||
rows,
|
||||
cellWidth: cellW,
|
||||
cellHeight: cellH,
|
||||
canvasWidth: canvasW,
|
||||
canvasHeight: canvasH,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { extname } from "node:path";
|
||||
import { resolveEncoder } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
audioEncodeArgsForContainer,
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z
|
||||
@@ -36,16 +40,8 @@ export function registerTrimVideo(app: FastifyInstance) {
|
||||
String(settings.startS),
|
||||
"-to",
|
||||
String(settings.endS),
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
"-c:a",
|
||||
resolveEncoder("aac"),
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
...audioEncodeArgsForContainer(origExt),
|
||||
out,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { extname } from "node:path";
|
||||
import { resolveEncoder } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
@@ -31,14 +34,7 @@ export function registerVideoColor(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
`eq=brightness=${settings.brightness}:contrast=${settings.contrast}:saturation=${settings.saturation}:gamma=${settings.gamma}`,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
out,
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runFfmpegWithProgress, stageMediaInputs, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
audioEncodeArgsForContainer,
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { InputValidationError } from "../../modality/contract.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
@@ -30,17 +35,18 @@ export function registerVideoLoudnorm(app: FastifyInstance) {
|
||||
|
||||
const outPath = join(ctx.scratchDir, "media", outName);
|
||||
|
||||
// loudnorm runs internally at 192 kHz and emits at 192 kHz unless we
|
||||
// resample back, so restore the source rate to avoid inflating the audio.
|
||||
const sr = info.streams.find((s) => s.type === "audio")?.sampleRate ?? 48000;
|
||||
|
||||
const args = [
|
||||
"-i",
|
||||
inPath,
|
||||
"-af",
|
||||
"loudnorm=I=-16:TP=-1.5:LRA=11",
|
||||
`loudnorm=I=-16:TP=-1.5:LRA=11,aresample=${sr}`,
|
||||
"-c:v",
|
||||
"copy",
|
||||
"-c:a",
|
||||
resolveEncoder("aac"),
|
||||
"-b:a",
|
||||
"192k",
|
||||
...audioEncodeArgsForContainer(origExt),
|
||||
outPath,
|
||||
];
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { extname, join } from "node:path";
|
||||
import { probeMedia, resolveEncoder } from "@snapotter/media-engine";
|
||||
import { probeMedia } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
audioEncodeArgsForContainer,
|
||||
buildAtempoChain,
|
||||
runFfmpegWithProgress,
|
||||
stageMediaInputs,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
@@ -54,16 +56,8 @@ export function registerVideoSpeed(app: FastifyInstance) {
|
||||
"[v]",
|
||||
"-map",
|
||||
"[a]",
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
"-c:a",
|
||||
resolveEncoder("aac"),
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
...audioEncodeArgsForContainer(origExt),
|
||||
];
|
||||
} else {
|
||||
args = [
|
||||
@@ -72,14 +66,7 @@ export function registerVideoSpeed(app: FastifyInstance) {
|
||||
"-vf",
|
||||
`setpts=PTS/${settings.factor}`,
|
||||
"-an",
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@ const settingsSchema = z.object({
|
||||
.string()
|
||||
.regex(/^#[0-9a-fA-F]{6}$/)
|
||||
.default("#000000"),
|
||||
radius: z.number().int().min(0).max(100).default(70),
|
||||
softness: z.number().int().min(0).max(100).default(50),
|
||||
roundness: z.number().int().min(0).max(100).default(100),
|
||||
centerX: z.number().int().min(0).max(100).default(50),
|
||||
centerY: z.number().int().min(0).max(100).default(50),
|
||||
});
|
||||
|
||||
export function registerVignette(app: FastifyInstance) {
|
||||
@@ -21,11 +26,32 @@ export function registerVignette(app: FastifyInstance) {
|
||||
const w = meta.width ?? 1;
|
||||
const h = meta.height ?? 1;
|
||||
|
||||
const { radius, softness, roundness, centerX, centerY } = settings;
|
||||
|
||||
// Outer radius of the gradient (percentage of the half-diagonal)
|
||||
const outerR = radius / 100;
|
||||
// Inner transparent stop: higher softness pushes it inward (more feather)
|
||||
const innerStop = Math.max(0, Math.min(1, outerR * (1 - softness / 100)));
|
||||
|
||||
// For roundness < 100, stretch the gradient to match image aspect ratio.
|
||||
// At roundness 0 the gradient is fully elliptical (matching the image AR);
|
||||
// at roundness 100 it is a perfect circle.
|
||||
const ar = w / h;
|
||||
const roundFactor = roundness / 100;
|
||||
// scaleX: interpolate from aspect ratio to 1 as roundness goes 0..100
|
||||
const scaleX = ar >= 1 ? 1 : 1 / (roundFactor + (1 - roundFactor) * ar);
|
||||
const scaleY = ar >= 1 ? roundFactor + (1 - roundFactor) / ar : 1;
|
||||
|
||||
const gradientTransform =
|
||||
roundness < 100
|
||||
? ` gradientTransform="translate(${centerX / 100} ${centerY / 100}) scale(${scaleX.toFixed(6)} ${scaleY.toFixed(6)}) translate(-${centerX / 100} -${centerY / 100})"`
|
||||
: "";
|
||||
|
||||
// Build radial-gradient SVG overlay
|
||||
const svg = Buffer.from(
|
||||
`<svg width="${w}" height="${h}">` +
|
||||
`<defs><radialGradient id="v" cx="50%" cy="50%" r="70%">` +
|
||||
`<stop offset="50%" stop-color="${settings.color}" stop-opacity="0"/>` +
|
||||
`<defs><radialGradient id="v" cx="${centerX}%" cy="${centerY}%" r="${outerR * 100}%"${gradientTransform}>` +
|
||||
`<stop offset="${(innerStop * 100).toFixed(1)}%" stop-color="${settings.color}" stop-opacity="0"/>` +
|
||||
`<stop offset="100%" stop-color="${settings.color}" stop-opacity="${settings.strength}"/>` +
|
||||
`</radialGradient></defs>` +
|
||||
`<rect width="100%" height="100%" fill="url(#v)"/>` +
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { extname, join } from "node:path";
|
||||
import { resolveEncoder, resolveFontFile } from "@snapotter/media-engine";
|
||||
import { resolveFontFile } from "@snapotter/media-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { z } from "zod";
|
||||
import { runMediaTool, videoContentType } from "../../lib/media-tool.js";
|
||||
import {
|
||||
runMediaTool,
|
||||
videoContentType,
|
||||
videoEncodeArgsForContainer,
|
||||
} from "../../lib/media-tool.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
@@ -61,14 +65,7 @@ export function registerWatermarkVideo(app: FastifyInstance) {
|
||||
inPath,
|
||||
"-vf",
|
||||
vf,
|
||||
"-c:v",
|
||||
resolveEncoder("h264"),
|
||||
"-crf",
|
||||
"20",
|
||||
"-preset",
|
||||
"medium",
|
||||
"-pix_fmt",
|
||||
"yuv420p",
|
||||
...videoEncodeArgsForContainer(origExt),
|
||||
"-c:a",
|
||||
"copy",
|
||||
out,
|
||||
|
||||
@@ -29,17 +29,43 @@ function findFirstArray(node: unknown): Record<string, unknown>[] | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback for a single (non-repeating) record: depth-first, find the first
|
||||
* object whose values are all scalar (a leaf record), skipping the XML
|
||||
* declaration (keys starting with "?"). Lets a 1-element XML still tabulate.
|
||||
*/
|
||||
function findFirstRecord(node: unknown): Record<string, unknown> | null {
|
||||
if (typeof node !== "object" || node === null || Array.isArray(node)) return null;
|
||||
const entries = Object.entries(node as Record<string, unknown>).filter(
|
||||
([k]) => !k.startsWith("?"),
|
||||
);
|
||||
const objectChildren = entries.filter(([, v]) => typeof v === "object" && v !== null);
|
||||
const hasScalar = entries.some(([, v]) => v === null || typeof v !== "object");
|
||||
if (hasScalar && objectChildren.length === 0) {
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
for (const [, v] of objectChildren) {
|
||||
const found = findFirstRecord(v);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten one level: nested objects and arrays become JSON strings in the cell.
|
||||
*/
|
||||
function flattenRow(row: Record<string, unknown>): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = {};
|
||||
const original = new Set(Object.keys(row));
|
||||
for (const [key, val] of Object.entries(row)) {
|
||||
if (val !== null && typeof val === "object") {
|
||||
out[key] = JSON.stringify(val);
|
||||
} else {
|
||||
out[key] = val;
|
||||
}
|
||||
// Strip fast-xml-parser's internal markers from column names: "@_" on
|
||||
// attributes and "#text" for an element's text content -- unless a sibling
|
||||
// element already owns the cleaned-up name.
|
||||
let bare = key;
|
||||
if (key.startsWith("@_")) bare = key.slice(2);
|
||||
else if (key === "#text") bare = "text";
|
||||
const outKey = bare !== key && original.has(bare) ? key : bare;
|
||||
out[outKey] = val !== null && typeof val === "object" ? JSON.stringify(val) : val;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -71,13 +97,21 @@ export function registerXmlToCsv(app: FastifyInstance) {
|
||||
throw new InputValidationError(`XML parse failed: ${msg.split("\n")[0]}`);
|
||||
}
|
||||
|
||||
const rows = findFirstArray(parsed);
|
||||
let rows = findFirstArray(parsed);
|
||||
if (!rows) {
|
||||
// No repeating array: fall back to a single record (1-row table).
|
||||
const single = findFirstRecord(parsed);
|
||||
if (single) rows = [single];
|
||||
}
|
||||
if (!rows || rows.length === 0) {
|
||||
throw new InputValidationError("No repeating elements found to tabulate");
|
||||
}
|
||||
|
||||
const flattened = rows.map(flattenRow);
|
||||
const csv = Papa.unparse(flattened);
|
||||
// Papa.unparse derives columns from the first row only; pass the union of
|
||||
// all keys so heterogeneous records don't silently drop columns.
|
||||
const columns = Array.from(new Set(flattened.flatMap((row) => Object.keys(row))));
|
||||
const csv = Papa.unparse(flattened, { columns });
|
||||
|
||||
return {
|
||||
buffer: Buffer.from(csv, "utf8"),
|
||||
|
||||
@@ -51,7 +51,10 @@ export function registerYamlJson(app: FastifyInstance) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
throw new InputValidationError(`Not valid YAML: ${msg.split("\n")[0]}`);
|
||||
}
|
||||
const json = JSON.stringify(parsed, null, 2);
|
||||
// js-yaml returns undefined for empty/comment-only documents; normalize to
|
||||
// null so JSON.stringify yields the string "null" instead of undefined
|
||||
// (Buffer.from(undefined) throws).
|
||||
const json = JSON.stringify(parsed ?? null, null, 2);
|
||||
return {
|
||||
buffer: Buffer.from(json, "utf8"),
|
||||
filename: `${base}.json`,
|
||||
|
||||
@@ -430,7 +430,7 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
|
||||
return reply.status(404).send({ error: "File not found" });
|
||||
}
|
||||
|
||||
let stream;
|
||||
let stream: Awaited<ReturnType<typeof streamStoredFile>>;
|
||||
try {
|
||||
stream = await streamStoredFile(file.storedName);
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user