From 655cd12e1d6dc0b68695e888ab1919e0387ee0ce Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Mon, 10 Apr 2023 13:21:07 +0200 Subject: Add last modified date automation --- src/works/stories.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/works/stories.rs') diff --git a/src/works/stories.rs b/src/works/stories.rs index 9db3ec3..c2b4b8f 100644 --- a/src/works/stories.rs +++ b/src/works/stories.rs @@ -15,6 +15,7 @@ struct Story { path_to_document: String, number_of_pages: u16, language: Language, + #[serde(default)] last_update: NaiveDate, } @@ -40,6 +41,16 @@ impl super::Works for Stories { } anyhow::Ok(()).with_context(|| "Failed to render single story pages") } + + // create stories from stories.json and add last modification date + fn new_from_file(file: std::fs::File) -> anyhow::Result { + let mut stories: Stories = serde_json::from_reader(std::io::BufReader::new(&file))?; + for story in stories.0.iter_mut() { + story.get_last_modified()?; + } + // add error context + Ok(stories) + } } impl Story { @@ -87,6 +98,16 @@ impl Story { let text = std::fs::read_to_string(self.get_content_html_path())?; anyhow::Ok(text).with_context(|| format!("Failed to extract text for {}", self.title)) } + + // get date of last modification of pdf file + fn get_last_modified(&mut self) -> anyhow::Result { + use std::os::linux::fs::MetadataExt; + let pdf_last_modification = std::fs::metadata(self.get_pdf_path())?.st_mtime(); + let html_last_modification = std::fs::metadata(self.get_html_path())?.st_mtime(); + let date = chrono::NaiveDateTime::from_timestamp(std::cmp::max(pdf_last_modification, html_last_modification), 0).date(); + self.last_update = date; + Ok(date) + } } // language of story content -- cgit v1.2.3