11use std:: fs;
2- use std:: io:: Read ;
32use std:: path:: { Path , PathBuf } ;
43use std:: sync:: Arc ;
54use std:: sync:: atomic:: { AtomicBool , Ordering } ;
65use std:: time:: Duration ;
76
87use futures:: StreamExt ;
98use reqwest:: Client ;
10- use sha2:: { Digest , Sha256 } ;
119use tauri:: { AppHandle , Emitter } ;
1210use tokio:: sync:: Mutex ;
1311
@@ -22,7 +20,6 @@ use crate::services::updater::UpdateService;
2220use super :: types:: { PreparedRuntimeUpdate , RuntimeLatestRelease } ;
2321
2422const POLL_INTERVAL : Duration = Duration :: from_secs ( 180 ) ;
25- const HEAD_HASH_LIMIT_BYTES : usize = 10 * 1024 * 1024 ;
2623const READY_EVENT_NAME : & str = "app-update-ready" ;
2724
2825#[ cfg( target_os = "windows" ) ]
@@ -113,10 +110,12 @@ impl RuntimeUpdateService {
113110 return Ok ( ( ) ) ;
114111 }
115112
116- if let Some ( local_head_hash) =
117- calculate_head_hash_if_exists ( & crate :: app:: runtime:: runtime_executable_path ( ) ?) ?
118- {
119- if local_head_hash. eq_ignore_ascii_case ( & platform. head_sha256_10mb ) {
113+ let runtime_path = crate :: app:: runtime:: runtime_executable_path ( ) ?;
114+ if runtime_path. exists ( ) {
115+ let local_hash = calculate_file_hash ( & runtime_path) . map_err ( |error| {
116+ Error :: UpdateCheckFailed ( format ! ( "runtime 本地文件哈希计算失败: {}" , error) )
117+ } ) ?;
118+ if local_hash. eq_ignore_ascii_case ( & platform. sha256 ) {
120119 let mut state = self . state . lock ( ) . await ;
121120 state. prepared = None ;
122121 return Ok ( ( ) ) ;
@@ -181,7 +180,7 @@ impl RuntimeUpdateService {
181180 release : & RuntimeLatestRelease ,
182181 platform : & super :: types:: RuntimeLatestReleasePlatform ,
183182 ) -> Result < PreparedRuntimeUpdate > {
184- if platform. r2_url . trim ( ) . is_empty ( ) {
183+ if platform. url . trim ( ) . is_empty ( ) {
185184 return Err ( "runtime latest.json 未提供下载地址" . into ( ) ) ;
186185 }
187186
@@ -193,7 +192,7 @@ impl RuntimeUpdateService {
193192 let tasks_file = runtime_dir. join ( "runtime-update-tasks.json" ) ;
194193 let backup_path = runtime_dir. join ( format ! ( "simprint-runtime-{}.bak" , release. version) ) ;
195194
196- download_to_file ( & platform. r2_url , & artifact_path) . await ?;
195+ download_to_file ( & platform. url , & artifact_path) . await ?;
197196
198197 let actual_hash = calculate_file_hash ( & artifact_path) . map_err ( |error| {
199198 Error :: UpdateCheckFailed ( format ! ( "runtime 更新包校验失败: {}" , error) )
@@ -264,30 +263,6 @@ fn runtime_update_dir() -> Result<PathBuf> {
264263 Ok ( crate :: core:: paths:: PathManager :: get_updater_dir ( ) ?. join ( "runtime" ) )
265264}
266265
267- fn calculate_head_hash_if_exists ( file_path : & Path ) -> Result < Option < String > > {
268- if !file_path. exists ( ) {
269- return Ok ( None ) ;
270- }
271-
272- let mut file = fs:: File :: open ( file_path) ?;
273- let mut hasher = Sha256 :: new ( ) ;
274- let mut remaining = HEAD_HASH_LIMIT_BYTES ;
275- let mut buffer = [ 0u8 ; 8192 ] ;
276-
277- while remaining > 0 {
278- let to_read = remaining. min ( buffer. len ( ) ) ;
279- let bytes_read = file. read ( & mut buffer[ ..to_read] ) ?;
280- if bytes_read == 0 {
281- break ;
282- }
283-
284- hasher. update ( & buffer[ ..bytes_read] ) ;
285- remaining -= bytes_read;
286- }
287-
288- Ok ( Some ( format ! ( "{:x}" , hasher. finalize( ) ) ) )
289- }
290-
291266async fn download_to_file ( url : & str , target_path : & Path ) -> Result < ( ) > {
292267 let client = Client :: builder ( ) . timeout ( Duration :: from_secs ( 300 ) ) . build ( ) ?;
293268 let response = client. get ( url) . send ( ) . await ?;
0 commit comments