mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: comprehensive HEIC/HEIF support and edit-metadata ExifTool overhaul
- Add ensureSharpCompat() helper for automatic HEIC detection and decode - Fix HEIC support in all 14 custom-route tools (image-to-pdf, split, barcode-read, compose, collage, stitch, compare, find-duplicates, color-palette, watermark-image, vectorize, favicon, info, branding) - Fix PdfPagePreview using store's decoded blobUrl instead of raw File - Add onError fallback in ImageViewer for unrenderable formats - Fix image-to-pdf progress bar with flushSync for reliable rendering - Add ExifTool backend for edit-metadata (GPS, keywords, IPTC, dates) - Rename Strip Metadata to Remove Metadata with interactive Leaflet map - Fix user-files thumbnail generation for stored HEIC files - Fix info tool stats() histogram for HEIC via decoded buffer - Skip HEIC preprocessing in batch route for metadata tools
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
import { basename } from "node:path";
|
||||
import { editMetadata, parseExif, parseGps, parseXmp } from "@stirling-image/image-engine";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { basename, join } from "node:path";
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||
import sharp from "sharp";
|
||||
import { z } from "zod";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
import {
|
||||
buildTagArgs,
|
||||
type EditMetadataSettings,
|
||||
inspectMetadata,
|
||||
writeMetadata,
|
||||
} from "../../lib/exiftool.js";
|
||||
import { validateImageBuffer } from "../../lib/file-validation.js";
|
||||
import { decodeHeic } from "../../lib/heic-converter.js";
|
||||
import { createWorkspace } from "../../lib/workspace.js";
|
||||
import { registerToolProcessFn } from "../tool-factory.js";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
artist: z.string().optional(),
|
||||
@@ -14,10 +24,47 @@ const settingsSchema = z.object({
|
||||
dateTimeOriginal: z.string().optional(),
|
||||
clearGps: z.boolean().default(false),
|
||||
fieldsToRemove: z.array(z.string()).default([]),
|
||||
gpsLatitude: z.number().min(-90).max(90).optional(),
|
||||
gpsLongitude: z.number().min(-180).max(180).optional(),
|
||||
gpsAltitude: z.number().optional(),
|
||||
keywords: z.array(z.string()).optional(),
|
||||
keywordsMode: z.enum(["add", "set"]).default("add"),
|
||||
dateShift: z
|
||||
.string()
|
||||
.regex(/^[+-]\d{1,2}:\d{2}$/)
|
||||
.optional(),
|
||||
setAllDates: z.string().optional(),
|
||||
iptcTitle: z.string().optional(),
|
||||
iptcHeadline: z.string().optional(),
|
||||
iptcCity: z.string().optional(),
|
||||
iptcState: z.string().optional(),
|
||||
iptcCountry: z.string().optional(),
|
||||
});
|
||||
|
||||
type Settings = z.infer<typeof settingsSchema>;
|
||||
|
||||
const MIME_BY_FORMAT: Record<string, string> = {
|
||||
jpeg: "image/jpeg",
|
||||
png: "image/png",
|
||||
webp: "image/webp",
|
||||
avif: "image/avif",
|
||||
tiff: "image/tiff",
|
||||
gif: "image/gif",
|
||||
heif: "image/heif",
|
||||
};
|
||||
|
||||
const BROWSER_PREVIEWABLE = new Set([
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/gif",
|
||||
"image/webp",
|
||||
"image/svg+xml",
|
||||
"image/bmp",
|
||||
"image/avif",
|
||||
]);
|
||||
|
||||
export function registerEditMetadata(app: FastifyInstance) {
|
||||
// Inspect endpoint - returns parsed metadata as JSON
|
||||
// Inspect endpoint - returns parsed metadata via ExifTool
|
||||
app.post(
|
||||
"/api/v1/tools/edit-metadata/inspect",
|
||||
async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
@@ -48,45 +95,7 @@ export function registerEditMetadata(app: FastifyInstance) {
|
||||
}
|
||||
|
||||
try {
|
||||
const metadata = await sharp(fileBuffer).metadata();
|
||||
const result: Record<string, unknown> = {
|
||||
filename,
|
||||
fileSize: fileBuffer.length,
|
||||
};
|
||||
|
||||
if (metadata.exif) {
|
||||
try {
|
||||
const parsed = parseExif(metadata.exif);
|
||||
const exifData: Record<string, unknown> = {
|
||||
...parsed.image,
|
||||
...parsed.photo,
|
||||
...parsed.iop,
|
||||
};
|
||||
const gpsData: Record<string, unknown> = { ...parsed.gps };
|
||||
|
||||
if (Object.keys(parsed.gps).length > 0) {
|
||||
const coords = parseGps(parsed.gps);
|
||||
if (coords.latitude !== null) gpsData._latitude = coords.latitude;
|
||||
if (coords.longitude !== null) gpsData._longitude = coords.longitude;
|
||||
if (coords.altitude !== null) gpsData._altitude = coords.altitude;
|
||||
}
|
||||
|
||||
if (Object.keys(exifData).length > 0) result.exif = exifData;
|
||||
if (Object.keys(gpsData).length > 0) result.gps = gpsData;
|
||||
} catch {
|
||||
result.exif = null;
|
||||
result.exifError = "Failed to parse EXIF data";
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata.xmp) {
|
||||
try {
|
||||
result.xmp = parseXmp(metadata.xmp);
|
||||
} catch {
|
||||
result.xmp = null;
|
||||
}
|
||||
}
|
||||
|
||||
const result = await inspectMetadata(fileBuffer, filename);
|
||||
return reply.send(result);
|
||||
} catch (err) {
|
||||
return reply.status(422).send({
|
||||
@@ -97,53 +106,144 @@ export function registerEditMetadata(app: FastifyInstance) {
|
||||
},
|
||||
);
|
||||
|
||||
// Edit endpoint - writes metadata and returns updated image
|
||||
createToolRoute(app, {
|
||||
// Edit endpoint - writes metadata in-place using ExifTool (no pixel re-encoding)
|
||||
app.post("/api/v1/tools/edit-metadata", async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
let fileBuffer: Buffer | null = null;
|
||||
let filename = "image";
|
||||
let settingsRaw: string | null = null;
|
||||
|
||||
try {
|
||||
const parts = request.parts();
|
||||
for await (const part of parts) {
|
||||
if (part.type === "file") {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of part.file) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
fileBuffer = Buffer.concat(chunks);
|
||||
filename = basename(part.filename ?? "image");
|
||||
} else if (part.fieldname === "settings") {
|
||||
settingsRaw = part.value as string;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return reply.status(400).send({
|
||||
error: "Failed to parse multipart request",
|
||||
details: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
|
||||
if (!fileBuffer || fileBuffer.length === 0) {
|
||||
return reply.status(400).send({ error: "No image file provided" });
|
||||
}
|
||||
|
||||
const validation = await validateImageBuffer(fileBuffer);
|
||||
if (!validation.valid) {
|
||||
return reply.status(400).send({ error: `Invalid image: ${validation.reason}` });
|
||||
}
|
||||
|
||||
// Parse and validate settings
|
||||
let settings: Settings;
|
||||
try {
|
||||
const parsed = settingsRaw ? JSON.parse(settingsRaw) : {};
|
||||
const result = settingsSchema.safeParse(parsed);
|
||||
if (!result.success) {
|
||||
return reply.status(400).send({
|
||||
error: "Invalid settings",
|
||||
details: result.error.issues.map((i) => ({
|
||||
path: i.path.join("."),
|
||||
message: i.message,
|
||||
})),
|
||||
});
|
||||
}
|
||||
settings = result.data;
|
||||
} catch {
|
||||
return reply.status(400).send({ error: "Settings must be valid JSON" });
|
||||
}
|
||||
|
||||
try {
|
||||
// Build ExifTool arguments from settings
|
||||
const tags = buildTagArgs(settings as EditMetadataSettings);
|
||||
|
||||
// If no changes requested, return the original buffer
|
||||
let outputBuffer: Buffer;
|
||||
if (tags.length === 0) {
|
||||
outputBuffer = fileBuffer;
|
||||
} else {
|
||||
outputBuffer = await writeMetadata(fileBuffer, filename, tags);
|
||||
}
|
||||
|
||||
// Determine content type from validated format
|
||||
const contentType = MIME_BY_FORMAT[validation.format] ?? "image/jpeg";
|
||||
|
||||
// Create workspace and save output
|
||||
const jobId = randomUUID();
|
||||
const workspacePath = await createWorkspace(jobId);
|
||||
const outputPath = join(workspacePath, "output", filename);
|
||||
await writeFile(outputPath, outputBuffer);
|
||||
|
||||
// Generate preview for non-browser-previewable formats (HEIF, TIFF)
|
||||
let previewUrl: string | undefined;
|
||||
if (!BROWSER_PREVIEWABLE.has(contentType)) {
|
||||
try {
|
||||
let previewInput = outputBuffer;
|
||||
if (contentType === "image/heif" || contentType === "image/heic") {
|
||||
previewInput = await decodeHeic(outputBuffer);
|
||||
}
|
||||
const previewBuffer = await sharp(previewInput).webp({ quality: 80 }).toBuffer();
|
||||
const previewPath = join(workspacePath, "output", "preview.webp");
|
||||
await writeFile(previewPath, previewBuffer);
|
||||
previewUrl = `/api/v1/download/${jobId}/preview.webp`;
|
||||
} catch {
|
||||
// Non-fatal - frontend shows fallback
|
||||
}
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
jobId,
|
||||
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(filename)}`,
|
||||
previewUrl,
|
||||
originalSize: fileBuffer.length,
|
||||
processedSize: outputBuffer.length,
|
||||
});
|
||||
} catch (err) {
|
||||
request.log.error({ err, toolId: "edit-metadata" }, "Metadata edit failed");
|
||||
return reply.status(422).send({
|
||||
error: "Metadata edit failed",
|
||||
details: err instanceof Error ? err.message : "Unknown error",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Register in pipeline/batch registry
|
||||
registerToolProcessFn({
|
||||
toolId: "edit-metadata",
|
||||
settingsSchema,
|
||||
process: async (inputBuffer, settings, filename) => {
|
||||
const metadata = await sharp(inputBuffer).metadata();
|
||||
const format = metadata.format ?? "jpeg";
|
||||
const image = sharp(inputBuffer);
|
||||
const result = await editMetadata(image, settings);
|
||||
const s = settings as Settings;
|
||||
const tags = buildTagArgs(s as EditMetadataSettings);
|
||||
const buffer =
|
||||
tags.length > 0 ? await writeMetadata(inputBuffer, filename, tags) : inputBuffer;
|
||||
|
||||
switch (format) {
|
||||
case "jpeg":
|
||||
result.jpeg({ quality: 95, mozjpeg: true });
|
||||
break;
|
||||
case "png":
|
||||
result.png({ compressionLevel: 6 });
|
||||
break;
|
||||
case "webp":
|
||||
result.webp({ quality: 90 });
|
||||
break;
|
||||
case "avif":
|
||||
result.avif({ quality: 60 });
|
||||
break;
|
||||
case "tiff":
|
||||
result.tiff({ quality: 90 });
|
||||
break;
|
||||
default:
|
||||
result.jpeg({ quality: 95 });
|
||||
break;
|
||||
}
|
||||
|
||||
const buffer = await result.toBuffer();
|
||||
const ext = format === "jpeg" ? "jpg" : format;
|
||||
const outFilename = filename.replace(/\.[^.]+$/, `.${ext}`);
|
||||
const mimeMap: Record<string, string> = {
|
||||
// Determine content type from extension
|
||||
const ext = filename.split(".").pop()?.toLowerCase() ?? "";
|
||||
const extToMime: Record<string, string> = {
|
||||
jpg: "image/jpeg",
|
||||
jpeg: "image/jpeg",
|
||||
png: "image/png",
|
||||
webp: "image/webp",
|
||||
avif: "image/avif",
|
||||
tiff: "image/tiff",
|
||||
tif: "image/tiff",
|
||||
gif: "image/gif",
|
||||
heic: "image/heif",
|
||||
heif: "image/heif",
|
||||
};
|
||||
|
||||
return {
|
||||
buffer,
|
||||
filename: outFilename,
|
||||
contentType: mimeMap[format] ?? "image/jpeg",
|
||||
filename,
|
||||
contentType: extToMime[ext] ?? "image/jpeg",
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user