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

View file

@ -1,5 +1,5 @@
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use std::path::PathBuf; use std::path::{Path, PathBuf};
pub enum Attachment { pub enum Attachment {
Copy, Copy,
@ -62,3 +62,16 @@ impl Attachment {
Ok(()) 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
}