diff options
author | Vojtěch Pejša <vojtechpejsa7@gmail.com> | 2019-03-28 10:29:56 +0000 |
---|---|---|
committer | Vojtěch Pejša <vojtechpejsa7@gmail.com> | 2019-04-04 08:51:00 +0000 |
commit | 84b5852aad17961dfa2cb6ea3351b9fa3244fe6f (patch) | |
tree | b73a63302b16fef6f8a2553127e670ffea332119 | |
parent | Add CLI arguments for file uploading. (diff) | |
download | miniserve-84b5852aad17961dfa2cb6ea3351b9fa3244fe6f.tar.gz miniserve-84b5852aad17961dfa2cb6ea3351b9fa3244fe6f.zip |
Fix file upload when used with random route.
-rw-r--r-- | src/file_upload.rs | 6 | ||||
-rw-r--r-- | src/listing.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 9 | ||||
-rw-r--r-- | src/renderer.rs | 5 |
4 files changed, 22 insertions, 6 deletions
diff --git a/src/file_upload.rs b/src/file_upload.rs index 9f87724..98e3680 100644 --- a/src/file_upload.rs +++ b/src/file_upload.rs @@ -1,6 +1,6 @@ use actix_web::{ dev, error, - http::header::{ContentDisposition, LOCATION}, + http::header::{ContentDisposition, LOCATION, REFERER}, multipart, Error, FromRequest, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Query, }; use futures::{future, Future, Stream}; @@ -99,6 +99,7 @@ pub fn upload_file(req: &HttpRequest<crate::MiniserveConfig>) -> FutureResponse< )) } }; + let return_path = req.headers()[REFERER].clone(); // if target path is under app root directory save file let target_dir = match &app_root_dir.clone().join(path.clone()).canonicalize() { @@ -112,9 +113,10 @@ pub fn upload_file(req: &HttpRequest<crate::MiniserveConfig>) -> FutureResponse< .map(move |item| handle_multipart(item, target_dir.clone(), override_files)) .flatten() .collect() + //.map(|s| HttpResponse::Ok().json(s)) .map(move |_| { HttpResponse::TemporaryRedirect() - .header(LOCATION, format!("{}", path.display())) + .header(LOCATION, format!("{}", return_path.to_str().unwrap_or("/"))) .finish() }) .map_err(|e| e), diff --git a/src/listing.rs b/src/listing.rs index 5fde879..4a0927b 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -132,12 +132,17 @@ pub fn directory_listing<S>( skip_symlinks: bool, file_upload: bool, random_route: Option<String>, + upload_route: String, ) -> Result<HttpResponse, io::Error> { let title = format!("Index of {}", req.path()); let base = Path::new(req.path()); let random_route = format!("/{}", random_route.unwrap_or_default()); let is_root = base.parent().is_none() || req.path() == random_route; let page_parent = base.parent().map(|p| p.display().to_string()); + let current_dir = match base.strip_prefix(random_route) { + Ok(c_d) => Path::new("/").join(c_d), + Err(_) => base.to_path_buf(), + }; let (sort_method, sort_order, download) = if let Ok(query) = Query::<QueryParameters>::extract(req) { @@ -267,7 +272,8 @@ pub fn directory_listing<S>( sort_method, sort_order, file_upload, - &base.to_string_lossy(), + &upload_route, + ¤t_dir.display().to_string(), ) .into_string(), )) diff --git a/src/main.rs b/src/main.rs index f7d0b1b..7ae06bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,14 +182,20 @@ fn main() { /// Configures the Actix application fn configure_app(mut app: App<MiniserveConfig>) -> App<MiniserveConfig> { + let upload_route; let s = { let path = &app.state().path; let no_symlinks = app.state().no_symlinks; let random_route = app.state().random_route.clone(); let file_upload = app.state().file_upload.clone(); + upload_route = match app.state().random_route.clone() { + Some(random_route) => format!("/{}/upload", random_route), + None => format!("/upload"), + }; if path.is_file() { None } else { + let u_r = upload_route.clone(); Some( fs::StaticFiles::new(path) .expect("Couldn't create path") @@ -201,6 +207,7 @@ fn configure_app(mut app: App<MiniserveConfig>) -> App<MiniserveConfig> { no_symlinks, file_upload, random_route.clone(), + u_r.clone(), ) }), ) @@ -212,7 +219,7 @@ fn configure_app(mut app: App<MiniserveConfig>) -> App<MiniserveConfig> { // Allow file upload if app.state().file_upload { - app = app.resource("/upload", |r| { + app = app.resource(&upload_route, |r| { r.method(Method::POST).f(file_upload::upload_file) }); } diff --git a/src/renderer.rs b/src/renderer.rs index 6b3fb9f..0a9f42b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -15,7 +15,8 @@ pub fn page( sort_method: Option<listing::SortingMethod>, sort_order: Option<listing::SortingOrder>, file_upload: bool, - base: &str, + upload_route: &str, + current_dir: &str, ) -> Markup { html! { (page_header(page_title)) @@ -23,7 +24,7 @@ pub fn page( span #top { } h1 { (page_title) } @if file_upload { - form action={"/upload?path=" (base)} method="POST" enctype="multipart/form-data" { + form action={(upload_route) "?path=" (current_dir)} method="POST" enctype="multipart/form-data" { p { "Select file to upload" } input type="file" name="file_to_upload" {} input type="submit" value="Upload file" {} |