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 { 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:CreatorWorkEmail=peter@example.pt", "-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: "peter@example.pt", 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({}); }); });