|
|
@ -1,6 +1,6 @@ |
|
|
|
use std::{ |
|
|
|
use std::{ |
|
|
|
collections::HashMap, |
|
|
|
collections::HashMap, |
|
|
|
fs::{read_to_string, DirBuilder, File}, |
|
|
|
fs::{read_to_string, remove_file, DirBuilder, File}, |
|
|
|
io::{self, BufRead}, |
|
|
|
io::{self, BufRead}, |
|
|
|
path::PathBuf, |
|
|
|
path::PathBuf, |
|
|
|
}; |
|
|
|
}; |
|
|
@ -30,6 +30,8 @@ pub enum Error { |
|
|
|
ShitMissing(String), |
|
|
|
ShitMissing(String), |
|
|
|
#[error("filesystem error")] |
|
|
|
#[error("filesystem error")] |
|
|
|
FS, |
|
|
|
FS, |
|
|
|
|
|
|
|
#[error("id collision wtf")] |
|
|
|
|
|
|
|
IDCollision, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn meta_str_to_map(from: &str) -> Result<HashMap<&str, String>, Error> { |
|
|
|
fn meta_str_to_map(from: &str) -> Result<HashMap<&str, String>, Error> { |
|
|
@ -65,6 +67,15 @@ impl TextMeta { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for TextMeta { |
|
|
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
|
|
|
|
|
let buf = "".to_string(); |
|
|
|
|
|
|
|
self.language |
|
|
|
|
|
|
|
.map(|lang| buf += format!("{}\n", lang).as_str()); |
|
|
|
|
|
|
|
write!(f, "{}---\n", buf) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct FileMeta { |
|
|
|
struct FileMeta { |
|
|
|
file_name: String, |
|
|
|
file_name: String, |
|
|
|
} |
|
|
|
} |
|
|
@ -128,6 +139,73 @@ impl DataStorage { |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn create_text(&mut self, id: String, text: String, meta: TextMeta) -> Result<(), Error> { |
|
|
|
|
|
|
|
if self.text.contains_key(&id) { |
|
|
|
|
|
|
|
return Err(Error::IDCollision); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
self.text.insert(id, TextPaste { text, meta }); |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn edit_text( |
|
|
|
|
|
|
|
&mut self, |
|
|
|
|
|
|
|
id: String, |
|
|
|
|
|
|
|
new_text: Option<String>, |
|
|
|
|
|
|
|
new_meta: Option<TextMeta>, |
|
|
|
|
|
|
|
) -> Result<(), Error> { |
|
|
|
|
|
|
|
let mut paste = self.text.get(&id).ok_or(Error::ShitMissing(id))?; |
|
|
|
|
|
|
|
new_text.map(|nt| paste.text = nt); |
|
|
|
|
|
|
|
new_meta.map(|nm| paste.meta = nm); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn delete_text(&mut self, id: String) -> Result<String, Error> { |
|
|
|
|
|
|
|
let old_text = self |
|
|
|
|
|
|
|
.text |
|
|
|
|
|
|
|
.remove(&id) |
|
|
|
|
|
|
|
.ok_or(Error::ShitMissing(id)) |
|
|
|
|
|
|
|
.map(|(_, paste)| paste.text)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
remove_file(self.base_dir.join(*TEXT_DIR).join(id))?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(old_text) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn create_file(&mut self, id: String, file_meta: FileMeta) -> Result<(), Error> { |
|
|
|
|
|
|
|
todo!() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn delete_file(&mut self, id: String) -> Result<(), Error> { |
|
|
|
|
|
|
|
let file_meta = self.files.remove(&id).ok_or(Error::ShitMissing(id))?.1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
remove_file(self.base_dir.join(*FILE_DIR).join(id))?; |
|
|
|
|
|
|
|
remove_file(self.base_dir.join(*FILE_DIR).join(id + ".meta"))?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn create_redirect(&mut self, id: String, target: String) -> Result<(), Error> { |
|
|
|
|
|
|
|
todo!() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn edit_redirect(&mut self, id: String, new_target: String) -> Result<(), Error> { |
|
|
|
|
|
|
|
if !self.redirects.contains_key(&id) { |
|
|
|
|
|
|
|
return Err(Error::ShitMissing(id)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
self.redirects.insert(id, new_target); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.write_redirects()?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn delete_redirect(&mut self, id: String) -> Result<(), Error> { |
|
|
|
|
|
|
|
self.redirects.remove(&id).ok_or(Error::ShitMissing(id))?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.write_redirects()?; |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn ensure_folder_structure(&self) -> Result<(), Error> { |
|
|
|
fn ensure_folder_structure(&self) -> Result<(), Error> { |
|
|
|
let dir_builder = DirBuilder::new().recursive(true); |
|
|
|
let dir_builder = DirBuilder::new().recursive(true); |
|
|
|
if cfg!(target_family = "unix") { |
|
|
|
if cfg!(target_family = "unix") { |
|
|
@ -220,4 +298,25 @@ impl DataStorage { |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn write_redirects(&self) -> Result<(), Error> { |
|
|
|
|
|
|
|
let fuckery: String = self |
|
|
|
|
|
|
|
.redirects |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.map(|(id, target)| format!("{}: {}\n", id, target)) |
|
|
|
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
std::fs::write(self.base_dir.join(*REDIRECTS_FILE), fuckery)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn write_text(&self, id: String) -> Result<(), Error> { |
|
|
|
|
|
|
|
let text_paste = self.text.get(&id).ok_or(Error::ShitMissing(id))?; |
|
|
|
|
|
|
|
let fuckery = text_paste.meta.to_string(); |
|
|
|
|
|
|
|
fuckery += text_paste.text.as_str(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::fs::write(self.base_dir.join(*TEXT_DIR).join(id), fuckery)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|