fix(delivery): write pick/reject/stars into XMP on export (BIT-173) #174

Merged
spark merged 1 commits from task/BIT-173-export-pick-xmp into main 2026-11-03 23:26:14 +00:00
5 changed files with 70 additions and 9 deletions

No files matched your search

+7 -6
View File
@@ -1,4 +1,4 @@
import { type CreatedStamp, toExiftoolArgs, type IptcValues } from "@photoreport/metadata";
import { type CreatedStamp, type LibraryMarks, 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";
@@ -34,7 +34,7 @@ export interface RenderOptions {
* quality, camera EXIF and colour profile kept, IPTC written into both IIM and XMP. A file that is already at
* least {@link CROP_FLOOR_PX} and only a little under `longEdge` is enlarged up to it. Anything smaller stays.
*/
export async function renderDelivery(original: string, iptc: IptcValues, opts: RenderOptions, created?: CreatedStamp, edit?: DeliveryEdit): Promise<Buffer> {
export async function renderDelivery(original: string, iptc: IptcValues, opts: RenderOptions, created?: CreatedStamp, edit?: DeliveryEdit, marks?: LibraryMarks): Promise<Buffer> {
const dir = await mkdtemp(path.join(tmpdir(), "pr-render-"));
const file = path.join(dir, "out.jpg");
const exif = opts.exif !== false;
@@ -44,6 +44,7 @@ export async function renderDelivery(original: string, iptc: IptcValues, opts: R
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"] : [];
const meta = toExiftoolArgs(iptc, created, marks);
try {
if (edit) {
// Edited: the develop engine renders the original (crop and all) straight to the delivered size, in sRGB,
@@ -54,13 +55,13 @@ export async function renderDelivery(original: string, iptc: IptcValues, opts: R
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)] });
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...copyExif, ...dpi, ...meta] });
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)] });
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...dpi, ...meta] });
return await readFile(file);
} finally {
await rm(dir, { recursive: true, force: true });
@@ -71,12 +72,12 @@ export async function renderDelivery(original: string, iptc: IptcValues, opts: R
* 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> {
export async function originalWithMetadata(original: string, iptc: IptcValues, created?: CreatedStamp, marks?: LibraryMarks): 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)] });
await exiftool.write(file, {}, { writeArgs: ["-overwrite_original", ...toExiftoolArgs(iptc, created, marks)] });
return await readFile(file);
} finally {
await rm(dir, { recursive: true, force: true });
+4 -2
View File
@@ -570,11 +570,13 @@ export class DeliveryService {
const fileName = renderFileName(recipe.fileNameTemplate, named, sendOriginals && untouched ? item.photo.originalName : deliveredName(item.photo));
const edit = sendOriginals ? undefined : await this.editFor(item.photoId, item.photo.developSeq);
const created = item.photo.capturedAt ? createdStamp(item.photo.capturedAt) : undefined;
// Cull marks (pick / reject / stars) go into XMP so Lightroom sees rating + green label on import.
const marks = { flag: item.photo.flag, rating: item.photo.rating };
const rendered = sendOriginals
? untouched
? await readFile(source)
: await originalWithMetadata(source, iptc?.values ?? {}, created)
: await renderDelivery(source, iptc?.values ?? {}, { longEdge: recipe.longEdge, quality: recipe.jpegQuality, sharpen: recipe.sharpen, dpi: recipe.dpi, exif: recipe.keepExif, srgb: recipe.srgb }, created, edit);
: await originalWithMetadata(source, iptc?.values ?? {}, created, marks)
: await renderDelivery(source, iptc?.values ?? {}, { longEdge: recipe.longEdge, quality: recipe.jpegQuality, sharpen: recipe.sharpen, dpi: recipe.dpi, exif: recipe.keepExif, srgb: recipe.srgb }, created, edit, marks);
const data = rendered;
if (item.target.kind === "LOCAL") {
// Kept here until the computer's desktop app takes it (localClaim → localFile → localResult).
+5
View File
@@ -427,6 +427,11 @@ Each target also has Photo Mechanic's uploader output options: fit-in-box size,
sharpening, ppi, keep or drop the camera EXIF, and conversion to sRGB. Targets can be duplicated (same server and
credentials, another name and folder).
**Cull marks on delivery.** A pick (⚑), reject (✕) and star rating are written into the delivered JPEG
(`XMP-xmp:Rating`, colour label Green for picks, digiKam `PickLabel`, IPTC Urgency). Lightroom's Flagged
filter is catalog-only and will not show third-party picks; filter by **rating** or **green label** after
import. Photo Mechanic reads Urgency / PickLabel.
**Photo Mechanic compatibility (templates and IPTC).** Every template (IPTC pad, folders, file names, rename on
arrival) takes every variable of Photo Mechanic's list, under its names and short names, in any case
(`packages/metadata/src/pmvars.ts`), and its modifiers: substrings `{var:index,count}` (negative index from the
+31 -1
View File
@@ -246,12 +246,41 @@ export interface CreatedStamp {
iso: string; // YYYY-MM-DDTHH:MM:SS+hh:mm (XMP)
}
/** Library cull (⚑ / ✕ / stars) written into the delivered file so Lightroom and Photo Mechanic see it on import. */
export interface LibraryMarks {
flag?: "NONE" | "PICK" | "REJECT" | string | null;
rating?: number | null;
}
/**
* Stars, pick and reject as XMP/EXIF/IPTC.
*
* Lightroom's **Flagged** filter is catalog-only — Adobe does not read a third-party pick flag from XMP. What does
* import: star rating (`XMP-xmp:Rating`) and colour label (`XMP-xmp:Label`). A PhotoReport pick with no stars is
* written as rating 1 + green label; a reject as rating −1. digiKam `PickLabel` and IPTC Urgency help Photo Mechanic.
*/
export function marksToExiftoolArgs(marks: LibraryMarks): string[] {
const flag = marks.flag ?? "NONE";
const rating = Math.max(0, Math.min(5, Math.round(Number(marks.rating) || 0)));
if (flag !== "PICK" && flag !== "REJECT" && rating === 0) return [];
if (flag === "REJECT") {
return ["-XMP-xmp:Rating=-1", "-Rating=-1", "-XMP-xmp:Label=", "-XMP-digiKam:PickLabel=1", "-IPTC:Urgency=", "-XMP-photoshop:Urgency="];
}
const stars = rating > 0 ? rating : flag === "PICK" ? 1 : 0;
const args: string[] = [];
if (stars > 0) args.push(`-XMP-xmp:Rating=${stars}`, `-Rating=${stars}`);
if (flag === "PICK") {
args.push("-XMP-xmp:Label=Green", "-XMP-digiKam:PickLabel=3", "-IPTC:Urgency=1", "-XMP-photoshop:Urgency=1");
}
return args;
}
/**
* ExifTool arguments (`-TAG=value`) for the given values; list fields repeat the tag once per item.
* IIM is written as UTF-8 and flagged as such (CodedCharacterSet), so accents survive in every reader; files
* from Lightroom today carry no flag and show "Pavilh‹o" in some tools. `-m` keeps long values whole.
*/
export function toExiftoolArgs(values: IptcValues, created?: CreatedStamp): string[] {
export function toExiftoolArgs(values: IptcValues, created?: CreatedStamp, marks?: LibraryMarks): string[] {
const args = ["-m", "-charset", "iptc=UTF8", "-codedcharacterset=utf8"];
if (created) {
args.push(`-IPTC:DateCreated=${created.date}`, `-IPTC:TimeCreated=${created.time}`, `-XMP-photoshop:DateCreated=${created.iso}`);
@@ -268,5 +297,6 @@ export function toExiftoolArgs(values: IptcValues, created?: CreatedStamp): stri
}
}
}
if (marks) args.push(...marksToExiftoolArgs(marks));
return args;
}
+23
View File
@@ -15,6 +15,7 @@ import {
renderFileName,
renderFolder,
seasonOf,
marksToExiftoolArgs,
toExiftoolArgs,
variablesIn,
} from "./index.js";
@@ -119,6 +120,28 @@ describe("IPTC template", () => {
"-XMP-dc:Subject+=ABC",
]);
});
it("writes pick/reject/stars so Lightroom and Photo Mechanic see the cull on import", () => {
expect(marksToExiftoolArgs({ flag: "NONE", rating: 0 })).toEqual([]);
expect(marksToExiftoolArgs({ flag: "PICK", rating: 0 })).toEqual([
"-XMP-xmp:Rating=1",
"-Rating=1",
"-XMP-xmp:Label=Green",
"-XMP-digiKam:PickLabel=3",
"-IPTC:Urgency=1",
"-XMP-photoshop:Urgency=1",
]);
expect(marksToExiftoolArgs({ flag: "PICK", rating: 4 })).toContain("-XMP-xmp:Rating=4");
expect(marksToExiftoolArgs({ flag: "REJECT", rating: 3 })).toEqual([
"-XMP-xmp:Rating=-1",
"-Rating=-1",
"-XMP-xmp:Label=",
"-XMP-digiKam:PickLabel=1",
"-IPTC:Urgency=",
"-XMP-photoshop:Urgency=",
]);
expect(toExiftoolArgs({ title: "A" }, undefined, { flag: "PICK", rating: 0 })).toContain("-XMP-xmp:Label=Green");
});
});
describe("vars", () => {