init
This commit is contained in:
266
src/routes.rs
Normal file
266
src/routes.rs
Normal file
@@ -0,0 +1,266 @@
|
||||
use crate::database::Database;
|
||||
use crate::image_processor::ImageProcessor;
|
||||
use crate::models::*;
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_albums(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
match db.get_all_albums().await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册列表失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取相册列表失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_original_image(
|
||||
db: web::Data<Arc<Database>>,
|
||||
image_processor: web::Data<Arc<ImageProcessor>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let image_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(image_id).await {
|
||||
Ok(Some(image)) => {
|
||||
let original_path = image_processor.config.images_dir.join(&image.filename);
|
||||
|
||||
if original_path.exists() {
|
||||
match fs::read(&original_path) {
|
||||
Ok(data) => HttpResponse::Ok().content_type(image.mime_type).body(data),
|
||||
Err(e) => {
|
||||
eprintln!("读取原始图片失败: {}", e);
|
||||
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(e) => {
|
||||
eprintln!("获取图片信息失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取图片信息失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn search_albums(
|
||||
db: web::Data<Arc<Database>>,
|
||||
query: web::Query<std::collections::HashMap<String, String>>,
|
||||
) -> impl Responder {
|
||||
let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
|
||||
|
||||
if q.is_empty() {
|
||||
return match db.get_all_albums().await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册列表失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取相册列表失败"
|
||||
}))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
match db.search_albums(q).await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("搜索相册失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "搜索失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_album_detail(
|
||||
db: web::Data<Arc<Database>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let album_id = match Uuid::parse_str(&path.into_inner()) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "无效的相册ID"
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
match db.get_album_with_images(album_id).await {
|
||||
Ok(Some(album)) => HttpResponse::Ok().json(album),
|
||||
Ok(None) => HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "相册不存在"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册详情失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取相册详情失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_album(
|
||||
db: web::Data<Arc<Database>>,
|
||||
album_data: web::Json<CreateAlbum>,
|
||||
) -> impl Responder {
|
||||
match db.create_album(album_data.into_inner()).await {
|
||||
Ok(album_id) => HttpResponse::Created().json(serde_json::json!({
|
||||
"id": album_id,
|
||||
"message": "相册创建成功"
|
||||
})),
|
||||
Err(e) => {
|
||||
eprintln!("创建相册失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "创建相册失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_thumbnail(
|
||||
db: web::Data<Arc<Database>>,
|
||||
image_processor: web::Data<Arc<ImageProcessor>>,
|
||||
path: web::Path<String>,
|
||||
) -> impl Responder {
|
||||
let image_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(image_id).await {
|
||||
Ok(Some(image)) => {
|
||||
// 优先返回缩略图,如果不存在则生成
|
||||
let thumbnail_path = image_processor
|
||||
.config
|
||||
.thumbnails_dir
|
||||
.join(image.id.to_string());
|
||||
let original_path = image_processor.config.images_dir.join(&image.filename);
|
||||
|
||||
if thumbnail_path.exists() {
|
||||
// 返回缩略图
|
||||
match fs::read(&thumbnail_path) {
|
||||
Ok(data) => {
|
||||
let mime_type = image_processor.detect_mime_type("jpg"); // 缩略图总是JPEG格式
|
||||
HttpResponse::Ok().content_type(mime_type).body(data)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("读取缩略图失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "读取缩略图失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
} else if original_path.exists() {
|
||||
// 生成缩略图并返回
|
||||
match fs::read(&original_path) {
|
||||
Ok(data) => {
|
||||
match image::load_from_memory(&data) {
|
||||
Ok(img) => {
|
||||
// 在内存中生成缩略图,避免重复文件读写
|
||||
let thumbnail = img.thumbnail(800, 600);
|
||||
|
||||
// 使用临时文件来生成缩略图数据
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let temp_file =
|
||||
temp_dir.join(format!("temp_thumb_{}", Uuid::new_v4()));
|
||||
|
||||
if let Err(e) =
|
||||
thumbnail.save_with_format(&temp_file, image::ImageFormat::Jpeg)
|
||||
{
|
||||
eprintln!("生成缩略图失败: {}", e);
|
||||
// 如果生成缩略图失败,返回原始图片
|
||||
return HttpResponse::Ok()
|
||||
.content_type(image.mime_type)
|
||||
.body(data);
|
||||
}
|
||||
|
||||
// 读取缩略图数据
|
||||
match fs::read(&temp_file) {
|
||||
Ok(thumbnail_data) => {
|
||||
// 异步保存缩略图到文件(不阻塞响应)
|
||||
let config_clone = image_processor.config.clone();
|
||||
let filename_clone = image.id.clone();
|
||||
let thumbnail_data_clone = thumbnail_data.clone();
|
||||
tokio::spawn(async move {
|
||||
let thumbnail_path = config_clone
|
||||
.thumbnails_dir
|
||||
.join(filename_clone.to_string());
|
||||
if let Err(e) =
|
||||
fs::write(&thumbnail_path, &thumbnail_data_clone)
|
||||
{
|
||||
eprintln!("保存缩略图文件失败: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
// 清理临时文件
|
||||
let _ = fs::remove_file(&temp_file);
|
||||
|
||||
// 返回缩略图数据
|
||||
HttpResponse::Ok()
|
||||
.content_type("image/jpeg")
|
||||
.body(thumbnail_data)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("读取缩略图数据失败: {}", e);
|
||||
// 如果读取失败,返回原始图片
|
||||
HttpResponse::Ok().content_type(image.mime_type).body(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("加载图片失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "加载图片失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("读取原始图片失败: {}", e);
|
||||
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(e) => {
|
||||
eprintln!("获取图片信息失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "获取图片信息失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user