@@ -13,6 +13,7 @@ use tauri::{
13
13
} ;
14
14
15
15
use std:: {
16
+ borrow:: Cow ,
16
17
fs:: File ,
17
18
io:: { BufReader , Lines , Read , Write } ,
18
19
path:: { Path , PathBuf } ,
@@ -105,6 +106,8 @@ pub enum CommandError {
105
106
#[ error( transparent) ]
106
107
Tauri ( #[ from] tauri:: Error ) ,
107
108
#[ error( transparent) ]
109
+ Json ( #[ from] serde_json:: Error ) ,
110
+ #[ error( transparent) ]
108
111
Io ( #[ from] std:: io:: Error ) ,
109
112
#[ error( transparent) ]
110
113
UrlParseError ( #[ from] url:: ParseError ) ,
@@ -910,25 +913,31 @@ pub async fn write_file<R: Runtime>(
910
913
command_scope : CommandScope < Entry > ,
911
914
request : tauri:: ipc:: Request < ' _ > ,
912
915
) -> CommandResult < ( ) > {
913
- if let tauri:: ipc:: InvokeBody :: Raw ( data) = request. body ( ) {
914
- let path = request
915
- . headers ( )
916
- . get ( "path" )
917
- . ok_or_else ( || anyhow:: anyhow!( "missing file path" ) . into ( ) )
918
- . and_then ( |p| {
919
- p. to_str ( )
920
- . map_err ( |e| anyhow:: anyhow!( "invalid path: {e}" ) . into ( ) )
921
- } )
922
- . and_then ( |p| SafeFilePath :: from_str ( p) . map_err ( CommandError :: from) ) ?;
923
- let options = request
924
- . headers ( )
925
- . get ( "options" )
926
- . and_then ( |p| p. to_str ( ) . ok ( ) )
927
- . and_then ( |opts| serde_json:: from_str ( opts) . ok ( ) ) ;
928
- write_file_inner ( webview, & global_scope, & command_scope, path, data, options)
929
- } else {
930
- Err ( anyhow:: anyhow!( "unexpected invoke body" ) . into ( ) )
931
- }
916
+ let data = match request. body ( ) {
917
+ tauri:: ipc:: InvokeBody :: Raw ( data) => Cow :: Borrowed ( data) ,
918
+ tauri:: ipc:: InvokeBody :: Json ( serde_json:: Value :: Array ( data) ) => Cow :: Owned (
919
+ data. iter ( )
920
+ . flat_map ( |v| v. as_number ( ) . and_then ( |v| v. as_u64 ( ) . map ( |v| v as u8 ) ) )
921
+ . collect ( ) ,
922
+ ) ,
923
+ _ => return Err ( anyhow:: anyhow!( "unexpected invoke body" ) . into ( ) ) ,
924
+ } ;
925
+
926
+ let path = request
927
+ . headers ( )
928
+ . get ( "path" )
929
+ . ok_or_else ( || anyhow:: anyhow!( "missing file path" ) . into ( ) )
930
+ . and_then ( |p| {
931
+ p. to_str ( )
932
+ . map_err ( |e| anyhow:: anyhow!( "invalid path: {e}" ) . into ( ) )
933
+ } )
934
+ . and_then ( |p| SafeFilePath :: from_str ( p) . map_err ( CommandError :: from) ) ?;
935
+ let options = request
936
+ . headers ( )
937
+ . get ( "options" )
938
+ . and_then ( |p| p. to_str ( ) . ok ( ) )
939
+ . and_then ( |opts| serde_json:: from_str ( opts) . ok ( ) ) ;
940
+ write_file_inner ( webview, & global_scope, & command_scope, path, & data, options)
932
941
}
933
942
934
943
#[ tauri:: command]
0 commit comments