Keep a photo's arrival hash after it leaves the ingest folder.
Filing rewrites the path, and a later caption change rewrites the hash. The index follows the filed file so the original can still be opened.
This commit is contained in:
1 parent
af63ea5673
commit
2a612a6f9a
2 files changed
+123
-5
No files matched your search
@@ -1598,9 +1598,18 @@ pub fn ftp_sent(
|
||||
event: String,
|
||||
date: String,
|
||||
) -> Result<String, String> {
|
||||
// Canonical path while the file is still in ingest: that is the string `ftp_info` stored.
|
||||
let source = std::fs::canonicalize(&path)
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|_| path.clone());
|
||||
let target = state.sent(&path, &event, &date)?;
|
||||
if let Ok(sha) = crate::local::sha256_file(Path::new(&target)) {
|
||||
index.record(&sha, Path::new(&target));
|
||||
let filed = PathBuf::from(&target);
|
||||
index.retarget(&source, &filed);
|
||||
if source != path {
|
||||
index.retarget(&path, &filed);
|
||||
}
|
||||
if let Ok(sha) = crate::local::sha256_file(&filed) {
|
||||
index.record(&sha, &filed);
|
||||
}
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
@@ -95,9 +95,68 @@ impl LocalIndex {
|
||||
impl LocalIndex {
|
||||
/// The file recorded for this hash, even if it changed since it arrived (another app, Photo Mechanic, wrote
|
||||
/// captions into it): the same photo, with other metadata. Never handed out as the fingerprinted original.
|
||||
///
|
||||
/// Filing moves the photo out of the ingest folder and a later rewrite changes its hash, so the arrival
|
||||
/// hash can still point at a path that is gone. The copy filed under the same name is that photo.
|
||||
pub fn find_changed(&self, sha256: &str) -> Option<PathBuf> {
|
||||
let path = PathBuf::from(self.with(|m| m.get(sha256).cloned())?);
|
||||
path.is_file().then_some(path)
|
||||
let recorded = PathBuf::from(self.with(|m| m.get(sha256).cloned())?);
|
||||
if recorded.is_file() {
|
||||
return Some(recorded);
|
||||
}
|
||||
let name = recorded.file_name()?.to_owned();
|
||||
let mut found = self.with(|m| {
|
||||
let mut hits = Vec::new();
|
||||
for (other, path) in m.iter() {
|
||||
if other == sha256 {
|
||||
continue;
|
||||
}
|
||||
let candidate = Path::new(path);
|
||||
if candidate.file_name() != Some(name.as_os_str()) {
|
||||
continue;
|
||||
}
|
||||
if !candidate.is_file() {
|
||||
continue;
|
||||
}
|
||||
hits.push(PathBuf::from(path));
|
||||
}
|
||||
hits
|
||||
});
|
||||
// Two different shoots can reuse a camera name. One remaining file is the one that was filed.
|
||||
if found.len() != 1 {
|
||||
return None;
|
||||
}
|
||||
let path = found.pop()?;
|
||||
self.record(sha256, &path);
|
||||
Some(path)
|
||||
}
|
||||
|
||||
/// The arrival hash keeps pointing at the file after it leaves the ingest folder. The bytes at the
|
||||
/// destination may already differ (captions written in), so recording only the new hash would lose it.
|
||||
pub fn retarget(&self, from: &str, to: &Path) {
|
||||
let to_owned = to.to_string_lossy().into_owned();
|
||||
if from == to_owned {
|
||||
return;
|
||||
}
|
||||
let file = self.file();
|
||||
let snapshot = self.with(|m| {
|
||||
let mut changed = false;
|
||||
for path in m.values_mut() {
|
||||
if path == from {
|
||||
*path = to_owned.clone();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return None;
|
||||
}
|
||||
serde_json::to_vec(m).ok()
|
||||
});
|
||||
if let Some(bytes) = snapshot {
|
||||
if let Some(dir) = file.parent() {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
}
|
||||
let _ = std::fs::write(file, bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +246,9 @@ pub async fn local_stat(
|
||||
sha256: String,
|
||||
) -> Result<LocalStat, String> {
|
||||
let path = index.find_changed(&sha256).ok_or("not_found")?;
|
||||
let meta = tokio::fs::metadata(&path).await.map_err(|e| e.to_string())?;
|
||||
let meta = tokio::fs::metadata(&path)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut size = meta.len();
|
||||
let mut mtime_ms = meta
|
||||
.modified()
|
||||
@@ -350,6 +411,54 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finds_the_filed_copy_when_the_ingest_path_is_gone() {
|
||||
let dir = std::env::temp_dir().join(format!("pr-local-filed-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let ingest = dir.join("ingest");
|
||||
let work = dir.join("work");
|
||||
std::fs::create_dir_all(&ingest).unwrap();
|
||||
std::fs::create_dir_all(&work).unwrap();
|
||||
let arrived = ingest.join("PSR14526.JPG");
|
||||
std::fs::write(&arrived, b"camera jpeg").unwrap();
|
||||
let arrival = sha256_file(&arrived).unwrap();
|
||||
let filed = work.join("PSR14526.JPG");
|
||||
std::fs::write(&filed, b"camera jpeg plus captions").unwrap();
|
||||
let rewritten = sha256_file(&filed).unwrap();
|
||||
|
||||
let index = LocalIndex {
|
||||
file: Some(dir.join("index.json")),
|
||||
..Default::default()
|
||||
};
|
||||
index.record(&arrival, &arrived);
|
||||
index.record(&rewritten, &filed);
|
||||
std::fs::remove_file(&arrived).unwrap();
|
||||
|
||||
assert_eq!(index.find_changed(&arrival), Some(filed.clone()));
|
||||
assert_eq!(index.find(&arrival), None);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retarget_keeps_the_arrival_hash_on_the_filed_path() {
|
||||
let dir = std::env::temp_dir().join(format!("pr-local-retarget-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let arrived = dir.join("from.JPG");
|
||||
let filed = dir.join("to.JPG");
|
||||
std::fs::write(&arrived, b"same").unwrap();
|
||||
std::fs::write(&filed, b"same").unwrap();
|
||||
let sha = sha256_file(&arrived).unwrap();
|
||||
let index = LocalIndex {
|
||||
file: Some(dir.join("index.json")),
|
||||
..Default::default()
|
||||
};
|
||||
index.record(&sha, &arrived);
|
||||
index.retarget(arrived.to_str().unwrap(), &filed);
|
||||
assert_eq!(index.find_changed(&sha), Some(filed));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_filed_jpeg_becomes_a_filmstrip_thumbnail() {
|
||||
let dir = std::env::temp_dir().join(format!("pr-film-{}", std::process::id()));
|
||||
|
||||
Reference in new issue
Block a user