update code level
This commit is contained in:
@@ -6,8 +6,14 @@ 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 {
|
||||
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);
|
||||
@@ -196,9 +202,11 @@ pub async fn search_albums(
|
||||
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().await {
|
||||
return match db.get_all_albums(page, limit).await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("获取相册列表失败: {}", e);
|
||||
@@ -209,7 +217,7 @@ pub async fn search_albums(
|
||||
};
|
||||
}
|
||||
|
||||
match db.search_albums(q).await {
|
||||
match db.search_albums(q, page, limit).await {
|
||||
Ok(albums) => HttpResponse::Ok().json(albums),
|
||||
Err(e) => {
|
||||
eprintln!("搜索相册失败: {}", e);
|
||||
@@ -306,64 +314,33 @@ pub async fn get_thumbnail(
|
||||
// 生成缩略图并返回
|
||||
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)
|
||||
// 使用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);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("读取缩略图数据失败: {}", e);
|
||||
// 如果读取失败,返回原始图片
|
||||
HttpResponse::Ok().content_type(image.mime_type).body(data)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 返回缩略图数据
|
||||
HttpResponse::Ok()
|
||||
.content_type("image/jpeg")
|
||||
.body(thumbnail_data)
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("加载图片失败: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "加载图片失败"
|
||||
}))
|
||||
eprintln!("生成缩略图失败: {}", e);
|
||||
// 如果生成缩略图失败,返回原始图片
|
||||
HttpResponse::Ok().content_type(image.mime_type).body(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user