From 3223c6379c501ddb601c8df1dae72b9511176246 Mon Sep 17 00:00:00 2001 From: orhun Date: Thu, 26 Aug 2021 21:09:33 +0300 Subject: [PATCH] refactor(paste): associate directories with the paste types --- src/main.rs | 3 ++- src/paste.rs | 31 +++++++++++++++++++++++++------ src/server.rs | 8 +++++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index d62ea10..8af2a9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use actix_web::middleware::Logger; use actix_web::{App, HttpServer}; use rustypaste::config::Config; +use rustypaste::paste::PasteType; use rustypaste::server; use std::env; use std::fs; @@ -14,7 +15,7 @@ async fn main() -> IoResult<()> { .expect("failed to parse config"); let server_config = config.server.clone(); fs::create_dir_all(&server_config.upload_path)?; - fs::create_dir_all(&server_config.upload_path.join("url"))?; + fs::create_dir_all(PasteType::Url.get_path(&server_config.upload_path))?; let mut http_server = HttpServer::new(move || { App::new() .data(config.clone()) diff --git a/src/paste.rs b/src/paste.rs index b87fb33..401bce8 100644 --- a/src/paste.rs +++ b/src/paste.rs @@ -3,7 +3,7 @@ use crate::header::ContentDisposition; use std::convert::TryFrom; use std::fs::{self, File}; use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult, Write}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::str; use url::Url; @@ -29,6 +29,21 @@ impl<'a> TryFrom<&'a ContentDisposition> for PasteType { } } +impl PasteType { + /// Returns the corresponding directory of the paste type. + pub fn get_dir(&self) -> String { + match self { + Self::File => String::new(), + Self::Url => String::from("url"), + } + } + + /// Returns the given path with [`directory`](Self::get_dir) adjoined. + pub fn get_path(&self, path: &Path) -> PathBuf { + path.join(self.get_dir()) + } +} + /// Representation of a single paste. #[derive(Debug)] pub struct Paste { @@ -110,8 +125,10 @@ impl Paste { .paste .random_url .generate() - .unwrap_or_else(|| String::from("url")); - let path = config.server.upload_path.join("url").join(&file_name); + .unwrap_or_else(|| PasteType::Url.get_dir()); + let path = PasteType::Url + .get_path(&config.server.upload_path) + .join(&file_name); fs::write(&path, url.to_string())?; Ok(file_name) } @@ -182,7 +199,7 @@ mod tests { assert_eq!("test", fs::read_to_string(&file_name)?); fs::remove_file(file_name)?; - fs::create_dir_all(config.server.upload_path.join("url"))?; + fs::create_dir_all(PasteType::Url.get_path(&config.server.upload_path))?; config.paste.random_url.enabled = true; let url = String::from("https://orhun.dev/"); @@ -191,7 +208,9 @@ mod tests { type_: PasteType::Url, }; let file_name = paste.store_url(&config)?; - let file_path = config.server.upload_path.join("url").join(&file_name); + let file_path = PasteType::Url + .get_path(&config.server.upload_path) + .join(&file_name); assert_eq!(url, fs::read_to_string(&file_path)?); fs::remove_file(file_path)?; @@ -202,7 +221,7 @@ mod tests { }; assert!(paste.store_url(&config).is_err()); - fs::remove_dir(config.server.upload_path.join("url"))?; + fs::remove_dir(PasteType::Url.get_path(&config.server.upload_path))?; Ok(()) } diff --git a/src/server.rs b/src/server.rs index c16c917..badee3c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -29,9 +29,11 @@ async fn serve( ) -> Result { let mut path = config.server.upload_path.join(&*file); let mut paste_type = PasteType::File; - for (type_, alt_path) in &[(PasteType::Url, "url")] { - if !path.exists() || path.file_name().map(|v| v.to_str()).flatten() == Some(alt_path) { - path = config.server.upload_path.join(alt_path).join(&*file); + for type_ in &[PasteType::Url] { + if !path.exists() + || path.file_name().map(|v| v.to_str()).flatten() == Some(&type_.get_dir()) + { + path = config.server.upload_path.join(type_.get_dir()).join(&*file); paste_type = *type_; break; }