11mod archive;
22
3- use std:: { collections:: HashMap , path:: PathBuf } ;
3+ use std:: {
4+ collections:: HashMap ,
5+ os,
6+ path:: { self , PathBuf } ,
7+ } ;
48
59pub use archive:: Method ;
610use axum:: {
711 body:: Body ,
812 extract:: { Multipart , Path , Query , State } ,
9- http:: { header , HeaderValue , Request , StatusCode , Uri } ,
13+ http:: { HeaderValue , Request , StatusCode , Uri , header } ,
1014 response:: IntoResponse ,
1115} ;
12- use file_share_app:: { shell, utils:: format_bytes, AppState } ;
16+ use file_share_app:: { AppConfig , AppState , shell, utils:: format_bytes} ;
1317use leptos:: { logging, prelude:: provide_context} ;
1418use rust_embed:: RustEmbed ;
1519use tokio:: io:: AsyncWriteExt ;
@@ -42,8 +46,7 @@ pub async fn file_and_error_handler(
4246 }
4347
4448 let handler = leptos_axum:: render_app_to_stream_with_context (
45- // app_state.leptos_options.clone(),
46- move || provide_context ( app_state. target_dir . clone ( ) ) ,
49+ move || provide_context ( app_state. app_config . clone ( ) ) ,
4750 move || shell ( app_state. leptos_options . clone ( ) ) ,
4851 ) ;
4952 handler ( request) . await . into_response ( )
@@ -52,22 +55,22 @@ pub async fn file_and_error_handler(
5255/// Handles archive requests.
5356#[ allow( clippy:: implicit_hasher) ]
5457pub async fn handle_archive_with_path < ' a > (
55- State ( base_dir ) : State < PathBuf > ,
58+ State ( AppConfig { target_dir , .. } ) : State < AppConfig > ,
5659 Path ( path) : Path < String > ,
5760 Query ( params) : Query < HashMap < String , String > > ,
5861) -> impl IntoResponse + use < ' a > {
5962 logging:: log!( "Handling archive with path '{path:?}' and params '{params:?}'" ) ;
60- handle_archive ( base_dir , params. get ( "method" ) , path) . await
63+ handle_archive ( target_dir , params. get ( "method" ) , path) . await
6164}
6265
6366/// Handles archive requests.
6467#[ allow( clippy:: implicit_hasher) ]
6568pub async fn handle_archive_without_path (
66- State ( base_dir ) : State < PathBuf > ,
69+ State ( AppConfig { target_dir , .. } ) : State < AppConfig > ,
6770 Query ( params) : Query < HashMap < String , String > > ,
6871) -> impl IntoResponse + use < > {
6972 logging:: log!( "Handling archive without path and with params '{params:?}'" ) ;
70- handle_archive ( base_dir , params. get ( "method" ) , String :: new ( ) ) . await
73+ handle_archive ( target_dir , params. get ( "method" ) , String :: new ( ) ) . await
7174}
7275
7376#[ allow( clippy:: unused_async) ] // has to be in an async context, but doesn't await directly
@@ -124,19 +127,32 @@ async fn handle_archive(
124127 ( headers, Body :: from_stream ( stream) ) . into_response ( )
125128}
126129
130+ const UPLOAD_DISABLED : ( StatusCode , & ' static str ) =
131+ ( StatusCode :: FORBIDDEN , "Upload is not enabled" ) ;
132+
127133pub async fn file_upload_with_path (
128- State ( base_dir ) : State < PathBuf > ,
134+ State ( AppState { app_config , .. } ) : State < AppState > ,
129135 Path ( path) : Path < String > ,
130136 multipart : Multipart ,
131137) -> impl IntoResponse {
132- file_upload ( base_dir, path, multipart) . await
138+ if !app_config. allow_upload {
139+ return UPLOAD_DISABLED . into_response ( ) ;
140+ }
141+ file_upload ( app_config. target_dir , path, multipart)
142+ . await
143+ . into_response ( )
133144}
134145
135146pub async fn file_upload_without_path (
136- State ( base_dir ) : State < PathBuf > ,
147+ State ( AppState { app_config , .. } ) : State < AppState > ,
137148 multipart : Multipart ,
138149) -> impl IntoResponse {
139- file_upload ( base_dir, String :: new ( ) , multipart) . await
150+ if !app_config. allow_upload {
151+ return UPLOAD_DISABLED . into_response ( ) ;
152+ }
153+ file_upload ( app_config. target_dir , String :: new ( ) , multipart)
154+ . await
155+ . into_response ( )
140156}
141157
142158pub async fn file_upload (
0 commit comments