@@ -20,21 +20,63 @@ mod scope;
20
20
21
21
pub ( crate ) struct Http {
22
22
#[ cfg( feature = "cookies" ) ]
23
- cookies_jar : std:: sync:: Arc < reqwest:: cookie:: Jar > ,
23
+ cookies_jar_path : std:: path:: PathBuf ,
24
+ #[ cfg( feature = "cookies" ) ]
25
+ cookies_jar : std:: sync:: Arc < reqwest_cookie_store:: CookieStoreMutex > ,
24
26
}
25
27
26
28
pub fn init < R : Runtime > ( ) -> TauriPlugin < R > {
27
29
Builder :: < R > :: new ( "http" )
28
30
. setup ( |app, _| {
31
+ #[ cfg( feature = "cookies" ) ]
32
+ let ( cookies_jar_path, cookies_jar) = {
33
+ use reqwest_cookie_store:: * ;
34
+ use std:: fs:: File ;
35
+ use std:: io:: BufReader ;
36
+ use std:: sync:: Arc ;
37
+
38
+ let cache_dir = app. path ( ) . app_cache_dir ( ) ?;
39
+ std:: fs:: create_dir_all ( & cache_dir) ?;
40
+
41
+ let path = cache_dir. join ( "Cookies" ) ;
42
+ let file = File :: options ( )
43
+ . create ( true )
44
+ . write ( true )
45
+ . read ( true )
46
+ . open ( & path) ?;
47
+
48
+ let reader = BufReader :: new ( file) ;
49
+ let store = CookieStore :: load_json ( reader) . map_err ( |e| e. to_string ( ) ) ?;
50
+
51
+ ( path, Arc :: new ( CookieStoreMutex :: new ( store) ) )
52
+ } ;
53
+
29
54
let state = Http {
30
55
#[ cfg( feature = "cookies" ) ]
31
- cookies_jar : std:: sync:: Arc :: new ( reqwest:: cookie:: Jar :: default ( ) ) ,
56
+ cookies_jar_path,
57
+ #[ cfg( feature = "cookies" ) ]
58
+ cookies_jar,
32
59
} ;
33
60
34
61
app. manage ( state) ;
35
62
36
63
Ok ( ( ) )
37
64
} )
65
+ . on_event ( |app, event| {
66
+ #[ cfg( feature = "cookies" ) ]
67
+ if let tauri:: RunEvent :: Exit = event {
68
+ use std:: fs:: File ;
69
+ use std:: io:: BufWriter ;
70
+
71
+ let state = app. state :: < Http > ( ) ;
72
+
73
+ if let Ok ( file) = File :: create ( & state. cookies_jar_path ) {
74
+ let store = state. cookies_jar . lock ( ) . expect ( "poisoned cookie jar mutex" ) ;
75
+ let mut writer = BufWriter :: new ( file) ;
76
+ let _ = store. save_json ( & mut writer) ;
77
+ }
78
+ }
79
+ } )
38
80
. invoke_handler ( tauri:: generate_handler![
39
81
commands:: fetch,
40
82
commands:: fetch_cancel,
0 commit comments