Merge pull request 'fix(desktop): flush remembered Lightroom marks into work-folder files (BIT-262)' (#264) from task/BIT-262-flush-work-folder-xmp into main
This commit was merged in pull request #264.
This commit is contained in:
commit
9e464ee267
7 files changed
+118
-54
No files matched your search
@@ -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<string, string>();
|
||||
@@ -161,6 +161,8 @@ function marksOf(flag: string, rating: number, colorLabel: string, sent: boolean
|
||||
export async function catchUpFileMarks(api: ApiClient, orgId: string): Promise<void> {
|
||||
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;
|
||||
}
|
||||
@@ -369,10 +369,26 @@ async function flushPhotoFile(sha256: string): Promise<void> {
|
||||
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 {
|
||||
|
||||
@@ -110,11 +110,14 @@ export class OriginalSender {
|
||||
}
|
||||
|
||||
private async sendOne(need: OriginalNeed): Promise<void> {
|
||||
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 {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@photoreport/desktop",
|
||||
"version": "0.3.33",
|
||||
"version": "0.3.34",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -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."
|
||||
|
||||
@@ -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<String, String> {
|
||||
// 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<String, String> {
|
||||
// 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<String, String> {
|
||||
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#"<?xml version="1.0"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/" xmlns:xmpDM="http://ns.adobe.com/xmp/1.0/DynamicMedia/" crs:Version="15.4" crs:ProcessVersion="11.0" crs:HasSettings="True" xmpDM:pick="1" xmpDM:good="True"/></rdf:RDF></x:xmpmeta>"#;
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user