Skip to content

Commit 0f5a318

Browse files
committed
build: switch runtime manifest to url field
1 parent 3dc81e7 commit 0f5a318

3 files changed

Lines changed: 14 additions & 40 deletions

File tree

src-tauri/build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mod runtime_assets {
9292

9393
#[derive(Deserialize)]
9494
struct RuntimeLatestReleasePlatform {
95-
r2_url: String,
95+
url: String,
9696
sha256: String,
9797
}
9898

@@ -179,16 +179,16 @@ mod runtime_assets {
179179
)
180180
})?;
181181

182-
if platform.r2_url.trim().is_empty() {
183-
return Err("runtime latest.json r2_url is empty".into());
182+
if platform.url.trim().is_empty() {
183+
return Err("runtime latest.json url is empty".into());
184184
}
185185

186186
println!(
187187
"cargo:warning=Downloading simprint-runtime binary from {}",
188-
platform.r2_url
188+
platform.url
189189
);
190190

191-
let bytes = client.get(&platform.r2_url).send()?.error_for_status()?.bytes()?;
191+
let bytes = client.get(&platform.url).send()?.error_for_status()?.bytes()?;
192192

193193
let actual_sha256 = sha256_hex(&bytes);
194194
if !actual_sha256.eq_ignore_ascii_case(&platform.sha256) {

src-tauri/src/services/runtime_updater/service.rs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use std::fs;
2-
use std::io::Read;
32
use std::path::{Path, PathBuf};
43
use std::sync::Arc;
54
use std::sync::atomic::{AtomicBool, Ordering};
65
use std::time::Duration;
76

87
use futures::StreamExt;
98
use reqwest::Client;
10-
use sha2::{Digest, Sha256};
119
use tauri::{AppHandle, Emitter};
1210
use tokio::sync::Mutex;
1311

@@ -22,7 +20,6 @@ use crate::services::updater::UpdateService;
2220
use super::types::{PreparedRuntimeUpdate, RuntimeLatestRelease};
2321

2422
const POLL_INTERVAL: Duration = Duration::from_secs(180);
25-
const HEAD_HASH_LIMIT_BYTES: usize = 10 * 1024 * 1024;
2623
const 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-
291266
async 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?;

src-tauri/src/services/runtime_updater/types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ pub struct RuntimeLatestRelease {
1515

1616
#[derive(Debug, Clone, Deserialize)]
1717
pub struct RuntimeLatestReleasePlatform {
18-
pub r2_url: String,
18+
pub url: String,
1919
pub size: u64,
2020
pub sha256: String,
21-
pub head_sha256_10mb: String,
2221
}
2322

2423
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)