mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Merge branch 'testing/image-editor' into main
Merges all image editor work: RAW decode improvements, expanded format export (AVIF/TIFF/GIF/JXL/PSD), Photoshop-style menu bar, custom Konva filters, smart guides, clone stamp, dodge/burn, eyedropper, pixel brush, selection and transform tool overlays, autosave blob URL persistence, and 49+ bug fixes across editor canvas and E2E tests. # Conflicts: # apps/api/src/routes/tool-factory.ts # apps/api/src/routes/tools/convert.ts # apps/web/src/components/editor/common/export-dialog.tsx # apps/web/src/components/editor/editor-canvas.tsx
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { extname } from "node:path";
|
||||
import { execFile } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { extname, join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { convert } from "@snapotter/image-engine";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import sharp from "sharp";
|
||||
@@ -8,6 +13,28 @@ import { encodeHeic } from "../../lib/heic-converter.js";
|
||||
import { isSvgBuffer } from "../../lib/svg-sanitize.js";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
let cachedMagickCmd: string | null = null;
|
||||
|
||||
async function findMagickCmd(): Promise<string> {
|
||||
if (cachedMagickCmd) return cachedMagickCmd;
|
||||
for (const cmd of ["magick", "convert"]) {
|
||||
try {
|
||||
await execFileAsync(cmd, ["--version"], { timeout: 5_000 });
|
||||
cachedMagickCmd = cmd;
|
||||
return cmd;
|
||||
} catch {
|
||||
// try next
|
||||
}
|
||||
}
|
||||
throw new Error("No ImageMagick found. Install imagemagick (provides convert/magick).");
|
||||
}
|
||||
|
||||
function magickArgs(cmd: string, args: string[]): string[] {
|
||||
return cmd === "magick" ? ["convert", ...args] : args;
|
||||
}
|
||||
|
||||
const FORMAT_CONTENT_TYPES: Record<string, string> = {
|
||||
jpg: "image/jpeg",
|
||||
png: "image/png",
|
||||
@@ -22,6 +49,7 @@ const FORMAT_CONTENT_TYPES: Record<string, string> = {
|
||||
ico: "image/x-icon",
|
||||
jp2: "image/jp2",
|
||||
qoi: "image/x-qoi",
|
||||
psd: "image/vnd.adobe.photoshop",
|
||||
};
|
||||
|
||||
const CLI_ENCODERS: Record<string, (buf: Buffer, quality?: number) => Promise<Buffer>> = {
|
||||
@@ -46,6 +74,7 @@ const settingsSchema = z.object({
|
||||
"ico",
|
||||
"jp2",
|
||||
"qoi",
|
||||
"psd",
|
||||
]),
|
||||
quality: z.number().min(1).max(100).optional(),
|
||||
});
|
||||
@@ -73,12 +102,27 @@ export function registerConvert(app: FastifyInstance) {
|
||||
const image = sharp(inputBuffer, sharpOpts);
|
||||
|
||||
let buffer: Buffer;
|
||||
if (settings.format === "heic" || settings.format === "heif") {
|
||||
// Sharp cannot encode HEVC. Convert to PNG first, then use heif-enc.
|
||||
if (settings.format === "psd") {
|
||||
const pngBuffer = await image.png().toBuffer();
|
||||
const id = randomUUID();
|
||||
const inputPath = join(tmpdir(), `psd-enc-in-${id}.png`);
|
||||
const outputPath = join(tmpdir(), `psd-enc-out-${id}.psd`);
|
||||
try {
|
||||
await writeFile(inputPath, pngBuffer);
|
||||
const cmd = await findMagickCmd();
|
||||
await execFileAsync(cmd, magickArgs(cmd, [inputPath, `psd:${outputPath}`]), {
|
||||
timeout: 120_000,
|
||||
});
|
||||
buffer = await readFile(outputPath);
|
||||
} finally {
|
||||
await rm(inputPath, { force: true }).catch(() => {});
|
||||
await rm(outputPath, { force: true }).catch(() => {});
|
||||
}
|
||||
} else if (settings.format === "heic" || settings.format === "heif") {
|
||||
const pngBuffer = await image.png().toBuffer();
|
||||
buffer = await encodeHeic(pngBuffer, settings.quality);
|
||||
} else {
|
||||
const result = await convert(image, settings);
|
||||
const result = await convert(image, settings as Parameters<typeof convert>[1]);
|
||||
buffer = await result.toBuffer();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user