@@ -32,29 +32,48 @@ pub struct NotificationParams {
3232
3333#[ command]
3434pub async fn download_file ( app : AppHandle , params : DownloadFileParams ) -> Result < ( ) , String > {
35- let window: WebviewWindow = app. get_webview_window ( "pake" ) . unwrap ( ) ;
35+ let window: WebviewWindow = app. get_webview_window ( "pake" ) . ok_or ( "Window not found" ) ?;
36+
3637 show_toast (
3738 & window,
3839 & get_download_message_with_lang ( MessageType :: Start , params. language . clone ( ) ) ,
3940 ) ;
4041
41- let output_path = app. path ( ) . download_dir ( ) . unwrap ( ) . join ( params. filename ) ;
42- let file_path = check_file_or_append ( output_path. to_str ( ) . unwrap ( ) ) ;
43- let client = ClientBuilder :: new ( ) . build ( ) . unwrap ( ) ;
42+ let download_dir = app
43+ . path ( )
44+ . download_dir ( )
45+ . map_err ( |e| format ! ( "Failed to get download dir: {}" , e) ) ?;
46+
47+ let output_path = download_dir. join ( & params. filename ) ;
48+
49+ let path_str = output_path. to_str ( ) . ok_or ( "Invalid output path" ) ?;
50+
51+ let file_path = check_file_or_append ( path_str) ;
4452
45- let response = client
46- . execute ( Request :: new (
47- Method :: GET ,
48- Url :: from_str ( & params. url ) . unwrap ( ) ,
49- ) )
50- . await ;
53+ let client = ClientBuilder :: new ( )
54+ . build ( )
55+ . map_err ( |e| format ! ( "Failed to build client: {}" , e) ) ?;
56+
57+ let url = Url :: from_str ( & params. url ) . map_err ( |e| format ! ( "Invalid URL: {}" , e) ) ?;
58+
59+ let request = Request :: new ( Method :: GET , url) ;
60+
61+ let response = client. execute ( request) . await ;
5162
5263 match response {
5364 Ok ( mut res) => {
54- let mut file = File :: create ( file_path) . unwrap ( ) ;
55- while let Some ( chunk) = res. chunk ( ) . await . unwrap ( ) {
56- file. write_all ( & chunk) . unwrap ( ) ;
65+ let mut file =
66+ File :: create ( file_path) . map_err ( |e| format ! ( "Failed to create file: {}" , e) ) ?;
67+
68+ while let Some ( chunk) = res
69+ . chunk ( )
70+ . await
71+ . map_err ( |e| format ! ( "Failed to get chunk: {}" , e) ) ?
72+ {
73+ file. write_all ( & chunk)
74+ . map_err ( |e| format ! ( "Failed to write chunk: {}" , e) ) ?;
5775 }
76+
5877 show_toast (
5978 & window,
6079 & get_download_message_with_lang ( MessageType :: Success , params. language . clone ( ) ) ,
@@ -76,15 +95,25 @@ pub async fn download_file_by_binary(
7695 app : AppHandle ,
7796 params : BinaryDownloadParams ,
7897) -> Result < ( ) , String > {
79- let window: WebviewWindow = app. get_webview_window ( "pake" ) . unwrap ( ) ;
98+ let window: WebviewWindow = app. get_webview_window ( "pake" ) . ok_or ( "Window not found" ) ?;
99+
80100 show_toast (
81101 & window,
82102 & get_download_message_with_lang ( MessageType :: Start , params. language . clone ( ) ) ,
83103 ) ;
84- let output_path = app. path ( ) . download_dir ( ) . unwrap ( ) . join ( params. filename ) ;
85- let file_path = check_file_or_append ( output_path. to_str ( ) . unwrap ( ) ) ;
86- let download_file_result = fs:: write ( file_path, & params. binary ) ;
87- match download_file_result {
104+
105+ let download_dir = app
106+ . path ( )
107+ . download_dir ( )
108+ . map_err ( |e| format ! ( "Failed to get download dir: {}" , e) ) ?;
109+
110+ let output_path = download_dir. join ( & params. filename ) ;
111+
112+ let path_str = output_path. to_str ( ) . ok_or ( "Invalid output path" ) ?;
113+
114+ let file_path = check_file_or_append ( path_str) ;
115+
116+ match fs:: write ( file_path, & params. binary ) {
88117 Ok ( _) => {
89118 show_toast (
90119 & window,
@@ -111,21 +140,22 @@ pub fn send_notification(app: AppHandle, params: NotificationParams) -> Result<(
111140 . body ( & params. body )
112141 . icon ( & params. icon )
113142 . show ( )
114- . unwrap ( ) ;
143+ . map_err ( |e| format ! ( "Failed to show notification: {}" , e ) ) ? ;
115144 Ok ( ( ) )
116145}
117146
118147#[ command]
119148pub async fn update_theme_mode ( app : AppHandle , mode : String ) {
120149 #[ cfg( target_os = "macos" ) ]
121150 {
122- let window = app. get_webview_window ( "pake" ) . unwrap ( ) ;
123- let theme = if mode == "dark" {
124- Theme :: Dark
125- } else {
126- Theme :: Light
127- } ;
128- let _ = window. set_theme ( Some ( theme) ) ;
151+ if let Some ( window) = app. get_webview_window ( "pake" ) {
152+ let theme = if mode == "dark" {
153+ Theme :: Dark
154+ } else {
155+ Theme :: Light
156+ } ;
157+ let _ = window. set_theme ( Some ( theme) ) ;
158+ }
129159 }
130160 #[ cfg( not( target_os = "macos" ) ) ]
131161 {
0 commit comments