mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 11:59:48 -05:00
refactor(paste): associate directories with the paste types
This commit is contained in:
parent
47f6ff57bd
commit
3223c6379c
3 changed files with 32 additions and 10 deletions
|
@ -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())
|
||||
|
|
31
src/paste.rs
31
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(())
|
||||
}
|
||||
|
|
|
@ -29,9 +29,11 @@ async fn serve(
|
|||
) -> Result<HttpResponse, Error> {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue