Files
sparkandClaude Opus 5.5 04137476ff
ci / rust (push) Successful in 4m27s
ci / typescript (push) Successful in 7m27s
Photo Mechanic compatibility: all its variables and modifiers, its full IPTC pad, camera details
Templates (IPTC pad, folders, file names, rename on arrival) take every variable of Photo Mechanic's list under its names and short names, in any case, with its modifiers (substrings, lc/UC/PC, tr:find/repl). IPTC variables are the photo's pad, as in Photo Mechanic. The pad gains every Photo Mechanic field with the same IIM/XMP tags, read back from incoming files. Camera details (exposure compensation, flash, metering, program, white balance, 35mm, lens range, firmware, owner, artist, GPS) are kept in photos.exif at ingest and read from the file at delivery for older photos.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-15 02:43:43 +01:00

103 lines
4.2 KiB
TypeScript

import { exiftool } from "exiftool-vendored";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import sharp from "sharp";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { readEmbeddedIptc, repairMacRoman } from "../src/ingest/embedded.js";
import { readCameraInfo } from "../src/ingest/exif.js";
describe("IPTC already in incoming files", () => {
let dir: string;
beforeAll(async () => {
dir = await mkdtemp(path.join(tmpdir(), "pr-emb-"));
});
afterAll(async () => {
await exiftool.end();
await rm(dir, { recursive: true, force: true });
});
async function file(name: string, args: string[]): Promise<string> {
const p = path.join(dir, name);
await writeFile(p, await sharp({ create: { width: 64, height: 48, channels: 3, background: "#456" } }).jpeg().toBuffer());
await exiftool.write(p, {}, { writeArgs: ["-overwrite_original", ...args] });
return p;
}
it("repairs Mac Roman text written without a charset flag (Lightroom/Photo Mechanic on the Mac)", async () => {
expect(repairMacRoman("Pavilh\u008Bo da Escola EB 2/3 Andr\u008E Soares")).toBe("Pavilhão da Escola EB 2/3 André Soares");
const p = await file("macroman.jpg", ["-charset", "iptc=MacRoman", "-IPTC:Sub-location=Pavilhão da Escola EB 2/3 André Soares", "-IPTC:ObjectName=João Gonçalves", "-IPTC:Keywords=ANDEBOL"]);
expect(await readEmbeddedIptc(p)).toEqual({ location: "Pavilhão da Escola EB 2/3 André Soares", title: "João Gonçalves", keywords: ["ANDEBOL"] });
});
it("prefers XMP, which is always UTF-8", async () => {
const p = await file("xmp.jpg", ["-XMP-dc:Title=Nome do Jogador", "-XMP-iptcExt:Event=ABC Braga x CDE Gil Eanes", "-XMP-iptcCore:Location=Pavilhão", "-XMP-iptcExt:PersonInImage=Rui Silva"]);
expect(await readEmbeddedIptc(p)).toEqual({ title: "Nome do Jogador", event: "ABC Braga x CDE Gil Eanes", location: "Pavilhão", personsShown: ["Rui Silva"] });
});
it("reads the rest of Photo Mechanic's pad, as Photo Mechanic writes it", async () => {
const p = await file("pm.jpg", [
"-IPTC:By-lineTitle=Fotojornalista",
"-IPTC:Writer-Editor=PS",
"-IPTC:Province-State=Braga",
"-IPTC:Country-PrimaryLocationName=Portugal",
"-IPTC:SpecialInstructions=Embargo",
"-XMP-iptcCore:[email protected]",
"-XMP-iptcCore:CreatorCity=Braga",
"-XMP-xmpRights:UsageTerms=Uso editorial",
]);
expect(await readEmbeddedIptc(p)).toMatchObject({
jobTitle: "Fotojornalista",
captionWriter: "PS",
state: "Braga",
country: "Portugal",
instructions: "Embargo",
creatorEmail: "[email protected]",
creatorCity: "Braga",
usageTerms: "Uso editorial",
});
});
it("reads the camera details Photo Mechanic shows", async () => {
const p = await file("cam.jpg", [
"-EXIF:Make=Canon",
"-EXIF:Model=Canon EOS R3",
"-EXIF:ExposureCompensation=-0.33",
"-EXIF:Flash#=16",
"-EXIF:MeteringMode#=3",
"-EXIF:ExposureProgram#=1",
"-EXIF:WhiteBalance#=0",
"-EXIF:FocalLengthIn35mmFormat=400",
"-EXIF:LensInfo=100 500 4.5 7.1",
"-EXIF:Software=Firmware Version 1.8.1",
"-EXIF:OwnerName=Peter Spark",
"-EXIF:Artist=Peter Spark",
"-GPSLatitude=41.5618",
"-GPSLatitudeRef=N",
"-GPSLongitude=8.4291",
"-GPSLongitudeRef=W",
]);
const { extra } = await readCameraInfo(p);
expect(extra).toMatchObject({
exposureCompensation: -0.33,
flashFired: false,
meteringMode: "Spot",
exposureProgram: "Manual",
whiteBalance: "Auto",
focalLength35: 400,
lensRange: [100, 500],
firmware: "1.8.1",
owner: "Peter Spark",
artist: "Peter Spark",
});
expect(extra?.latitude).toBeCloseTo(41.5618, 3);
expect(extra?.longitude).toBeCloseTo(-8.4291, 3);
});
it("returns nothing for a bare camera file", async () => {
const p = path.join(dir, "bare.jpg");
await writeFile(p, await sharp({ create: { width: 8, height: 8, channels: 3, background: "#000" } }).jpeg().toBuffer());
expect(await readEmbeddedIptc(p)).toEqual({});
});
});