apply Snac time updated to the new files

This commit is contained in:
postscriptum 2025-07-05 14:32:24 +03:00
parent 7baba55272
commit c2d8336156
2 changed files with 66 additions and 50 deletions

View file

@ -68,8 +68,7 @@ impl Nex {
}) })
} }
// Sync the Snac post with the Nex entry // Sync Snac post with Nex entry
// * @TODO apply the post time to the files
pub fn sync( pub fn sync(
&self, &self,
name: &str, name: &str,
@ -130,11 +129,12 @@ impl Nex {
// if this meta file exists and has timestamp changed, also cleanup attachment files to update // if this meta file exists and has timestamp changed, also cleanup attachment files to update
let mut s = PathBuf::from(&d); let mut s = PathBuf::from(&d);
s.push(".state"); s.push(".state");
let state = updated.unwrap_or(published).to_string(); let timestamp = updated.unwrap_or(published);
let state = timestamp.to_string();
if !fs::read_to_string(&s).is_ok_and(|this| this == state) { if !fs::read_to_string(&s).is_ok_and(|this| this == state) {
fs::remove_dir_all(&d)?; fs::remove_dir_all(&d)?;
fs::create_dir_all(&d)?; fs::create_dir_all(&d)?;
fs::write(&s, state)? save(&s, timestamp.into(), state.as_bytes())?
} else { } else {
if let Some(ref mut i) = index { if let Some(ref mut i) = index {
if let Some(ref a) = attachments { if let Some(ref a) = attachments {
@ -159,52 +159,55 @@ impl Nex {
} else { } else {
Change::Created Change::Created
}; };
fs::write( save(
&f, &f,
self.template.build( timestamp.into(),
updated, self.template
content, .build(
link, updated,
tags, content,
attachments.map(|a| { link,
let mut b = Vec::with_capacity(a.len()); tags,
for (n, (name, media_type, source)) in a.into_iter().enumerate() { attachments.map(|a| {
b.push(( let mut b = Vec::with_capacity(a.len());
match source { for (n, (name, media_type, source)) in a.into_iter().enumerate() {
Source::Url(url) => url, b.push((
Source::File(from) => { match source {
let to = attachment::filepath(&d, &from, n); Source::Url(url) => url,
let uri = format!( Source::File(from) => {
"{}/{}", let to = attachment::filepath(&d, &from, n);
d.file_name().unwrap().to_string_lossy(), let uri = format!(
to.file_name().unwrap().to_string_lossy() "{}/{}",
); d.file_name().unwrap().to_string_lossy(),
self.attachment.sync(&from, &to).unwrap(); to.file_name().unwrap().to_string_lossy()
if let Some(ref mut i) = index { );
i.push(to); self.attachment.sync(&from, &to, timestamp.into()).unwrap();
if let Some(ref mut i) = index {
i.push(to);
}
uri
} }
uri },
} {
}, let mut alt = Vec::with_capacity(2);
{ if !name.is_empty() {
let mut alt = Vec::with_capacity(2); alt.push(name)
if !name.is_empty() { }
alt.push(name) if !media_type.is_empty() {
} alt.push(format!("({media_type})"))
if !media_type.is_empty() { }
alt.push(format!("({media_type})")) if alt.is_empty() {
} None
if alt.is_empty() { } else {
None Some(alt.join(" "))
} else { }
Some(alt.join(" ")) },
} ))
}, }
)) b
} }),
b )
}), .as_bytes(),
),
)?; )?;
// move all paths processed to cleanup ignore // move all paths processed to cleanup ignore
@ -263,3 +266,12 @@ impl Nex {
self.is_cleanup self.is_cleanup
} }
} }
/// Wrapper function to change time updated for new file, according to the Snac time.
fn save(path: &PathBuf, modified: std::time::SystemTime, data: &[u8]) -> Result<()> {
use std::io::Write;
let mut f = fs::File::create(path)?;
f.write_all(data)?;
f.set_modified(modified)?; // it's important to call after write
Ok(())
}

View file

@ -1,5 +1,8 @@
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use std::path::{Path, PathBuf}; use std::{
path::{Path, PathBuf},
time::SystemTime,
};
pub enum Attachment { pub enum Attachment {
Copy, Copy,
@ -20,11 +23,12 @@ impl Attachment {
None => Self::Disabled, None => Self::Disabled,
}) })
} }
pub fn sync(&self, source: &PathBuf, target: &PathBuf) -> Result<()> { pub fn sync(&self, source: &PathBuf, target: &PathBuf, modified: SystemTime) -> Result<()> {
use std::{fs, os}; use std::{fs, os};
match self { match self {
Attachment::Copy => { Attachment::Copy => {
fs::copy(source, target)?; fs::copy(source, target)?;
fs::File::open(target)?.set_modified(modified)?;
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(any(target_os = "linux", target_os = "macos"))]
{ {
use std::{fs::Permissions, os::unix::fs::PermissionsExt}; use std::{fs::Permissions, os::unix::fs::PermissionsExt};