From 4644ee82f26e5d4fd0b1a1defaa186880607d5ab Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 14 Nov 2026 17:28:10 +0000 Subject: [PATCH 1/2] fix(desktop): flush remembered Lightroom marks and stop blocking writes Picks were kept in localStorage but never reached the work-folder JPEGs when soft-crop bookkeeping ran before ftp_write_xmp. Write the file first, re-flush on catch-up, and move embeds off the UI thread. --- apps/app/src/desktop/fileMarks.ts | 11 ++- apps/app/src/desktop/lightroomSidecar.ts | 24 ++++- apps/desktop/package.json | 2 +- apps/desktop/src-tauri/Cargo.toml | 2 +- apps/desktop/src-tauri/src/ftp.rs | 120 +++++++++++++++-------- apps/desktop/src-tauri/tauri.conf.json | 2 +- 6 files changed, 111 insertions(+), 50 deletions(-) diff --git a/apps/app/src/desktop/fileMarks.ts b/apps/app/src/desktop/fileMarks.ts index 74ec132..3cab243 100644 --- a/apps/app/src/desktop/fileMarks.ts +++ b/apps/app/src/desktop/fileMarks.ts @@ -3,7 +3,7 @@ import type { ApiClient, EventSummary } from "@photoreport/sdk"; import { useEffect } from "react"; import { useLibraryLive } from "../library/realtime.js"; import { isDesktop } from "../platform.js"; -import { settingsFromDevelop, writePhotoCommands, writePhotoFile, type FileIptc, type FileMarkUpdate } from "./lightroomSidecar.js"; +import { flushAllPhotoFiles, settingsFromDevelop, writePhotoCommands, writePhotoFile, type FileIptc, type FileMarkUpdate } from "./lightroomSidecar.js"; const LOCAL = "local:"; const idToSha = new Map(); @@ -161,6 +161,8 @@ function marksOf(flag: string, rating: number, colorLabel: string, sent: boolean export async function catchUpFileMarks(api: ApiClient, orgId: string): Promise { if (!isDesktop() || !orgId || caughtUp.has(orgId)) return; caughtUp.add(orgId); + // Re-apply anything already remembered (survives a desktop update that swallowed earlier writes). + flushAllPhotoFiles(); let events: EventSummary[] = []; try { events = (await api.listEvents(orgId)).events.filter((event) => eventIsCurrent(event, Date.now())); @@ -200,6 +202,13 @@ export function FileMarksSync({ api, orgId }: { api: ApiClient; orgId: string }) useEffect(() => { if (!orgId) return; void catchUpFileMarks(api, orgId); + // Periodic retry: a one-shot catch-up at login misses photos that arrive later, and a failed + // ftp_write_xmp must not leave the work folder empty for the rest of the day. + const timer = window.setInterval(() => { + caughtUp.delete(orgId); + void catchUpFileMarks(api, orgId); + }, 10 * 60 * 1000); + return () => window.clearInterval(timer); }, [api, orgId]); return null; } diff --git a/apps/app/src/desktop/lightroomSidecar.ts b/apps/app/src/desktop/lightroomSidecar.ts index 4404a17..21a83b8 100644 --- a/apps/app/src/desktop/lightroomSidecar.ts +++ b/apps/app/src/desktop/lightroomSidecar.ts @@ -369,10 +369,26 @@ async function flushPhotoFile(sha256: string): Promise { if (!row) return; const xml = lightroomXmp(row.settings, { flag: row.flag, rating: row.rating, colorLabel: row.colorLabel, sent: row.sent }, iptcOf(row.iptc)); if (!xml) return; - const { noteOwnSoftCropWrite } = await import("./softCropWatch.js"); - const writtenFlag = row.sent || row.flag === "PICK" ? "PICK" : row.flag === "REJECT" ? "REJECT" : row.flag === "NONE" ? "NONE" : undefined; - noteOwnSoftCropWrite(sha256, softCropFromSettings(row.settings), writtenFlag); - await desktopInvoke("ftp_write_xmp", { sha256, xml }).catch(() => {}); + // Write the file first. Soft-crop bookkeeping must not block or abort the Lightroom packet. + try { + await desktopInvoke("ftp_write_xmp", { sha256, xml }); + } catch (err) { + console.error("ftp_write_xmp", sha256.slice(0, 12), err); + return; + } + try { + const { noteOwnSoftCropWrite } = await import("./softCropWatch.js"); + const writtenFlag = row.sent || row.flag === "PICK" ? "PICK" : row.flag === "REJECT" ? "REJECT" : row.flag === "NONE" ? "NONE" : undefined; + noteOwnSoftCropWrite(sha256, softCropFromSettings(row.settings), writtenFlag); + } catch { + // Watcher is optional; the file already has the marks. + } +} + +/** Push every remembered mark into the work-folder originals (e.g. after an update that missed earlier flushes). */ +export function flushAllPhotoFiles(): void { + if (!isDesktop()) return; + for (const sha256 of Object.keys(loadRemembered())) scheduleFlush(sha256); } export interface FileMarkUpdate { diff --git a/apps/desktop/package.json b/apps/desktop/package.json index e00dc6a..5cd71d0 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -1,6 +1,6 @@ { "name": "@photoreport/desktop", - "version": "0.3.33", + "version": "0.3.34", "private": true, "type": "module", "scripts": { diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 34aa381..1d8b0c2 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "photoreport-desktop" -version = "0.3.33" +version = "0.3.34" edition = "2024" publish = false description = "PhotoReport desktop: the web app in a native shell, with keychain sign-in and (later) local FTP and the native engine." diff --git a/apps/desktop/src-tauri/src/ftp.rs b/apps/desktop/src-tauri/src/ftp.rs index 043ee2a..348a11d 100644 --- a/apps/desktop/src-tauri/src/ftp.rs +++ b/apps/desktop/src-tauri/src/ftp.rs @@ -775,46 +775,7 @@ impl FtpState { /// JPEG: the edit is embedded in the file (Lightroom does not use a sidecar for JPEG). RAW: a `.xmp` beside it. pub fn write_xmp(&self, photo_path: &str, xml: &str) -> Result { - // Picks, stars, colour and IPTC belong in the file even without develop sliders. - if xml.len() > 60_000 - || !(xml.contains("camera-raw-settings") - || xml.contains("xmpDM:pick") - || xml.contains("xmpDM:Pick") - || xml.contains("xmp:Rating") - || xml.contains("xmp:Label") - || xml.contains("dc:title") - || xml.contains("dc:description") - || xml.contains("photoshop:Headline")) - { - return Err("not_develop_xmp".into()); - } - let photo = PathBuf::from(photo_path); - let canon = photo.canonicalize().map_err(|e| e.to_string())?; - let name = canon.file_name().and_then(|s| s.to_str()).unwrap_or(""); - if !is_photo(&canon) && !is_raw_name(name) { - return Err("not_a_photo".into()); - } - let root = work_root(&self.settings().work_folder); - let root = root.canonicalize().map_err(|e| e.to_string())?; - let Some(dir) = canon.parent() else { - return Err("bad_path".into()); - }; - if !dir.starts_with(&root) || canon == root { - return Err("outside_work_folder".into()); - } - if is_photo(&canon) { - embed_xmp_jpeg(&canon, xml)?; - return Ok(canon.to_string_lossy().into_owned()); - } - let stem = canon.file_stem().ok_or("bad_path")?; - let sidecar = canon.with_file_name(format!("{}.xmp", stem.to_string_lossy())); - let tmp = canon.with_file_name(format!("{}.xmp.photoreport-part", stem.to_string_lossy())); - std::fs::write(&tmp, xml.as_bytes()).map_err(|e| e.to_string())?; - std::fs::rename(&tmp, &sidecar).map_err(|e| { - let _ = std::fs::remove_file(&tmp); - e.to_string() - })?; - Ok(sidecar.to_string_lossy().into_owned()) + write_xmp_at(&self.settings().work_folder, photo_path, xml) } /// The path is inside the ingest folder and the JPEG is finished. Otherwise it goes back on the queue. @@ -830,6 +791,50 @@ impl FtpState { const XMP_HEADER: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; +/// Embed marks / develop / IPTC into a JPEG (or write a RAW `.xmp`) under the work-folder root. +fn write_xmp_at(work_folder_template: &str, photo_path: &str, xml: &str) -> Result { + // Picks, stars, colour and IPTC belong in the file even without develop sliders. + if xml.len() > 60_000 + || !(xml.contains("camera-raw-settings") + || xml.contains("xmpDM:pick") + || xml.contains("xmpDM:Pick") + || xml.contains("xmp:Rating") + || xml.contains("xmp:Label") + || xml.contains("dc:title") + || xml.contains("dc:description") + || xml.contains("photoshop:Headline")) + { + return Err("not_develop_xmp".into()); + } + let photo = PathBuf::from(photo_path); + let canon = photo.canonicalize().map_err(|e| e.to_string())?; + let name = canon.file_name().and_then(|s| s.to_str()).unwrap_or(""); + if !is_photo(&canon) && !is_raw_name(name) { + return Err("not_a_photo".into()); + } + let root = work_root(work_folder_template); + let root = root.canonicalize().map_err(|e| e.to_string())?; + let Some(dir) = canon.parent() else { + return Err("bad_path".into()); + }; + if !dir.starts_with(&root) || canon == root { + return Err("outside_work_folder".into()); + } + if is_photo(&canon) { + embed_xmp_jpeg(&canon, xml)?; + return Ok(canon.to_string_lossy().into_owned()); + } + let stem = canon.file_stem().ok_or("bad_path")?; + let sidecar = canon.with_file_name(format!("{}.xmp", stem.to_string_lossy())); + let tmp = canon.with_file_name(format!("{}.xmp.photoreport-part", stem.to_string_lossy())); + std::fs::write(&tmp, xml.as_bytes()).map_err(|e| e.to_string())?; + std::fs::rename(&tmp, &sidecar).map_err(|e| { + let _ = std::fs::remove_file(&tmp); + e.to_string() + })?; + Ok(sidecar.to_string_lossy().into_owned()) +} + fn mark_our_description(xml: &str) -> String { xml.replacen( "rdf:about=\"\"", @@ -1420,15 +1425,23 @@ pub async fn ftp_info( /// The server has the photo: file the original in the event's work folder and remember where it is, so the editor /// opens it from disk. /// JPEG: embed the develop edit in the file. RAW: write the `.xmp` beside it. +/// Runs off the UI thread: a full-resolution JPEG rewrite must not stall picks in the library. #[tauri::command] -pub fn ftp_write_xmp( +pub async fn ftp_write_xmp( state: tauri::State<'_, FtpState>, index: tauri::State<'_, crate::local::LocalIndex>, sha256: String, xml: String, ) -> Result { let path = index.find_changed(&sha256).ok_or("not_found")?; - state.write_xmp(&path.to_string_lossy(), &xml) + let path_str = path.to_string_lossy().into_owned(); + let work_folder = state.settings().work_folder.clone(); + tauri::async_runtime::spawn_blocking(move || { + // Same checks as FtpState::write_xmp, without holding the state lock across the disk rewrite. + write_xmp_at(&work_folder, &path_str, &xml) + }) + .await + .map_err(|e| e.to_string())? } #[tauri::command] @@ -1897,4 +1910,27 @@ mod tests { assert!(both.contains("xmpDM:pick=\"1\"")); assert!(both.contains("dc:title=\"Jogador\"")); } + + #[test] + fn write_xmp_at_embeds_pick_into_jpeg_copy() { + let dir = std::env::temp_dir().join(format!("pr-xmp-embed-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let work = dir.join("work"); + std::fs::create_dir_all(&work).unwrap(); + // Minimal JPEG (SOI + APP0 + EOI) so find_xmp_app1 and embed succeed. + let jpeg = work.join("shot.JPG"); + let mut bytes = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10]; + bytes.extend_from_slice(b"JFIF\0\x01\x01\0\0\x01\0\x01\0\0"); + bytes.extend_from_slice(&[0xFF, 0xD9]); + std::fs::write(&jpeg, &bytes).unwrap(); + let xml = r#""#; + // work_root is the prefix before `{`; point it at `work`. + let template = format!("{}/{{date}} - {{event}}", work.to_string_lossy()); + write_xmp_at(&template, jpeg.to_str().unwrap(), xml).expect("embed"); + let got = std::fs::read(&jpeg).unwrap(); + let text = String::from_utf8_lossy(&got); + assert!(text.contains("xmpDM:pick=\"1\"")); + assert!(text.contains("photoreport:embed=\"1\"")); + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json index 8cd649c..7bd0ec5 100644 --- a/apps/desktop/src-tauri/tauri.conf.json +++ b/apps/desktop/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "PhotoReport", - "version": "0.3.33", + "version": "0.3.34", "identifier": "pt.photoreport.desktop", "build": { "devUrl": "http://localhost:5173", -- 2.47.3 From 9f5532c737acde31ec5ffde6d84220dbc63e5691 Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 14 Nov 2026 17:57:43 +0000 Subject: [PATCH 2/2] fix(desktop): send delivery originals without re-hashing XMP-rewritten files After work-folder XMP writes the JPEG hash no longer matches the index. Trying local_original first re-hashed every multi-MB file and stalled the queue that was waiting on those uploads. --- apps/app/src/library/originals.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/app/src/library/originals.ts b/apps/app/src/library/originals.ts index 7e32d82..a9e2c5c 100644 --- a/apps/app/src/library/originals.ts +++ b/apps/app/src/library/originals.ts @@ -110,11 +110,14 @@ export class OriginalSender { } private async sendOne(need: OriginalNeed): Promise { - const exact = await withTimeout(localOriginal(need.sha256), ORIGINAL_IPC_MS); - let bytes: ArrayBuffer | null = exact && exact !== "timeout" ? exact : null; + // Prefer the path that skips a full-file SHA (XMP / IPTC rewrites change the hash but keep the same + // pixels). Trying `local_original` first re-hashes multi‑MB JPEGs only to fail, and stalls the + // delivery queue that is waiting on these uploads. + const changed = await withTimeout(localOriginalChanged(need.sha256), ORIGINAL_IPC_MS); + let bytes: ArrayBuffer | null = changed && changed !== "timeout" ? changed : null; if (!bytes) { - const changed = await withTimeout(localOriginalChanged(need.sha256), ORIGINAL_IPC_MS); - bytes = changed && changed !== "timeout" ? changed : null; + const exact = await withTimeout(localOriginal(need.sha256), ORIGINAL_IPC_MS); + bytes = exact && exact !== "timeout" ? exact : null; } if (!bytes) return; try { -- 2.47.3