]> git.r.bdr.sh - rbdr/blog/commitdiff
Allow adding / updating
authorRuben Beltran del Rio <redacted>
Mon, 26 Feb 2024 08:58:22 +0000 (08:58 +0000)
committerRuben Beltran del Rio <redacted>
Mon, 26 Feb 2024 08:58:22 +0000 (08:58 +0000)
src/command/add.rs
src/command/update.rs
src/metadata.rs

index 3e0e9876cbb5268aacace5762a202ea74de38a98..dbcc512c5f343199345785eaaf1255f503c0f37b 100644 (file)
@@ -1,5 +1,5 @@
-use std::fs::{create_dir_all, remove_dir_all, rename};
-use std::io::{Result, Error};
+use std::fs::{create_dir_all, rename};
+use std::io::Result;
 use super::{
     generate::Generate,
     sync_down::SyncDown,
@@ -14,24 +14,6 @@ impl Add {
     pub fn new() -> Self {
         Add
     }
-
-    // moves posts to their next
-    fn shift(&self, configuration: &Configuration) -> Result<()> {
-        for i in (0..configuration.max_posts).rev() {
-            let source = configuration.posts_directory.join(i.to_string());
-            let target = configuration.posts_directory.join((i + 1).to_string());
-
-            println!("Moving {} source to {}", source.display(), target.display());
-
-            if source.exists() {
-                match rename(&source, &target) {
-                    Ok(_) => continue,
-                    Err(e) => return Err(Error::new(e.kind(), format!("Could not shift post {} to {}", source.display(), target.display())))
-                }
-            }
-        }
-        Ok(())
-    }
 }
 
 impl super::Command for Add {
@@ -40,22 +22,16 @@ impl super::Command for Add {
     }
 
     fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
-        match create_dir_all(&configuration.posts_directory) {
-            Ok(_) => {
-                match self.shift(configuration) {
-                    Ok(_) => {
-                        let first_directory = configuration.posts_directory.join("0");
-                        let _ = remove_dir_all(&first_directory);
-                        match create_dir_all(&configuration.posts_directory) {
-                            Ok(_) => Ok(()),
-                            Err(e) => Err(Error::new(e.kind(), format!("Could not create first post directory")))
-                        }
-                    },
-                    Err(e) => Err(e)
-                }
-            },
-            Err(e) => Err(Error::new(e.kind(), format!("Could not create posts directory")))
+        create_dir_all(&configuration.posts_directory)?;
+        for i in (0..configuration.max_posts).rev() {
+            let source = configuration.posts_directory.join(i.to_string());
+            let target = configuration.posts_directory.join((i + 1).to_string());
+
+            if source.exists() {
+                rename(&source, &target)?;
+            }
         }
+        Ok(())
     }
 
     fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
index ed6b066556042f9325d60167d523a4fd49bcf81c..3796df2b67a082024a3029bba57f5b9311f5d1b6 100644 (file)
@@ -1,4 +1,4 @@
-use std::fs::create_dir_all;
+use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, write};
 use std::io::{Result, Error, ErrorKind};
 use std::path::PathBuf;
 use super::{sync_down::SyncDown, generate::Generate, sync_up::SyncUp};
@@ -13,16 +13,38 @@ impl Update {
         Update
     }
 
-    fn copy_post(&self, post_location: &PathBuf) {
-        
+    fn copy_post(&self, source: &PathBuf, target: &PathBuf) -> Result<()> {
+        let post_name = source.file_name()
+            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Could not get post filename."))?;
+
+        let target_post = target.join(post_name);
+        copy(source, target_post)?;
+        Ok(())
     }
 
-    fn write_metadata(&self, metadata: Metadata, metadata_location: &PathBuf) {
-        
+    fn write_metadata(&self, metadata: &Metadata, metadata_location: &PathBuf) -> Result<()> {
+        let serialized_metadata = metadata.serialize();
+        write(metadata_location, serialized_metadata)?;
+        Ok(())
     }
 
-    fn archive(&self, post_location: &PathBuf) {
-        
+    fn archive(&self, source: &PathBuf, target: &PathBuf) -> Result<()> {
+        let entries = read_dir(source)?;
+        for entry in entries {
+            let entry = entry?;
+            let entry_type = entry.file_type()?;
+            let entry_name = entry.file_name();
+            let entry_source = entry.path();
+            let entry_target = target.join(entry_name);
+
+            if entry_type.is_dir() {
+                self.archive(&entry_source, &entry_target)?;
+            } else {
+                copy(&entry_source, &entry_target)?;
+            }
+        }
+
+        Ok(())
     }
 }
 
@@ -38,16 +60,29 @@ impl super::Command for Update {
             return Err(Error::new(ErrorKind::NotFound, "The path provided does not exist"));
         }
 
+        // Step 1. Write into the ephemeral posts
+
         create_dir_all(&configuration.posts_directory)?;
 
-        let metadata_file_path = configuration.posts_directory
-            .join("0")
-            .join(METADATA_FILENAME);
+        let first_post_path = configuration.posts_directory.join("0");
+        let metadata_file_path = first_post_path.join(METADATA_FILENAME);
         let metadata = Metadata::read_or_create(&metadata_file_path);
 
-        self.copy_post(&post_location);
-        self.write_metadata(metadata, &metadata_file_path);
-        self.archive(&post_location);
+        let _ = remove_dir_all(&first_post_path);
+        create_dir_all(&first_post_path)?;
+
+        self.copy_post(&post_location, &first_post_path)?;
+        self.write_metadata(&metadata, &metadata_file_path)?;
+
+        // Step 2. Write into the archive
+
+        create_dir_all(&configuration.archive_directory)?;
+
+        let post_archive_path = configuration.archive_directory.join(metadata.id);
+        let _ = remove_dir_all(&post_archive_path);
+        create_dir_all(&post_archive_path)?;
+
+        self.archive(&first_post_path, &post_archive_path)?;
         return Ok(())
     }
 
index 97ef0d3262eb5b084f43dc8f8336ba7613e649ea..0e3c25a0eaca33b0c562f616734c20f3e0a4ac5f 100644 (file)
@@ -10,7 +10,10 @@ pub struct Metadata {
 
 impl Metadata {
     pub fn serialize(&self) -> String {
-        format!(r#"{{\n  "id": "{}",\n  "created_on": {}\n}}"#, self.id, self.created_on)
+        format!(r#"{{
+  "id": "{}",
+  "created_on": {}
+}}"#, self.id, self.created_on)
     }
 
     pub fn deserialize(input: &str) -> Option<Metadata> {
@@ -20,8 +23,9 @@ impl Metadata {
             .collect::<String>();
 
         let id = Metadata::read_field(&clean_input, "id")?;
-        let created_on = Metadata::read_field(&clean_input, "createdOn")
-            .or_else(|| Metadata::read_field(&clean_input, "created_on"))?;
+        let created_on = Metadata::read_field(&clean_input, "created_on")
+            // createdOn kept for compatibility with blog < 7.0.0
+            .or_else(|| Metadata::read_field(&clean_input, "createdOn"))?;
 
         Some(Metadata {
             id,
@@ -54,7 +58,7 @@ impl Metadata {
 
 
     fn read_field(input: &str, field_name: &str) -> Option<String> {
-        let key_pattern = format!(r#""{}":""#, field_name);
+        let key_pattern = format!(r#""{}":"#, field_name);
         input.find(&key_pattern)
             .and_then(|start| {
                 let value_start = start + key_pattern.len();