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>, ip: web::Data>, path: web::Path, ) -> 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": "查询失败"})), } }