From 866465c603cf169d24a65b8f77d4e462c7422f52 Mon Sep 17 00:00:00 2001 From: Violet White Date: Mon, 28 Oct 2019 17:28:28 -0400 Subject: [PATCH] Add environmental variable to control path of media (#683) --- plume-models/src/config.rs | 3 +++ plume-models/src/medias.rs | 15 +++++++++------ src/main.rs | 1 + src/routes/medias.rs | 9 +++++++-- src/routes/mod.rs | 13 ++++++++++--- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/plume-models/src/config.rs b/plume-models/src/config.rs index 41a989c1..40cece6d 100644 --- a/plume-models/src/config.rs +++ b/plume-models/src/config.rs @@ -15,6 +15,7 @@ pub struct Config { pub rocket: Result, pub logo: LogoConfig, pub default_theme: String, + pub media_directory: String, } #[derive(Debug, Clone)] @@ -201,5 +202,7 @@ lazy_static! { rocket: get_rocket_config(), logo: LogoConfig::default(), default_theme: var("DEFAULT_THEME").unwrap_or_else(|_| "default-light".to_owned()), + media_directory: var("MEDIA_UPLOAD_DIRECTORY") + .unwrap_or_else(|_| "static/media".to_owned()), }; } diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index 46f15a4b..21baf808 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -148,10 +148,12 @@ impl Media { if self.is_remote { Ok(self.remote_url.clone().unwrap_or_default()) } else { + let p = Path::new(&self.file_path); + let filename: String = p.file_name().unwrap().to_str().unwrap().to_owned(); Ok(ap_url(&format!( - "{}/{}", + "{}/static/media/{}", Instance::get_local()?.public_domain, - self.file_path + &filename ))) } } @@ -202,10 +204,11 @@ impl Media { .next() .map(ToOwned::to_owned) .unwrap_or_else(|| String::from("png")); - let path = - Path::new("static") - .join("media") - .join(format!("{}.{}", GUID::rand().to_string(), ext)); + let path = Path::new(&super::CONFIG.media_directory).join(format!( + "{}.{}", + GUID::rand().to_string(), + ext + )); let mut dest = fs::File::create(path.clone()).ok()?; reqwest::get(remote_url.as_str()) diff --git a/src/main.rs b/src/main.rs index f2150bad..4df0b0be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -239,6 +239,7 @@ Then try to restart Plume routes::theme_files, routes::plume_static_files, routes::static_files, + routes::plume_media_files, routes::tags::tag, routes::timelines::details, routes::timelines::new, diff --git a/src/routes/medias.rs b/src/routes/medias.rs index b8017445..c5b464cf 100644 --- a/src/routes/medias.rs +++ b/src/routes/medias.rs @@ -3,7 +3,7 @@ use multipart::server::{ save::{SaveResult, SavedData}, Multipart, }; -use plume_models::{db_conn::DbConn, medias::*, users::User, Error, PlumeRocket}; +use plume_models::{db_conn::DbConn, medias::*, users::User, Error, PlumeRocket, CONFIG}; use rocket::{ http::ContentType, response::{status, Flash, Redirect}, @@ -72,7 +72,12 @@ pub fn upload( .map(|ext| format!(".{}", ext)) }) .unwrap_or_default(); - let dest = format!("static/media/{}{}", GUID::rand().to_string(), ext); + let dest = format!( + "{}/{}{}", + CONFIG.media_directory, + GUID::rand().to_string(), + ext + ); match fields["file"][0].data { SavedData::Bytes(ref bytes) => fs::write(&dest, bytes) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 51c944a5..171de90b 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -17,8 +17,7 @@ use std::{ }; use template_utils::Ructe; -use plume_models::{posts::Post, Connection}; - +use plume_models::{posts::Post, Connection, CONFIG}; const ITEMS_PER_PAGE: i32 = 12; /// Special return type used for routes that "cannot fail", and instead @@ -212,7 +211,15 @@ pub fn theme_files(file: PathBuf, _build_id: &RawStr) -> Option { pub fn plume_static_files(file: PathBuf, _build_id: &RawStr) -> Option { static_files(file) } - +#[get("/static/media/")] +pub fn plume_media_files(file: PathBuf) -> Option { + NamedFile::open(Path::new(&CONFIG.media_directory).join(file)) + .ok() + .map(|f| CachedFile { + inner: f, + cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]), + }) +} #[get("/static/", rank = 3)] pub fn static_files(file: PathBuf) -> Option { NamedFile::open(Path::new("static/").join(file))