implement argument option to Keep Nex entry on Snac post was removed (disables cleanup action)

This commit is contained in:
postscriptum 2025-07-03 12:30:51 +03:00
parent 499a78004a
commit d8dac50cb6
5 changed files with 66 additions and 24 deletions

View file

@ -17,6 +17,7 @@ use std::{
pub struct Nex {
attachment: Attachment,
filename: String,
is_cleanup: bool,
pattern: String,
time_format: String,
users: HashMap<String, PathBuf>,
@ -29,6 +30,7 @@ impl Nex {
time_format: String,
pattern: String,
attachment_mode: Option<String>,
is_cleanup: bool,
user_names: &Vec<String>,
) -> Result<Self> {
use std::path::MAIN_SEPARATOR;
@ -52,16 +54,15 @@ impl Nex {
Ok(Self {
attachment: Attachment::init(attachment_mode)?,
filename,
is_cleanup,
pattern,
time_format,
users,
})
}
// @TODO: This function requires the following improvements:
// * validate attachments before updating
// * apply the post time to the files
/// Update destination with given data (typically received from Snac)
// Sync the Snac post with the Nex entry
// * @TODO apply the post time to the files
pub fn sync(
&self,
name: &str,
@ -72,21 +73,33 @@ impl Nex {
(published, updated): (DateTime<Utc>, Option<DateTime<Utc>>),
) -> Result<Sync> {
// collect existing entries to ignore on cleanup
let mut i = Vec::with_capacity(10); // includes est. attachments len
let mut index = if self.is_cleanup {
Some(Vec::with_capacity(10)) // includes est. attachments len
} else {
None
};
// prepare destination
let root = PathBuf::from(self.users.get(name).unwrap());
let mut p = PathBuf::from(&root);
i.push(root);
if let Some(ref mut i) = index {
i.push(root);
}
p.push(published.format("%Y").to_string());
i.push(p.clone());
if let Some(ref mut i) = index {
i.push(p.clone());
}
p.push(published.format("%m").to_string());
i.push(p.clone());
if let Some(ref mut i) = index {
i.push(p.clone());
}
p.push(published.format("%d").to_string());
i.push(p.clone());
if let Some(ref mut i) = index {
i.push(p.clone());
}
fs::create_dir_all(&p)?;
@ -114,10 +127,12 @@ impl Nex {
fs::create_dir_all(&d)?;
fs::write(&s, updated.unwrap_or(published).to_string())?
} else {
i.extend([d, f, p, s]); // move all paths processed to cleanup ignore
if let Some(ref mut i) = index {
i.extend([d, f, p, s]);
}
return Ok(Sync {
change: Change::Ignored,
keep: i,
keep: index,
});
}
@ -157,7 +172,9 @@ impl Nex {
if !to.exists() {
self.attachment.sync(&from, &to).unwrap()
}
i.push(to);
if let Some(ref mut i) = index {
i.push(to);
}
format!("{}/{f}", d.file_name().unwrap().to_string_lossy())
}
}
@ -197,9 +214,14 @@ impl Nex {
fs::write(&f, c)?;
// move all paths processed to cleanup ignore
i.extend([d, f, p, s]);
if let Some(ref mut i) = index {
i.extend([d, f, p, s]);
}
Ok(Sync { change, keep: i })
Ok(Sync {
change,
keep: index,
})
}
pub fn clean(&self, name: &str, ignore: HashSet<PathBuf>) -> Result<Clean> {
@ -233,4 +255,8 @@ impl Nex {
pub fn is_attachments_disabled(&self) -> bool {
matches!(self.attachment, Attachment::Disabled)
}
pub fn is_cleanup(&self) -> bool {
self.is_cleanup
}
}