fix missed attachment index extend on update, implement shared filepath function

This commit is contained in:
postscriptum 2025-07-03 19:05:54 +03:00
parent 4e41f3fdf8
commit b8c5e2de86
2 changed files with 28 additions and 10 deletions

View file

@ -130,6 +130,14 @@ impl Nex {
fs::write(&s, updated.unwrap_or(published).to_string())?
} else {
if let Some(ref mut i) = index {
if let Some(ref a) = attachments {
for (n, (_, _, source)) in a.iter().enumerate() {
match source {
Source::File(f) => i.push(attachment::filepath(&d, f, n)),
_ => continue,
}
}
}
i.extend([d, f, p, s]);
}
return Ok(Sync {
@ -162,22 +170,19 @@ impl Nex {
match source {
Source::Url(url) => url,
Source::File(from) => {
let mut to = PathBuf::from(&d);
let f = format!(
"{}{}",
n + 1,
from.extension()
.map(|e| format!(".{}", e.to_string_lossy()))
.unwrap_or_default(),
let to = attachment::filepath(&d, &from, n);
let uri = format!(
"{}/{}",
d.file_name().unwrap().to_string_lossy(),
to.file_name().unwrap().to_string_lossy()
);
to.push(&f);
if !to.exists() {
self.attachment.sync(&from, &to).unwrap()
}
if let Some(ref mut i) = index {
i.push(to);
}
format!("{}/{f}", d.file_name().unwrap().to_string_lossy())
uri
}
}
));

View file

@ -1,5 +1,5 @@
use anyhow::{Result, bail};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub enum Attachment {
Copy,
@ -62,3 +62,16 @@ impl Attachment {
Ok(())
}
}
pub fn filepath(d: &PathBuf, from: &Path, n: usize) -> PathBuf {
let mut to = PathBuf::from(&d);
let f = format!(
"{}{}",
n + 1,
from.extension()
.map(|e| format!(".{}", e.to_string_lossy()))
.unwrap_or_default(),
);
to.push(&f);
to
}