diff --git a/src/nex.rs b/src/nex.rs index 0bbc7f3..7e08827 100644 --- a/src/nex.rs +++ b/src/nex.rs @@ -64,6 +64,27 @@ impl Nex { p.push(published.format("%d").to_string()); 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 let c = self .pattern @@ -81,24 +102,21 @@ impl Nex { .map(|a| { let mut b = Vec::with_capacity(a.len()); 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); t.push(format!( "=> {}", match source { Source::Url(url) => url, Source::File(from) => { - // attachments directory starts with dot in this implementation - // it is usually hidden but accessible in the servers like Nexy - let d = format!(".{}", published.format(&self.filename)); - let mut to = PathBuf::from(&p); - to.push(&d); - fs::create_dir_all(&to).unwrap(); - let f = from - .file_name() - .unwrap() - .to_string_lossy() - .replace("post-", ""); // cleanup extras from readable txt + let mut to = PathBuf::from(&d); + let f = format!( + "{}{}", + i + 1, + from.extension() + .map(|e| format!(".{}", e.to_string_lossy())) + .unwrap_or_default(), + ); to.push(&f); if !to.exists() { fs::copy(&from, &to).unwrap(); @@ -107,7 +125,7 @@ impl Nex { 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(), ); - // write the data + // save post file p.push( published .format(self.filename.trim_matches(std::path::MAIN_SEPARATOR)) .to_string(), ); - - // safe SSD by prevent extra writes let f = p.to_string_lossy(); if fs::exists(&p)? { - if fs::read_to_string(&p)? != c { - 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.") - } + println!("\t\t\tpost file `{f}` update with new content.") } else { - fs::write(&p, c)?; - println!("\t\t\tcreated new post file `{f}`.") + println!("\t\t\tcreate new post file `{f}`.") } + fs::write(&p, c)?; Ok(()) }