From 66c1c10d39e6ecb212ec4709888493693339c07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Sun, 24 Mar 2019 10:25:48 +0100 Subject: Implement file upload. --- src/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f662a73..37a3226 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(proc_macro_hygiene)] +use actix_web::http::Method; use actix_web::{fs, middleware, server, App}; use clap::crate_version; use simplelog::{Config, LevelFilter, TermLogger}; @@ -13,6 +14,7 @@ mod archive; mod args; mod auth; mod errors; +mod file_upload; mod listing; mod renderer; @@ -195,6 +197,11 @@ fn configure_app(app: App) -> App { let random_route = app.state().random_route.clone().unwrap_or_default(); let full_route = format!("/{}", random_route); + //allow file upload + let app = app.resource("/upload", |r| { + r.method(Method::POST).f(file_upload::upload_file) + }); + if let Some(s) = s { // Handle directories app.handler(&full_route, s) -- cgit v1.2.3 From 1e8783e23c3c5e64acc40464329b64e3de6e2a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Mon, 25 Mar 2019 14:44:45 +0100 Subject: Document file upload. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 37a3226..fb7e321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -197,7 +197,7 @@ fn configure_app(app: App) -> App { let random_route = app.state().random_route.clone().unwrap_or_default(); let full_route = format!("/{}", random_route); - //allow file upload + // Allow file upload let app = app.resource("/upload", |r| { r.method(Method::POST).f(file_upload::upload_file) }); -- cgit v1.2.3 From d14e17d94964291fda976423c1fe1a772d5af60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Wed, 27 Mar 2019 09:44:29 +0100 Subject: Add CLI arguments for file uploading. --- src/main.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index fb7e321..f7d0b1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,12 @@ pub struct MiniserveConfig { /// Enable random route generation pub random_route: Option, + + /// Enable file upload + pub file_upload: bool, + + /// Enable upload to override existing files + pub override_files: bool, } fn main() { @@ -175,11 +181,12 @@ fn main() { } /// Configures the Actix application -fn configure_app(app: App) -> App { +fn configure_app(mut app: App) -> App { 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(); if path.is_file() { None } else { @@ -188,7 +195,13 @@ fn configure_app(app: App) -> App { .expect("Couldn't create path") .show_files_listing() .files_listing_renderer(move |dir, req| { - listing::directory_listing(dir, req, no_symlinks, random_route.clone()) + listing::directory_listing( + dir, + req, + no_symlinks, + file_upload, + random_route.clone(), + ) }), ) } @@ -198,9 +211,11 @@ fn configure_app(app: App) -> App { let full_route = format!("/{}", random_route); // Allow file upload - let app = app.resource("/upload", |r| { - r.method(Method::POST).f(file_upload::upload_file) - }); + if app.state().file_upload { + app = app.resource("/upload", |r| { + r.method(Method::POST).f(file_upload::upload_file) + }); + } if let Some(s) = s { // Handle directories -- cgit v1.2.3 From 84b5852aad17961dfa2cb6ea3351b9fa3244fe6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Thu, 28 Mar 2019 11:29:56 +0100 Subject: Fix file upload when used with random route. --- src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/main.rs') 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) -> App { + 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) -> App { no_symlinks, file_upload, random_route.clone(), + u_r.clone(), ) }), ) @@ -212,7 +219,7 @@ fn configure_app(mut app: App) -> App { // 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) }); } -- cgit v1.2.3 From 975fb16e5bde3d474bbadc99e2846b1940e6b954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Thu, 28 Mar 2019 18:33:07 +0100 Subject: Improve how file upload handler is added to server. --- src/main.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 7ae06bd..935d619 100644 --- a/src/main.rs +++ b/src/main.rs @@ -181,7 +181,7 @@ fn main() { } /// Configures the Actix application -fn configure_app(mut app: App) -> App { +fn configure_app(app: App) -> App { let upload_route; let s = { let path = &app.state().path; @@ -217,16 +217,18 @@ fn configure_app(mut app: App) -> App { let random_route = app.state().random_route.clone().unwrap_or_default(); let full_route = format!("/{}", random_route); - // Allow file upload - if app.state().file_upload { - app = app.resource(&upload_route, |r| { - r.method(Method::POST).f(file_upload::upload_file) - }); - } - if let Some(s) = s { - // Handle directories - app.handler(&full_route, s) + if app.state().file_upload { + // Allow file upload + app.resource(&upload_route, |r| { + r.method(Method::POST).f(file_upload::upload_file) + }) + // Handle directories + .handler(&full_route, s) + } else { + // Handle directories + app.handler(&full_route, s) + } } else { // Handle single files app.resource(&full_route, |r| r.f(listing::file_handler)) -- cgit v1.2.3 From 8f3d5b70416e9275fdd0953181dee3bd6c6b0c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Fri, 29 Mar 2019 17:47:33 +0100 Subject: Fix syntax error and clippy warnings. --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 935d619..c8c13ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -187,10 +187,10 @@ fn configure_app(app: App) -> App { 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(); + let file_upload = app.state().file_upload; upload_route = match app.state().random_route.clone() { Some(random_route) => format!("/{}/upload", random_route), - None => format!("/upload"), + None => "/upload".to_string(), }; if path.is_file() { None -- cgit v1.2.3 From 93ea625fe29ea02c5bda6b4f6361134a0fe667f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Pej=C5=A1a?= Date: Fri, 29 Mar 2019 23:33:27 +0100 Subject: Fix typos and indentation. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c8c13ea..0ca2fdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ pub struct MiniserveConfig { pub file_upload: bool, /// Enable upload to override existing files - pub override_files: bool, + pub overwrite_files: bool, } fn main() { -- cgit v1.2.3