@@ -69,6 +69,9 @@ use tokio::net::{TcpListener, TcpStream};
69
69
use tokio_rustls:: server:: TlsStream ;
70
70
use tokio_rustls:: TlsAcceptor ;
71
71
72
+ mod httpsfserror;
73
+ use httpsfserror:: HttpsFSError ;
74
+
72
75
/// A file system exposed over https
73
76
pub struct HttpsFS {
74
77
addr : String ,
@@ -320,6 +323,14 @@ impl HttpsFS {
320
323
let cert = reqwest:: Certificate :: from_pem ( & buf) ?;
321
324
Ok ( cert)
322
325
}
326
+
327
+ fn exec_command ( & self , cmd : & Command ) -> Result < CommandResponse , HttpsFSError > {
328
+ let req = serde_json:: to_string ( & cmd) ?;
329
+ let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
330
+ let result = result. text ( ) ?;
331
+ let result: CommandResponse = serde_json:: from_str ( & result) ?;
332
+ Ok ( result)
333
+ }
323
334
}
324
335
325
336
impl HttpsFSBuilder {
@@ -913,10 +924,7 @@ impl FileSystem for HttpsFS {
913
924
let req = Command :: ReadDir ( CommandReadDir {
914
925
path : String :: from ( path) ,
915
926
} ) ;
916
- let req = serde_json:: to_string ( & req) ?;
917
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
918
- let result = result. text ( ) ?;
919
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
927
+ let result = self . exec_command ( & req) ?;
920
928
let result = match result {
921
929
CommandResponse :: ReadDir ( value) => value,
922
930
_ => {
@@ -937,10 +945,7 @@ impl FileSystem for HttpsFS {
937
945
let req = Command :: CreateDir ( CommandCreateDir {
938
946
path : String :: from ( path) ,
939
947
} ) ;
940
- let req = serde_json:: to_string ( & req) ?;
941
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
942
- let result = result. text ( ) ?;
943
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
948
+ let result = self . exec_command ( & req) ?;
944
949
let result = match result {
945
950
CommandResponse :: CreateDir ( value) => value,
946
951
_ => {
@@ -977,10 +982,7 @@ impl FileSystem for HttpsFS {
977
982
let req = Command :: CreateFile ( CommandCreateFile {
978
983
path : String :: from ( path) ,
979
984
} ) ;
980
- let req = serde_json:: to_string ( & req) ?;
981
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
982
- let result = result. text ( ) ?;
983
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
985
+ let result = self . exec_command ( & req) ?;
984
986
let result = match result {
985
987
CommandResponse :: CreateFile ( value) => value,
986
988
_ => {
@@ -992,7 +994,7 @@ impl FileSystem for HttpsFS {
992
994
993
995
match result {
994
996
CommandResponseCreateFile :: Failed => Err ( VfsError :: Other {
995
- message : String :: from ( "Result doesn't match the request !" ) ,
997
+ message : String :: from ( "Faild to create file !" ) ,
996
998
} ) ,
997
999
CommandResponseCreateFile :: Success => Ok ( Box :: new ( WritableFile {
998
1000
client : self . client . clone ( ) ,
@@ -1017,10 +1019,7 @@ impl FileSystem for HttpsFS {
1017
1019
let req = Command :: Metadata ( CommandMetadata {
1018
1020
path : String :: from ( path) ,
1019
1021
} ) ;
1020
- let req = serde_json:: to_string ( & req) ?;
1021
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
1022
- let result = result. text ( ) ?;
1023
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
1022
+ let result = self . exec_command ( & req) ?;
1024
1023
match result {
1025
1024
CommandResponse :: Metadata ( value) => meta_res_convert_cmd_vfs ( value) ,
1026
1025
_ => Err ( VfsError :: Other {
@@ -1037,25 +1036,7 @@ impl FileSystem for HttpsFS {
1037
1036
let req = Command :: Exists ( CommandExists {
1038
1037
path : String :: from ( path) ,
1039
1038
} ) ;
1040
-
1041
- let req = serde_json:: to_string ( & req) ;
1042
- if let Err ( e) = req {
1043
- println ! ( "Error: {:?}" , e) ;
1044
- return false ;
1045
- }
1046
- let req = req. unwrap ( ) ;
1047
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ;
1048
- if let Err ( e) = result {
1049
- println ! ( "Error: {:?}" , e) ;
1050
- return false ;
1051
- }
1052
- let result = result. unwrap ( ) . text ( ) ;
1053
- if let Err ( e) = result {
1054
- println ! ( "Error: {:?}" , e) ;
1055
- return false ;
1056
- }
1057
- let result: Result < CommandResponse , serde_json:: Error > =
1058
- serde_json:: from_str ( & result. unwrap ( ) ) ;
1039
+ let result = self . exec_command ( & req) ;
1059
1040
if let Err ( e) = result {
1060
1041
println ! ( "Error: {:?}" , e) ;
1061
1042
return false ;
@@ -1070,10 +1051,7 @@ impl FileSystem for HttpsFS {
1070
1051
let req = Command :: RemoveFile ( CommandRemoveFile {
1071
1052
path : String :: from ( path) ,
1072
1053
} ) ;
1073
- let req = serde_json:: to_string ( & req) ?;
1074
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
1075
- let result = result. text ( ) ?;
1076
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
1054
+ let result = self . exec_command ( & req) ?;
1077
1055
let result = match result {
1078
1056
CommandResponse :: RemoveFile ( value) => value,
1079
1057
_ => {
@@ -1095,10 +1073,7 @@ impl FileSystem for HttpsFS {
1095
1073
let req = Command :: RemoveDir ( CommandRemoveDir {
1096
1074
path : String :: from ( path) ,
1097
1075
} ) ;
1098
- let req = serde_json:: to_string ( & req) ?;
1099
- let result = self . client . post ( & self . addr ) . body ( req) . send ( ) ?;
1100
- let result = result. text ( ) ?;
1101
- let result: CommandResponse = serde_json:: from_str ( & result) ?;
1076
+ let result = self . exec_command ( & req) ?;
1102
1077
let result = match result {
1103
1078
CommandResponse :: RemoveDir ( value) => value,
1104
1079
_ => {
0 commit comments