fix(download): 文件版加固(路径白名单/tokio 写盘/缩略图预热/临时清理/开关保留)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s

This commit is contained in:
qiqi committed 2026-12-04 12:32:46 +08:00
1 parent df3e99b0ee
commit 9f8fb273d1
11 files changed
+84 -16

No files matched your search

+1
View File
@@ -2034,6 +2034,7 @@ dependencies = [
"tauri-plugin-notification",
"tauri-plugin-single-instance",
"tauri-plugin-updater",
"tokio",
"url",
"windows-sys 0.52.0",
]
+2
View File
@@ -27,6 +27,8 @@ log = "0.4"
# 画布内嵌网页已下线(v3.25.27),但 get_window 仍要留着 —— save_geom/托盘/单实例都靠它,
# 且它比 get_webview_window 更稳(后者一旦窗口里有非同名 webview 就返回 None,见 v3.25.26)。
tauri = { version = "2.12.5", features = ["tray-icon", "image-png", "unstable"] }
# 2026-12-04:文件版下载用 tokio::fs 异步写盘(避免阻塞 async 运行时)
tokio = { version = "1", features = ["fs", "io-util"] }
url = "2"
tauri-plugin-log = "2"
tauri-plugin-dialog = "2"
+23 -8
View File
@@ -299,8 +299,8 @@ fn do_thumbs(src: &str, jobs: &[ThumbJob]) -> Result<serde_json::Value, String>
// 2026-12-04(文件版下载,默认关闭):流式写文件,避免整包 base64 过 IPC。
// 目标路径由 JS 提供(资料目录下的 cache/tmp/),这里只负责抓字节 + 落盘;失败会删半成品。
#[tauri::command]
async fn fetch_image_to(url: String, dst: String) -> Result<serde_json::Value, String> {
use std::io::Write;
async fn fetch_image_to(url: String, dst: String, base: Option<String>) -> Result<serde_json::Value, String> {
use tokio::io::AsyncWriteExt;
let client = tauri_plugin_http::reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(15))
.timeout(std::time::Duration::from_secs(300))
@@ -319,27 +319,42 @@ async fn fetch_image_to(url: String, dst: String) -> Result<serde_json::Value, S
.unwrap_or("application/octet-stream")
.to_string();
let path = std::path::PathBuf::from(&dst);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
// 2026-12-04:dst 由 JS 传入,命令又会被插件调用 —— 限制在资料目录(base)内,并拒绝 ..。
if dst.contains("..") {
return Ok(serde_json::json!({ "ok": false, "error": "bad-path" }));
}
let mut file = std::fs::File::create(&path).map_err(|e| e.to_string())?;
if let Some(b) = &base {
let base_path = std::path::PathBuf::from(b);
if !path.starts_with(&base_path) {
return Ok(serde_json::json!({ "ok": false, "error": "path-outside-base" }));
}
}
if let Some(parent) = path.parent() {
tokio::fs::create_dir_all(parent).await.map_err(|e| e.to_string())?;
}
// 2026-12-04:改用 tokio::fs 异步写,避免 std::fs 阻塞 async 运行时。
let mut file = tokio::fs::File::create(&path).await.map_err(|e| e.to_string())?;
let mut bytes: u64 = 0;
loop {
match resp.chunk().await {
Ok(Some(chunk)) => {
if let Err(e) = file.write_all(&chunk) {
let _ = std::fs::remove_file(&path);
if let Err(e) = file.write_all(&chunk).await {
let _ = tokio::fs::remove_file(&path).await;
return Ok(serde_json::json!({ "ok": false, "error": e.to_string() }));
}
bytes += chunk.len() as u64;
}
Ok(None) => break,
Err(e) => {
let _ = std::fs::remove_file(&path);
let _ = tokio::fs::remove_file(&path).await;
return Ok(serde_json::json!({ "ok": false, "error": e.to_string() }));
}
}
}
if let Err(e) = file.flush().await {
let _ = tokio::fs::remove_file(&path).await;
return Ok(serde_json::json!({ "ok": false, "error": e.to_string() }));
}
Ok(serde_json::json!({ "ok": true, "bytes": bytes, "mime": mime, "path": dst }))
}