fix(download): 文件版加固(路径白名单/tokio 写盘/缩略图预热/临时清理/开关保留)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
This commit is contained in:
1 parent
df3e99b0ee
commit
9f8fb273d1
11 files changed
+84
-16
No files matched your search
+23
-8
@@ -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 }))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user