370 lines
13 KiB
Rust
370 lines
13 KiB
Rust
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>>,
|
||
query: web::Query<std::collections::HashMap<String, String>>,
|
||
) -> impl Responder {
|
||
let page = query.get("page").and_then(|s| s.parse::<usize>().ok());
|
||
let limit = query.get("limit").and_then(|s| s.parse::<usize>().ok());
|
||
|
||
match db.get_all_albums(page, limit).await {
|
||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||
Err(e) => {
|
||
eprintln!("获取相册列表失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "获取相册列表失败"
|
||
}))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 收藏功能API路由
|
||
|
||
// 获取用户的收藏列表
|
||
pub async fn get_favorites(
|
||
db: web::Data<Arc<Database>>,
|
||
) -> impl Responder {
|
||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||
let user_id = "default_user".to_string();
|
||
|
||
match db.get_user_favorites(&user_id).await {
|
||
Ok(favorites) => HttpResponse::Ok().json(favorites),
|
||
Err(e) => {
|
||
eprintln!("获取收藏列表失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "获取收藏列表失败"
|
||
}))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查图片是否被收藏
|
||
pub async fn check_favorite_status(
|
||
db: web::Data<Arc<Database>>,
|
||
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"
|
||
}));
|
||
}
|
||
};
|
||
|
||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||
let user_id = "default_user".to_string();
|
||
|
||
match db.get_favorite_status(&user_id, image_id).await {
|
||
Ok(status) => HttpResponse::Ok().json(status),
|
||
Err(e) => {
|
||
eprintln!("检查收藏状态失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "检查收藏状态失败"
|
||
}))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 添加收藏
|
||
pub async fn add_favorite(
|
||
db: web::Data<Arc<Database>>,
|
||
favorite_data: web::Json<CreateFavorite>,
|
||
) -> impl Responder {
|
||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||
let user_id = "default_user".to_string();
|
||
|
||
match db.add_favorite(&user_id, favorite_data.into_inner()).await {
|
||
Ok(favorite_id) => HttpResponse::Created().json(serde_json::json!({
|
||
"id": favorite_id,
|
||
"message": "收藏成功"
|
||
})),
|
||
Err(e) => {
|
||
eprintln!("添加收藏失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "添加收藏失败"
|
||
}))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 取消收藏
|
||
pub async fn remove_favorite(
|
||
db: web::Data<Arc<Database>>,
|
||
path: web::Path<String>,
|
||
) -> impl Responder {
|
||
let favorite_id = match Uuid::parse_str(&path.into_inner()) {
|
||
Ok(id) => id,
|
||
Err(_) => {
|
||
return HttpResponse::BadRequest().json(serde_json::json!({
|
||
"error": "无效的收藏ID"
|
||
}));
|
||
}
|
||
};
|
||
|
||
match db.remove_favorite(favorite_id).await {
|
||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||
"message": "取消收藏成功"
|
||
})),
|
||
Err(e) => {
|
||
eprintln!("取消收藏失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "取消收藏失败"
|
||
}))
|
||
}
|
||
}
|
||
}
|
||
|
||
// 通过图片ID取消收藏
|
||
pub async fn remove_favorite_by_image(
|
||
db: web::Data<Arc<Database>>,
|
||
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"
|
||
}));
|
||
}
|
||
};
|
||
|
||
// 这里使用固定用户ID,实际项目中应该从认证信息中获取
|
||
let user_id = "default_user".to_string();
|
||
|
||
match db.remove_favorite_by_image(&user_id, image_id).await {
|
||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||
"message": "取消收藏成功"
|
||
})),
|
||
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("");
|
||
let page = query.get("page").and_then(|s| s.parse::<usize>().ok());
|
||
let limit = query.get("limit").and_then(|s| s.parse::<usize>().ok());
|
||
|
||
if q.is_empty() {
|
||
return match db.get_all_albums(page, limit).await {
|
||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||
Err(e) => {
|
||
eprintln!("获取相册列表失败: {}", e);
|
||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||
"error": "获取相册列表失败"
|
||
}))
|
||
}
|
||
};
|
||
}
|
||
|
||
match db.search_albums(q, page, limit).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) => {
|
||
// 使用ImageProcessor生成缩略图
|
||
match image_processor.generate_thumbnail_from_memory(&data, &image.id.to_string()).await {
|
||
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);
|
||
}
|
||
});
|
||
|
||
// 返回缩略图数据
|
||
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": "读取图片失败"
|
||
}))
|
||
}
|
||
}
|
||
} 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": "获取图片信息失败"
|
||
}))
|
||
}
|
||
}
|
||
} |