@@ -10,7 +10,6 @@ use async_std::fs::File;
10
10
use diesel:: r2d2:: { self , ConnectionManager } ;
11
11
12
12
use futures:: { AsyncWriteExt , StreamExt } ;
13
- use lazy_static:: lazy_static;
14
13
15
14
use log:: { error, info} ;
16
15
@@ -37,7 +36,7 @@ pub(crate) async fn upload(
37
36
38
37
let mut file_ids: Vec < String > = Vec :: new ( ) ;
39
38
while let Some ( Ok ( field) ) = multipart. next ( ) . await {
40
- let path = & * FILE_SAVE_PATH ;
39
+ let path = & get_file_save_path ( ) ;
41
40
match save_file ( field, path, & username, & pool) . await {
42
41
Ok ( file_id) => file_ids. push ( file_id) ,
43
42
Err ( err) => return Either :: Right ( Err ( err) ) ,
@@ -55,37 +54,36 @@ pub(crate) async fn upload(
55
54
pub ( crate ) async fn view_file ( path_params : web:: Path < ( String , ) > ) -> Result < fs:: NamedFile > {
56
55
let path_params = path_params. into_inner ( ) ;
57
56
let file_id = & path_params. 0 ;
58
- let path = Path :: new ( & * FILE_SAVE_PATH ) ;
57
+ let save_path = get_file_save_path ( ) ;
58
+ let path = Path :: new ( & save_path) ;
59
59
//todo 判断是否是私有文件
60
60
Ok ( fs:: NamedFile :: open ( path. join ( file_id) ) ?)
61
61
}
62
62
fn success_with_file_ids ( file_ids : Vec < String > ) -> HttpResponse {
63
63
HttpResponse :: Ok ( ) . json ( result:: AjaxResult :: success ( Some ( file_ids) ) )
64
64
}
65
65
fn get_file_save_path ( ) -> String {
66
- let path = match config_util:: APP_CONFIG . get_string ( "tl.app.upload.path" ) {
67
- Ok ( path) => path,
68
- Err ( _) => String :: from ( "upload" ) ,
69
- } ;
66
+ let path = config_util:: get_app_config ( )
67
+ . get_string ( "tl.app.upload.path" )
68
+ . unwrap_or_else ( |_| String :: from ( "upload" ) ) ;
70
69
match std:: fs:: create_dir_all ( & path) {
71
70
Ok ( _) => info ! ( " app upload path:{}" , & path) ,
72
- Err ( _) => error ! ( "error create app uplod path: {}" , & path) ,
71
+ Err ( _) => error ! ( "error create app upload path: {}" , & path) ,
73
72
}
74
73
path
75
74
}
76
75
77
76
fn get_file_max_size_bytes ( ) -> usize {
78
- let max_size_mb = match config_util:: APP_CONFIG . get_float ( "tl.app.upload.max_size" ) {
79
- Ok ( size_mb) => size_mb,
80
- Err ( _) => 1.0 ,
81
- } ;
77
+ let max_size_mb = config_util:: get_app_config ( )
78
+ . get_float ( "tl.app.upload.max_size" )
79
+ . unwrap_or_else ( |_| 1.0 ) ;
82
80
( max_size_mb * 1024.0 * 1024.0 ) as usize
83
81
}
84
82
85
- lazy_static ! {
86
- static ref FILE_SAVE_PATH : String = get_file_save_path( ) ;
87
- static ref FILE_MAX_SIZE : usize = get_file_max_size_bytes( ) ;
88
- }
83
+ /* lazy_static! {
84
+ // static ref FILE_SAVE_PATH: String = get_file_save_path();
85
+ // static ref FILE_MAX_SIZE: usize = get_file_max_size_bytes();
86
+ }*/
89
87
90
88
//保存文件
91
89
async fn save_file (
@@ -107,7 +105,7 @@ async fn save_file(
107
105
while let Some ( bytes) = field. next ( ) . await {
108
106
let bytes = bytes?;
109
107
length += bytes. len ( ) ;
110
- if length > * FILE_MAX_SIZE {
108
+ if length > get_file_max_size_bytes ( ) {
111
109
error ! ( "err:{}" , "上传的文件过大" ) ;
112
110
return Err ( error:: ErrorInternalServerError ( "上传的文件过大" ) ) ;
113
111
//return Err(Error::from(Response::new(StatusCode::INTERNAL_SERVER_ERROR).set_body(BoxBody::new("上传的文件过大"))));
0 commit comments