From e5d7ca324f767f2d8d61f54f8174adf7d43f5bae Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 6 Nov 2026 21:18:42 +0000 Subject: [PATCH] fix(library): take the colour Photo Mechanic paints The desktop re-read the file for a crop and a reject, and left the colour behind. Colour class 1, 3, 4, 5 and 7 become red, yellow, green, blue and purple on the photo. --- apps/app/src/desktop/SoftCropWatcher.tsx | 9 +++-- apps/app/src/desktop/lrMarksWatch.ts | 16 +++++++- apps/app/src/desktop/softCrop.ts | 24 ++++++++---- apps/app/src/desktop/softCropWatch.ts | 35 +++++++++++++---- docs/ARCHITECTURE.md | 3 +- packages/metadata/src/iptc.ts | 48 +++++++++++++++++++++--- packages/metadata/src/metadata.test.ts | 8 ++++ 7 files changed, 116 insertions(+), 27 deletions(-) diff --git a/apps/app/src/desktop/SoftCropWatcher.tsx b/apps/app/src/desktop/SoftCropWatcher.tsx index 92e03b6..ab2aaa0 100644 --- a/apps/app/src/desktop/SoftCropWatcher.tsx +++ b/apps/app/src/desktop/SoftCropWatcher.tsx @@ -8,9 +8,10 @@ const EVERY_MS = 4_000; /** * Desktop only: while signed in, notice Lightroom / Photo Mechanic soft-crop writes on local originals * and apply them as develop crops (same path as cropping in Revelar — including autopilot learning). + * A colour class Photo Mechanic paints on the same file is copied onto the library photo. */ export function SoftCropWatch({ enabled = isDesktop(), everyMs = EVERY_MS }: { enabled?: boolean; everyMs?: number }) { - const { organization } = useAuth(); + const { api, organization } = useAuth(); const orgId = organization?.id; useEffect(() => { if (!enabled || !orgId) return; @@ -18,7 +19,9 @@ export function SoftCropWatch({ enabled = isDesktop(), everyMs = EVERY_MS }: { e let timer: ReturnType | undefined; const tick = async () => { try { - await reconcileSoftCrops(); + await reconcileSoftCrops(async (id, photoId, color) => { + await api.updatePhotos(id, [photoId], { colorLabel: color }); + }); } catch { /* older desktop without local_stat, or offline index miss */ } @@ -29,6 +32,6 @@ export function SoftCropWatch({ enabled = isDesktop(), everyMs = EVERY_MS }: { e alive = false; clearTimeout(timer); }; - }, [orgId, enabled, everyMs]); + }, [api, orgId, enabled, everyMs]); return null; } diff --git a/apps/app/src/desktop/lrMarksWatch.ts b/apps/app/src/desktop/lrMarksWatch.ts index 7537368..ce5a8d5 100644 --- a/apps/app/src/desktop/lrMarksWatch.ts +++ b/apps/app/src/desktop/lrMarksWatch.ts @@ -1,4 +1,4 @@ -import { libraryMarksFromXmp, xmpPacketFromJpeg } from "@photoreport/metadata"; +import { colorClassFromXmp, libraryMarksFromXmp, xmpPacketFromJpeg } from "@photoreport/metadata"; import type { ApiClient } from "@photoreport/sdk"; import { desktopInvoke, isDesktop } from "../platform.js"; @@ -15,6 +15,8 @@ export interface WatchedDelivery { orgId: string; /** Last mtime we already reconciled (ms). */ seenAt: number; + /** Colour last copied from this file. */ + color?: string | undefined; } function load(): WatchedDelivery[] { @@ -96,6 +98,16 @@ export async function reconcileLightroomMarks(api: ApiClient): Promise { continue; } const marks = libraryMarksFromXmp(xmp); + const color = colorClassFromXmp(xmp); + if (color && color !== row.color) { + try { + await api.updatePhotos(row.orgId, [row.photoId], { colorLabel: color }); + row.color = color; + } catch { + next.push(row); + continue; + } + } if (marks.flag === "REJECT") { try { await deleteDeliveredFile(row.path); @@ -107,7 +119,7 @@ export async function reconcileLightroomMarks(api: ApiClient): Promise { continue; } } - next.push({ ...row, seenAt: Date.now() }); + next.push({ ...row, seenAt: Date.now(), color: row.color }); } save(next); return rejected; diff --git a/apps/app/src/desktop/softCrop.ts b/apps/app/src/desktop/softCrop.ts index 76cda85..9e8baa1 100644 --- a/apps/app/src/desktop/softCrop.ts +++ b/apps/app/src/desktop/softCrop.ts @@ -12,6 +12,21 @@ function asBytes(data: unknown): Uint8Array | null { return null; } +/** The XMP text of a local original (sidecar, else the packet in the JPEG). Null when this computer has no file. */ +export async function localXmpText(sha256: string): Promise { + if (!isDesktop() || !sha256) return null; + try { + const raw = await desktopInvoke("local_xmp", { sha256 }); + const bytes = asBytes(raw); + if (!bytes?.length) return null; + const text = new TextDecoder("utf-8", { fatal: false }).decode(bytes); + if (text.includes(" { if (pending) return pending; const work = (async () => { try { - const raw = await desktopInvoke("local_xmp", { sha256 }); - const bytes = asBytes(raw); - if (!bytes?.length) { - cache.set(sha256, null); - return null; - } - const text = new TextDecoder("utf-8", { fatal: false }).decode(bytes); - const xmp = text.includes(" Promise; + /** * One pass: re-read soft crops whose file (or sidecar) mtime moved, apply as develop crop when the - * rectangle changed. Returns how many photos received a new crop. + * rectangle changed, and copy a colour class Photo Mechanic wrote onto the library photo. + * Returns how many photos received a new crop. */ -export async function reconcileSoftCrops(): Promise { +export async function reconcileSoftCrops(applyColor?: ApplyExternalColor): Promise { if (!isDesktop()) return 0; const rows = load(); if (!rows.length) return 0; @@ -114,7 +120,9 @@ export async function reconcileSoftCrops(): Promise { next.push(row); continue; } - if (stat.mtimeMs && stat.mtimeMs === row.mtimeMs) { + // A row saved before colours were copied has no `color`. Read it once even if the mtime is already known, + // so a colour painted earlier still arrives. "" means we looked and the file names no colour. + if (stat.mtimeMs && stat.mtimeMs === row.mtimeMs && row.color !== undefined) { next.push(row); continue; } @@ -123,10 +131,22 @@ export async function reconcileSoftCrops(): Promise { continue; } forgetSoftCrop(row.sha256); - const crop = await softCropFor(row.sha256); + const xmp = await localXmpText(row.sha256); + const crop = xmp ? cropFromXmp(xmp) : null; + const color = xmp ? colorClassFromXmp(xmp) : null; + let colorApplied = color ? row.color : (row.color ?? ""); + if (color && color !== row.color && applyColor) { + try { + await applyColor(row.orgId, row.photoId, color); + colorApplied = color; + } catch { + next.push(row); + continue; + } + } const fingerprint = softCropFingerprint(crop); if (!crop || fingerprint === row.fingerprint) { - next.push({ ...row, mtimeMs: stat.mtimeMs || row.mtimeMs, fingerprint }); + next.push({ ...row, mtimeMs: stat.mtimeMs || row.mtimeMs, fingerprint, color: colorApplied }); continue; } const command = { @@ -147,6 +167,7 @@ export async function reconcileSoftCrops(): Promise { ...row, mtimeMs: stat.mtimeMs || row.mtimeMs, fingerprint, + color: colorApplied, ownWriteUntil: now + OWN_WRITE_GRACE_MS, }); } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 3136f10..e7c5ed4 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -464,7 +464,8 @@ credentials, another name and folder). for Lightroom Classic ≥ 13.2 Attribute filters: `XMP-xmpDM:Pick` (1 / 0 / −1) + `Good`, `XMP-xmp:Rating`, `XMP-xmp:Label`, plus digiKam `PickLabel` and IPTC Urgency for Photo Mechanic. When the desktop app is open it watches local delivery folders; a Reject written back by Lightroom (XMP auto-write) sets the photo to REJECT -and retracts any delivery of that file. +and retracts any delivery of that file. A colour Photo Mechanic paints on a local original, or on a delivered +file, is copied back (`photomechanic:ColorClass`: 1 red, 3 yellow, 4 green, 5 blue, 7 purple). **Colour ↔ destination.** A delivery target may own one library colour. Painting that colour queues the photo only there; enqueue skips `color_mismatch` otherwise. The library colour is how you see what went where. diff --git a/packages/metadata/src/iptc.ts b/packages/metadata/src/iptc.ts index 714a568..521ffe6 100644 --- a/packages/metadata/src/iptc.ts +++ b/packages/metadata/src/iptc.ts @@ -323,21 +323,57 @@ export function marksToExiftoolArgs(marks: LibraryMarks): string[] { /** * Read cull marks from an XMP packet (sidecar or embedded). Used when Lightroom writes Pick back to a delivered file. */ +/** + * Photo Mechanic's colour class (the number it writes, not the English name). Same map as the IPTC variables: + * 1 red, 3 yellow, 4 green, 5 blue, 7 purple. 0 clears the colour. 2 and 6 are unused in that set. + */ +export type XmpColor = "NONE" | "RED" | "YELLOW" | "GREEN" | "BLUE" | "PURPLE"; + +const PM_COLOR_CLASS: Record = { + 0: "NONE", + 1: "RED", + 3: "YELLOW", + 4: "GREEN", + 5: "BLUE", + 7: "PURPLE", +}; + +const LABEL_NAME: Record = { + red: "RED", + winner: "RED", + yellow: "YELLOW", + superior: "YELLOW", + green: "GREEN", + "superior alt": "GREEN", + blue: "BLUE", + typical: "BLUE", + purple: "PURPLE", + extra: "PURPLE", + none: "NONE", +}; + +/** + * The colour Photo Mechanic or Lightroom wrote. Null when the packet does not mention a colour, so a file + * that only has a crop does not wipe a colour set in the library. Colour class wins over the label name. + */ +export function colorClassFromXmp(xmp: string): XmpColor | null { + const klass = xmpMatchNum(xmp, /(?:photomechanic:)?ColorClass\s*(?:=|:|>)\s*"?(\d+)/i) ?? xmpMatchNum(xmp, /<(?:photomechanic:)?ColorClass>(\d+)<\/(?:photomechanic:)?ColorClass>/i); + if (klass !== null) return PM_COLOR_CLASS[klass] ?? null; + const labelRaw = xmpMatchStr(xmp, /(?:xmp:Label|Label)\s*(?:=|:|>)\s*"?([A-Za-z]+(?: [A-Za-z]+)?)/i) ?? xmpMatchStr(xmp, /([^<]+)<\/xmp:Label>/i); + if (labelRaw === null) return null; + return LABEL_NAME[labelRaw.trim().toLowerCase()] ?? null; +} + export function libraryMarksFromXmp(xmp: string): LibraryMarks { const pick = xmpMatchNum(xmp, /(?:xmpDM:pick|xmp:Pick|Pick)\s*(?:=|:|>)\s*"?(-?\d+)/i) ?? xmpMatchNum(xmp, /(-?\d+)<\/xmpDM:pick>/i); const rating = xmpMatchNum(xmp, /(?:xmp:Rating|Rating)\s*(?:=|:|>)\s*"?(-?\d+)/i) ?? xmpMatchNum(xmp, /(-?\d+)<\/xmp:Rating>/i); - const labelRaw = xmpMatchStr(xmp, /(?:xmp:Label|Label)\s*(?:=|:|>)\s*"?([A-Za-z]+)/i) ?? xmpMatchStr(xmp, /([^<]+)<\/xmp:Label>/i); let flag: LibraryMarks["flag"] = "NONE"; if (pick === -1 || rating === -1) flag = "REJECT"; else if (pick === 1) flag = "PICK"; - const colorLabel = - labelRaw && ["Red", "Yellow", "Green", "Blue", "Purple"].includes(labelRaw) - ? (labelRaw.toUpperCase() as "RED" | "YELLOW" | "GREEN" | "BLUE" | "PURPLE") - : "NONE"; return { flag, rating: rating !== null && rating > 0 ? Math.min(5, rating) : 0, - colorLabel, + colorLabel: colorClassFromXmp(xmp) ?? "NONE", }; } diff --git a/packages/metadata/src/metadata.test.ts b/packages/metadata/src/metadata.test.ts index 7d034ab..756a567 100644 --- a/packages/metadata/src/metadata.test.ts +++ b/packages/metadata/src/metadata.test.ts @@ -15,6 +15,7 @@ import { renderFileName, renderFolder, seasonOf, + colorClassFromXmp, cropFromXmp, libraryMarksFromXmp, marksToExiftoolArgs, @@ -170,6 +171,13 @@ describe("IPTC template", () => { colorLabel: "BLUE", }); expect(libraryMarksFromXmp(`xmpDM:pick="-1"`).flag).toBe("REJECT"); + expect(colorClassFromXmp(`photomechanic:ColorClass="1"`)).toBe("RED"); + expect(colorClassFromXmp(`photomechanic:ColorClass="3"`)).toBe("YELLOW"); + expect(colorClassFromXmp(`photomechanic:ColorClass="4"`)).toBe("GREEN"); + expect(colorClassFromXmp(`photomechanic:ColorClass="7"`)).toBe("PURPLE"); + expect(colorClassFromXmp(`photomechanic:ColorClass="0"`)).toBe("NONE"); + expect(colorClassFromXmp(`photomechanic:ColorClass="1" xmp:Label="Blue"`)).toBe("RED"); + expect(colorClassFromXmp(`crs:CropTop="0.1"`)).toBeNull(); }); it("reads the soft crop Photo Mechanic and Lightroom draw from crs attributes", () => { -- 2.47.3