make performance optimizations on storage update by using .state file based on timestamp, improve disk overwrite logic, rename attachments to short iter format.

This commit is contained in:
postscriptum 2025-07-02 13:47:09 +03:00
parent 2e93fd24e7
commit d60d5d00d6

View file

@ -64,6 +64,27 @@ impl Nex {
p.push(published.format("%d").to_string()); p.push(published.format("%d").to_string());
fs::create_dir_all(&p)?; fs::create_dir_all(&p)?;
// init storage directory for the post state file and attachments
// * the name starts with dot by the current implementation
// it is usually hidden but accessible (in servers like Nexy)
let mut d = PathBuf::from(&p);
d.push(format!(".{}", published.format(&self.filename)));
fs::create_dir_all(&d)?;
// create system meta file to track post time updated
// if this meta file exists and has timestamp changed, also cleanup attachment files to update
let mut s = PathBuf::from(&d);
s.push(".state");
if !s.exists() || updated.is_some_and(|u| u.to_string() != fs::read_to_string(&s).unwrap())
{
fs::remove_dir_all(&d)?;
fs::create_dir_all(&d)?;
fs::write(s, updated.unwrap_or(published).to_string())?
} else {
println!("\t\t\tpost is up to date.");
return Ok(());
}
// format content pattern // format content pattern
let c = self let c = self
.pattern .pattern
@ -81,24 +102,21 @@ impl Nex {
.map(|a| { .map(|a| {
let mut b = Vec::with_capacity(a.len()); let mut b = Vec::with_capacity(a.len());
b.push("\n".to_string()); b.push("\n".to_string());
for (name, media_type, source) in a { for (i, (name, media_type, source)) in a.into_iter().enumerate() {
let mut t = Vec::with_capacity(3); let mut t = Vec::with_capacity(3);
t.push(format!( t.push(format!(
"=> {}", "=> {}",
match source { match source {
Source::Url(url) => url, Source::Url(url) => url,
Source::File(from) => { Source::File(from) => {
// attachments directory starts with dot in this implementation let mut to = PathBuf::from(&d);
// it is usually hidden but accessible in the servers like Nexy let f = format!(
let d = format!(".{}", published.format(&self.filename)); "{}{}",
let mut to = PathBuf::from(&p); i + 1,
to.push(&d); from.extension()
fs::create_dir_all(&to).unwrap(); .map(|e| format!(".{}", e.to_string_lossy()))
let f = from .unwrap_or_default(),
.file_name() );
.unwrap()
.to_string_lossy()
.replace("post-", ""); // cleanup extras from readable txt
to.push(&f); to.push(&f);
if !to.exists() { if !to.exists() {
fs::copy(&from, &to).unwrap(); fs::copy(&from, &to).unwrap();
@ -107,7 +125,7 @@ impl Nex {
to.to_string_lossy() to.to_string_lossy()
) )
} }
format!("{d}/{f}") // relative URI format!("{}/{f}", d.file_name().unwrap().to_string_lossy())
} }
} }
)); ));
@ -137,26 +155,19 @@ impl Nex {
.unwrap_or_default(), .unwrap_or_default(),
); );
// write the data // save post file
p.push( p.push(
published published
.format(self.filename.trim_matches(std::path::MAIN_SEPARATOR)) .format(self.filename.trim_matches(std::path::MAIN_SEPARATOR))
.to_string(), .to_string(),
); );
// safe SSD by prevent extra writes
let f = p.to_string_lossy(); let f = p.to_string_lossy();
if fs::exists(&p)? { if fs::exists(&p)? {
if fs::read_to_string(&p)? != c { println!("\t\t\tpost file `{f}` update with new content.")
fs::write(&p, c)?;
println!("\t\t\tpost file `{f}` updated with new content.")
} else {
println!("\t\t\tpost file `{f}` is up to date.")
}
} else { } else {
fs::write(&p, c)?; println!("\t\t\tcreate new post file `{f}`.")
println!("\t\t\tcreated new post file `{f}`.")
} }
fs::write(&p, c)?;
Ok(()) Ok(())
} }