update
Some checks failed
Rust CI / Build Release (push) Has been cancelled
Rust CI / Cargo Check (push) Has been cancelled

This commit is contained in:
lychang
2026-07-19 15:04:22 +08:00
parent b915e69222
commit 82653c1a6c
40 changed files with 7778 additions and 1 deletions

29
src/routes/image.rs Normal file
View File

@@ -0,0 +1,29 @@
use crate::db::Database;
use crate::image_processor::ImageProcessor;
use actix_web::{HttpResponse, Responder, web};
use std::fs;
use std::sync::Arc;
use uuid::Uuid;
pub async fn get_image(
db: web::Data<Arc<Database>>,
ip: web::Data<Arc<ImageProcessor>>,
path: web::Path<String>,
) -> impl Responder {
let id = match Uuid::parse_str(&path.into_inner()) { Ok(id) => id, Err(_) => return HttpResponse::BadRequest().json(serde_json::json!({"error": "无效ID"})) };
match db.get_image(id).await {
Ok(Some(img)) => {
let fp = ip.config.images_dir.join(&img.filename);
if fp.exists() {
match fs::read(&fp) {
Ok(data) => HttpResponse::Ok().content_type(img.mime_type)
.insert_header(("Cache-Control", "public, max-age=31536000"))
.insert_header(("ETag", format!("\"{}\"", id))).body(data),
Err(_) => HttpResponse::InternalServerError().json(serde_json::json!({"error": "读取失败"})),
}
} else { HttpResponse::NotFound().json(serde_json::json!({"error": "文件不存在"})) }
}
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({"error": "图片不存在"})),
Err(_) => HttpResponse::InternalServerError().json(serde_json::json!({"error": "查询失败"})),
}
}