mirror of
https://codeberg.org/postscriptum/snac2nex.git
synced 2026-04-01 13:45:28 +00:00
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:
parent
2e93fd24e7
commit
d60d5d00d6
1 changed files with 35 additions and 24 deletions
57
src/nex.rs
57
src/nex.rs
|
|
@ -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 {
|
} else {
|
||||||
println!("\t\t\tpost file `{f}` is up to date.")
|
println!("\t\t\tcreate new post file `{f}`.")
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fs::write(&p, c)?;
|
fs::write(&p, c)?;
|
||||||
println!("\t\t\tcreated new post file `{f}`.")
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue