feat: Update database setup and download handling

- Set a fixed database password and ensure old data is overwritten during setup.
- Enhance the download functionality with improved error handling for missing files, providing user-friendly HTML responses.
- Add instructions for placing client builds in the downloads directory in the admin panel.
This commit is contained in:
2026-02-26 21:05:41 +08:00
parent a376a9e0f3
commit 89948f76b7
3 changed files with 39 additions and 11 deletions

View File

@@ -256,11 +256,31 @@ pub async fn list_builds() -> Json<Vec<ClientBuild>> {
Json(builds)
}
pub async fn download_file(Path(filename): Path<String>) -> Result<Response, StatusCode> {
pub async fn download_file(Path(filename): Path<String>) -> Result<Response, Response> {
let downloads_dir = std::env::var("DOWNLOADS_DIR").unwrap_or_else(|_| "./downloads".to_string());
let file_path = std::path::Path::new(&downloads_dir).join(&filename);
let file = File::open(&file_path).await.map_err(|_| StatusCode::NOT_FOUND)?;
let file = match File::open(&file_path).await {
Ok(f) => f,
Err(_) => {
let escaped = filename.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;");
let html = format!(r##"<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>文件不存在</title></head>
<body style="font-family:sans-serif;max-width:560px;margin:80px auto;padding:20px;">
<h1>文件不存在</h1>
<p>未找到 <strong>{}</strong>。</p>
<p>可能原因:该版本尚未构建或未上传到服务器。请联系管理员,或将构建好的客户端放入服务器的 <code>downloads</code> 目录后重试。</p>
<p><a href="/download">返回下载页</a></p>
</body>
</html>"##, escaped);
return Err(Response::builder()
.status(StatusCode::NOT_FOUND)
.header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.body(Body::from(html))
.unwrap());
}
};
let stream = ReaderStream::new(file);
let body = Body::from_stream(stream);