From f8ba54299eb54a6641b2d70acd1b18bd4eaa08c9 Mon Sep 17 00:00:00 2001 From: xenua Date: Sat, 7 Jan 2023 15:11:16 +0100 Subject: [PATCH] finish up the storage backend --- src/data.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/data.rs b/src/data.rs index c6930ca..97a83f2 100644 --- a/src/data.rs +++ b/src/data.rs @@ -89,6 +89,12 @@ impl FileMeta { } } +impl std::fmt::Display for FileMeta { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "file_name: {}\n", self.file_name) + } +} + struct UserData { pub username: String, pub pw_hash: String, @@ -173,7 +179,14 @@ impl DataStorage { } pub fn create_file(&mut self, id: String, file_meta: FileMeta) -> Result<(), Error> { - todo!() + if self.files.contains_key(&id) { + return Err(Error::IDCollision); + } + + self.files.insert(id, file_meta); + + self.write_file_meta(id); + Ok(()) } pub fn delete_file(&mut self, id: String) -> Result<(), Error> { @@ -186,7 +199,13 @@ impl DataStorage { } pub fn create_redirect(&mut self, id: String, target: String) -> Result<(), Error> { - todo!() + if self.redirects.contains_key(&id) { + return Err(Error::IDCollision); + } + self.redirects.insert(id, target); + + self.write_redirects()?; + Ok(()) } pub fn edit_redirect(&mut self, id: String, new_target: String) -> Result<(), Error> { @@ -319,4 +338,15 @@ impl DataStorage { Ok(()) } + + fn write_file_meta(&self, id: String) -> Result<(), Error> { + let file_meta = *self.files.get(&id).ok_or(Error::ShitMissing(id))?; + + std::fs::write( + self.base_dir.join(*FILE_DIR).join(id), + file_meta.to_string(), + )?; + + Ok(()) + } }