A destination can take the originals as they arrived (no resizing or recompression; IPTC written by exiftool; renamed by the templates), like Photo Mechanic's Live Ingest destination, and every arrival (FTP, desktop, uploads, once in an event) can be queued to it on its own. On a LOCAL destination: one's own backup in a folder on the computer, e.g. /Users/cyclop/Desktop/2026/{iptcdate} - {event}/PS_{event}_{timesort}.jpg. Adds {iptcdate} (CCYYMMDD).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
83 lines
4.3 KiB
TypeScript
83 lines
4.3 KiB
TypeScript
import { type CreatedStamp, toExiftoolArgs, type IptcValues } from "@photoreport/metadata";
|
|
import { exiftool } from "exiftool-vendored";
|
|
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import sharp, { type Sharp } from "sharp";
|
|
import type { RenderEngine, Settings } from "../develop/engine.js";
|
|
|
|
/** The photo was edited: render it with these develop settings. */
|
|
export interface DeliveryEdit {
|
|
engine: RenderEngine;
|
|
settings: Settings;
|
|
}
|
|
|
|
export interface RenderOptions {
|
|
longEdge: number;
|
|
quality: number;
|
|
/** Output sharpening after resizing (Photo Mechanic's "Sharpen"). */
|
|
sharpen?: boolean;
|
|
/** Pixels per inch written in the file (newspapers still look at it). */
|
|
dpi?: number;
|
|
/** Keep the camera EXIF (Photo Mechanic's "Preserve EXIF information"). */
|
|
exif?: boolean;
|
|
/** Convert to sRGB (unedited photos keep their profile otherwise; edited ones are always sRGB). */
|
|
srgb?: boolean;
|
|
}
|
|
|
|
/**
|
|
* The delivered JPEG: oriented, at most `longEdge` px on the longest side (never enlarged), 4:4:4 chroma at
|
|
* the requested quality, camera EXIF and colour profile kept, IPTC written into both IIM and XMP.
|
|
*/
|
|
export async function renderDelivery(original: string, iptc: IptcValues, opts: RenderOptions, created?: CreatedStamp, edit?: DeliveryEdit): Promise<Buffer> {
|
|
const dir = await mkdtemp(path.join(tmpdir(), "pr-render-"));
|
|
const file = path.join(dir, "out.jpg");
|
|
const exif = opts.exif !== false;
|
|
const finish = (img: Sharp) => {
|
|
if (opts.sharpen) img = img.sharpen({ sigma: 0.6, m1: 0.5, m2: 2 });
|
|
img = opts.srgb || edit ? img.withIccProfile("srgb") : img.keepIccProfile();
|
|
return img.jpeg({ quality: opts.quality, chromaSubsampling: "4:4:4", mozjpeg: false }).toBuffer();
|
|
};
|
|
const dpi = opts.dpi ? [`-XResolution=${opts.dpi}`, `-YResolution=${opts.dpi}`, "-ResolutionUnit=inches", `-JFIF:XResolution=${opts.dpi}`, `-JFIF:YResolution=${opts.dpi}`, "-JFIF:ResolutionUnit=inches"] : [];
|
|
try {
|
|
if (edit) {
|
|
// Edited: the develop engine renders the original (crop and all) straight to the delivered size, in sRGB,
|
|
// losslessly enough (quality 100) for the last pass to sharpen and encode at the delivered quality.
|
|
const rendered = path.join(dir, "edited.jpg");
|
|
await edit.engine.render({ input: original, output: rendered, settings: edit.settings, maxEdge: opts.longEdge, quality: opts.sharpen || opts.quality < 100 ? 100 : opts.quality });
|
|
await writeFile(file, opts.sharpen || opts.quality < 100 ? await finish(sharp(rendered)) : await readFile(rendered));
|
|
// The camera EXIF is copied back, without the orientation (the pixels are already upright).
|
|
const copyExif = exif ? ["-TagsFromFile", original, "-EXIF:all", "--Orientation", "--ThumbnailImage", "--PreviewImage", "-Orientation#=1"] : [];
|
|
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...copyExif, ...dpi, ...toExiftoolArgs(iptc, created)] });
|
|
return await readFile(file);
|
|
}
|
|
let img = sharp(original).rotate().resize({ width: opts.longEdge, height: opts.longEdge, fit: "inside", withoutEnlargement: true });
|
|
if (exif) img = img.keepExif();
|
|
await writeFile(file, await finish(img));
|
|
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...dpi, ...toExiftoolArgs(iptc, created)] });
|
|
return await readFile(file);
|
|
} finally {
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The original as it arrived, untouched (no resizing, no recompression, the camera's EXIF and profile as they
|
|
* are), with the IPTC written into it as Photo Mechanic does on ingest: a copy for one's own backup.
|
|
*/
|
|
export async function originalWithMetadata(original: string, iptc: IptcValues, created?: CreatedStamp): Promise<Buffer> {
|
|
const dir = await mkdtemp(path.join(tmpdir(), "pr-original-"));
|
|
const file = path.join(dir, `copy${path.extname(original).toLowerCase() || ".jpg"}`);
|
|
try {
|
|
await writeFile(file, await readFile(original));
|
|
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...toExiftoolArgs(iptc, created)] });
|
|
return await readFile(file);
|
|
} finally {
|
|
await rm(dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
export async function closeRenderer(): Promise<void> {
|
|
await exiftool.end();
|
|
}
|