Browse Source

finish up the storage backend

xenua 2 years ago
parent
commit
f8ba54299e
  1. 34
      src/data.rs

34
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 { struct UserData {
pub username: String, pub username: String,
pub pw_hash: String, pub pw_hash: String,
@ -173,7 +179,14 @@ impl DataStorage {
} }
pub fn create_file(&mut self, id: String, file_meta: FileMeta) -> Result<(), Error> { 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> { 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> { 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> { pub fn edit_redirect(&mut self, id: String, new_target: String) -> Result<(), Error> {
@ -319,4 +338,15 @@ impl DataStorage {
Ok(()) 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(())
}
} }

Loading…
Cancel
Save