init
This commit is contained in:
172
src/database.rs
172
src/database.rs
@@ -404,4 +404,176 @@ impl Database {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
// 收藏功能数据库操作
|
||||
|
||||
// 获取用户的收藏列表
|
||||
pub async fn get_user_favorites(&self, user_id: &str) -> Result<Vec<FavoriteWithImage>, sqlx::Error> {
|
||||
let rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
f.id as favorite_id,
|
||||
f.user_id,
|
||||
f.image_id,
|
||||
f.album_id,
|
||||
f.created_at as favorite_created_at,
|
||||
i.id as image_id,
|
||||
i.album_id,
|
||||
i.filename,
|
||||
i.original_url,
|
||||
i.title,
|
||||
i.description,
|
||||
i.file_size,
|
||||
i.width,
|
||||
i.height,
|
||||
i.mime_type,
|
||||
i.created_at as image_created_at,
|
||||
a.title as album_title
|
||||
FROM favorites f
|
||||
JOIN images i ON f.image_id = i.id
|
||||
JOIN albums a ON f.album_id = a.id
|
||||
WHERE f.user_id = ?
|
||||
ORDER BY f.created_at DESC
|
||||
"#
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
|
||||
let mut favorites = Vec::new();
|
||||
for row in rows {
|
||||
let favorite_id_str = row.get::<String, _>("favorite_id");
|
||||
let favorite_id = match Uuid::parse_str(&favorite_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的收藏UUID格式: {} - 错误: {}", favorite_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let image_id_str = row.get::<String, _>("image_id");
|
||||
let image_id = match Uuid::parse_str(&image_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的图片UUID格式: {} - 错误: {}", image_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let album_id_str = row.get::<String, _>("album_id");
|
||||
let album_id = match Uuid::parse_str(&album_id_str) {
|
||||
Ok(uuid) => uuid,
|
||||
Err(e) => {
|
||||
eprintln!("无效的相册UUID格式: {} - 错误: {}", album_id_str, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let favorite = Favorite {
|
||||
id: favorite_id,
|
||||
user_id: row.get("user_id"),
|
||||
image_id,
|
||||
album_id,
|
||||
created_at: row.get::<String, _>("favorite_created_at"),
|
||||
};
|
||||
|
||||
let image = Image {
|
||||
id: image_id,
|
||||
album_id,
|
||||
filename: row.get("filename"),
|
||||
original_url: row.get("original_url"),
|
||||
title: row.get("title"),
|
||||
description: row.get("description"),
|
||||
file_size: row.get("file_size"),
|
||||
width: row.get("width"),
|
||||
height: row.get("height"),
|
||||
mime_type: row.get("mime_type"),
|
||||
created_at: row.get::<String, _>("image_created_at"),
|
||||
};
|
||||
|
||||
favorites.push(FavoriteWithImage {
|
||||
favorite,
|
||||
image,
|
||||
album_title: row.get("album_title"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(favorites)
|
||||
}
|
||||
|
||||
// 检查图片是否被用户收藏
|
||||
pub async fn get_favorite_status(&self, user_id: &str, image_id: Uuid) -> Result<FavoriteStatus, sqlx::Error> {
|
||||
let row = sqlx::query(
|
||||
"SELECT id FROM favorites WHERE user_id = ? AND image_id = ?"
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(image_id.to_string())
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
|
||||
if let Some(row) = row {
|
||||
let favorite_id_str = row.get::<String, _>("id");
|
||||
let favorite_id = match Uuid::parse_str(&favorite_id_str) {
|
||||
Ok(uuid) => Some(uuid),
|
||||
Err(e) => {
|
||||
eprintln!("无效的收藏UUID格式: {} - 错误: {}", favorite_id_str, e);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
Ok(FavoriteStatus {
|
||||
is_favorited: true,
|
||||
favorite_id,
|
||||
})
|
||||
} else {
|
||||
Ok(FavoriteStatus {
|
||||
is_favorited: false,
|
||||
favorite_id: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
pub async fn add_favorite(&self, user_id: &str, favorite_data: CreateFavorite) -> Result<Uuid, sqlx::Error> {
|
||||
let favorite_id = Uuid::new_v4();
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO favorites (id, user_id, image_id, album_id, created_at) VALUES (?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(favorite_id.to_string())
|
||||
.bind(user_id)
|
||||
.bind(favorite_data.image_id.to_string())
|
||||
.bind(favorite_data.album_id.to_string())
|
||||
.bind(&now)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(favorite_id)
|
||||
}
|
||||
|
||||
// 移除收藏(通过收藏ID)
|
||||
pub async fn remove_favorite(&self, favorite_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
"DELETE FROM favorites WHERE id = ?"
|
||||
)
|
||||
.bind(favorite_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 移除收藏(通过用户ID和图片ID)
|
||||
pub async fn remove_favorite_by_image(&self, user_id: &str, image_id: Uuid) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
"DELETE FROM favorites WHERE user_id = ? AND image_id = ?"
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(image_id.to_string())
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
443
src/import.rs
443
src/import.rs
@@ -1,7 +1,9 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use image::GenericImageView;
|
||||
use serde::Deserialize;
|
||||
use tokio::task;
|
||||
use crate::config::Config;
|
||||
use crate::database::Database;
|
||||
use crate::models::*;
|
||||
@@ -12,7 +14,7 @@ struct AlbumIndex {
|
||||
images: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn import_existing_data(config: &Config, db: &Database) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub async fn import_existing_data(config: &Config, db: Arc<Database>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("开始扫描图片目录并导入新相册...");
|
||||
|
||||
let albums_dir = Path::new(&config.storage.images_dir);
|
||||
@@ -30,13 +32,12 @@ pub async fn import_existing_data(config: &Config, db: &Database) -> Result<(),
|
||||
|
||||
println!("数据库中现有相册数量: {}", existing_albums.len());
|
||||
|
||||
let entries = fs::read_dir(albums_dir)?;
|
||||
let mut imported_count = 0;
|
||||
// 第一阶段:快速扫描所有相册文件夹
|
||||
let entries: Vec<_> = fs::read_dir(albums_dir)?.collect::<Result<Vec<_>, _>>()?;
|
||||
let mut album_folders = Vec::new();
|
||||
|
||||
for entry in entries {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_dir() {
|
||||
if let Some(folder_name) = path.file_name().and_then(|s| s.to_str()) {
|
||||
// 检查是否已存在同名相册
|
||||
@@ -44,198 +45,260 @@ pub async fn import_existing_data(config: &Config, db: &Database) -> Result<(),
|
||||
println!("跳过已存在的相册: {}", folder_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
println!("发现新相册: {}", folder_name);
|
||||
|
||||
// 读取index.json文件
|
||||
let index_file_path = path.join("index.json");
|
||||
let album_title = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
Ok(content) => {
|
||||
match serde_json::from_str::<AlbumIndex>(&content) {
|
||||
Ok(index) => {
|
||||
println!(" 从index.json读取相册标题: {}", index.title);
|
||||
index.title
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" 解析index.json失败: {}, 使用文件夹名作为标题", e);
|
||||
folder_name.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" 读取index.json失败: {}, 使用文件夹名作为标题", e);
|
||||
folder_name.to_string()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!(" index.json不存在,使用文件夹名作为标题");
|
||||
folder_name.to_string()
|
||||
};
|
||||
|
||||
// 创建新相册
|
||||
let new_album = CreateAlbum {
|
||||
title: album_title.clone(),
|
||||
document: folder_name.to_string(),
|
||||
description: Some(format!("从文件夹 {} 自动导入的相册", folder_name)),
|
||||
};
|
||||
|
||||
let album_id = db.create_album(new_album).await?;
|
||||
println!(" 创建相册: {} (ID: {})", album_title, album_id);
|
||||
|
||||
// 读取图片URL列表(如果有index.json)
|
||||
let image_urls = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
Ok(content) => {
|
||||
match serde_json::from_str::<AlbumIndex>(&content) {
|
||||
Ok(index) => {
|
||||
println!(" 从index.json读取到 {} 个图片URL", index.images.len());
|
||||
Some(index.images)
|
||||
}
|
||||
Err(_) => None
|
||||
}
|
||||
}
|
||||
Err(_) => None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 扫描文件夹中的图片文件
|
||||
let image_entries = fs::read_dir(&path)?;
|
||||
let mut image_files = Vec::new();
|
||||
|
||||
for image_entry in image_entries {
|
||||
let image_entry = image_entry?;
|
||||
let image_path = image_entry.path();
|
||||
|
||||
if image_path.is_file() {
|
||||
if let Some(extension) = image_path.extension().and_then(|s| s.to_str()) {
|
||||
let valid_extensions = ["jpg", "jpeg", "png", "gif", "webp", "bmp"];
|
||||
|
||||
if valid_extensions.contains(&extension.to_lowercase().as_str()) {
|
||||
let filename = image_path.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("unknown.jpg")
|
||||
.to_string();
|
||||
|
||||
image_files.push((filename, image_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按数字顺序排序文件名
|
||||
image_files.sort_by(|a, b| {
|
||||
// 提取文件名中的数字部分进行排序
|
||||
let num_a = a.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
let num_b = b.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
num_a.cmp(&num_b)
|
||||
});
|
||||
|
||||
let mut image_count = 0;
|
||||
let mut images_to_insert = Vec::new();
|
||||
|
||||
// 收集所有图片信息
|
||||
for (filename, image_path) in image_files {
|
||||
image_count += 1;
|
||||
|
||||
// 获取对应的URL(如果有的话)
|
||||
let original_url = if let Some(ref urls) = image_urls {
|
||||
if urls.len() >= image_count {
|
||||
Some(urls[image_count - 1].clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 生成图片标题
|
||||
let image_title = if let Some(ref urls) = image_urls {
|
||||
if urls.len() >= image_count {
|
||||
Some(format!("图片 {}", image_count))
|
||||
} else {
|
||||
Some(format!("图片 {}", image_count))
|
||||
}
|
||||
} else {
|
||||
Some(format!("图片 {}", image_count))
|
||||
};
|
||||
|
||||
// 获取图片的实际信息
|
||||
let (file_size, width, height) = match get_image_info(&image_path) {
|
||||
Ok(info) => info,
|
||||
Err(e) => {
|
||||
eprintln!("获取图片信息失败: {} - {}", image_path.display(), e);
|
||||
(0, 0, 0)
|
||||
}
|
||||
};
|
||||
|
||||
let image = CreateImage {
|
||||
album_id,
|
||||
filename: format!("{}/{}", folder_name, filename).clone(),
|
||||
original_url: original_url.clone(),
|
||||
title: image_title,
|
||||
description: Some(format!("从文件夹 {} 导入的图片", folder_name)),
|
||||
file_size,
|
||||
width,
|
||||
height,
|
||||
mime_type: match image_path.extension().and_then(|s| s.to_str()).unwrap_or("jpg").to_lowercase().as_str() {
|
||||
"jpg" | "jpeg" => "image/jpeg".to_string(),
|
||||
"png" => "image/png".to_string(),
|
||||
"gif" => "image/gif".to_string(),
|
||||
"webp" => "image/webp".to_string(),
|
||||
"bmp" => "image/bmp".to_string(),
|
||||
_ => "image/jpeg".to_string(),
|
||||
},
|
||||
};
|
||||
|
||||
images_to_insert.push(image.clone());
|
||||
}
|
||||
|
||||
// 批量插入图片
|
||||
if !images_to_insert.is_empty() {
|
||||
println!(" 批量插入 {} 张图片...", images_to_insert.len());
|
||||
match db.add_images(images_to_insert.clone()).await {
|
||||
Ok(ids) => {
|
||||
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" 批量插入失败: {}", e);
|
||||
// 如果批量插入失败,尝试逐条插入
|
||||
println!(" 尝试逐条插入图片...");
|
||||
let mut success_count = 0;
|
||||
for image in images_to_insert {
|
||||
match db.add_image(image).await {
|
||||
Ok(_) => success_count += 1,
|
||||
Err(e) => eprintln!(" 单条插入失败: {}", e),
|
||||
}
|
||||
}
|
||||
println!(" 逐条插入完成,成功 {} 张", success_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!(" 相册 {} 导入完成,共导入 {} 张图片", folder_name, image_count);
|
||||
imported_count += 1;
|
||||
album_folders.push((folder_name.to_string(), path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("导入完成!共导入 {} 个新相册", imported_count);
|
||||
println!("发现 {} 个新相册需要导入", album_folders.len());
|
||||
|
||||
if album_folders.is_empty() {
|
||||
println!("没有新相册需要导入");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 第二阶段:并行处理相册导入
|
||||
let mut tasks = Vec::new();
|
||||
|
||||
// 限制并发数量,避免内存爆炸
|
||||
let max_concurrent = std::thread::available_parallelism()?.get().min(4);
|
||||
let semaphore = Arc::new(tokio::sync::Semaphore::new(max_concurrent));
|
||||
|
||||
for (folder_name, path) in album_folders {
|
||||
let db_clone = Arc::clone(&db);
|
||||
let semaphore_clone = Arc::clone(&semaphore);
|
||||
let folder_name_clone = folder_name.clone();
|
||||
let path_clone = path.clone();
|
||||
|
||||
let task = task::spawn(async move {
|
||||
let _permit = semaphore_clone.acquire().await.unwrap();
|
||||
match import_single_album(&db_clone, &folder_name_clone, &path_clone).await {
|
||||
Ok(count) => {
|
||||
println!("✅ 相册 {} 导入完成,共导入 {} 张图片", folder_name_clone, count);
|
||||
count
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("❌ 相册 {} 导入失败: {}", folder_name_clone, e);
|
||||
0
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tasks.push(task);
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
let mut total_imported = 0;
|
||||
let mut successful_albums = 0;
|
||||
|
||||
for task in tasks {
|
||||
match task.await {
|
||||
Ok(count) => {
|
||||
if count > 0 {
|
||||
successful_albums += 1;
|
||||
total_imported += count;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("任务执行失败: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("🎉 导入完成!成功导入 {} 个相册,共 {} 张图片", successful_albums, total_imported);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 导入单个相册的辅助函数
|
||||
async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
println!("📁 开始导入相册: {}", folder_name);
|
||||
|
||||
// 读取index.json文件
|
||||
let index_file_path = path.join("index.json");
|
||||
let album_title = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
Ok(content) => {
|
||||
match serde_json::from_str::<AlbumIndex>(&content) {
|
||||
Ok(index) => {
|
||||
println!(" 从index.json读取相册标题: {}", index.title);
|
||||
index.title
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" 解析index.json失败: {}, 使用文件夹名作为标题", e);
|
||||
folder_name.to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" 读取index.json失败: {}, 使用文件夹名作为标题", e);
|
||||
folder_name.to_string()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!(" index.json不存在,使用文件夹名作为标题");
|
||||
folder_name.to_string()
|
||||
};
|
||||
|
||||
// 创建新相册
|
||||
let new_album = CreateAlbum {
|
||||
title: album_title.clone(),
|
||||
document: folder_name.to_string(),
|
||||
description: Some(format!("从文件夹 {} 自动导入的相册", folder_name)),
|
||||
};
|
||||
|
||||
let album_id = db.create_album(new_album).await?;
|
||||
println!(" 创建相册: {} (ID: {})", album_title, album_id);
|
||||
|
||||
// 读取图片URL列表(如果有index.json)
|
||||
let image_urls = if index_file_path.exists() {
|
||||
match fs::read_to_string(&index_file_path) {
|
||||
Ok(content) => {
|
||||
match serde_json::from_str::<AlbumIndex>(&content) {
|
||||
Ok(index) => {
|
||||
println!(" 从index.json读取到 {} 个图片URL", index.images.len());
|
||||
Some(index.images)
|
||||
}
|
||||
Err(_) => None
|
||||
}
|
||||
}
|
||||
Err(_) => None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// 快速扫描图片文件
|
||||
let image_files = scan_image_files(path, folder_name)?;
|
||||
|
||||
if image_files.is_empty() {
|
||||
println!(" 相册 {} 中没有找到图片文件", folder_name);
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
println!(" 发现 {} 张图片,开始处理...", image_files.len());
|
||||
|
||||
// 并行处理图片信息获取
|
||||
let mut image_tasks = Vec::new();
|
||||
|
||||
for (index, (filename, image_path)) in image_files.iter().enumerate() {
|
||||
let image_url = image_urls.as_ref().and_then(|urls| {
|
||||
if urls.len() > index {
|
||||
Some(urls[index].clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
let filename_clone = filename.clone();
|
||||
let image_path_clone = image_path.clone();
|
||||
let album_id_clone = album_id;
|
||||
let folder_name_clone = folder_name.to_string();
|
||||
|
||||
let task = task::spawn_blocking(move || {
|
||||
// 在阻塞线程中处理图片信息
|
||||
let (file_size, width, height) = match get_image_info(&image_path_clone) {
|
||||
Ok(info) => info,
|
||||
Err(e) => {
|
||||
eprintln!("获取图片信息失败: {} - {}", image_path_clone.display(), e);
|
||||
(0, 0, 0)
|
||||
}
|
||||
};
|
||||
|
||||
CreateImage {
|
||||
album_id: album_id_clone,
|
||||
filename: format!("{}/{}", folder_name_clone, filename_clone),
|
||||
original_url: image_url,
|
||||
title: Some(format!("图片 {}", index + 1)),
|
||||
description: Some(format!("从文件夹 {} 导入的图片", folder_name_clone)),
|
||||
file_size,
|
||||
width,
|
||||
height,
|
||||
mime_type: match image_path_clone.extension().and_then(|s| s.to_str()).unwrap_or("jpg").to_lowercase().as_str() {
|
||||
"jpg" | "jpeg" => "image/jpeg".to_string(),
|
||||
"png" => "image/png".to_string(),
|
||||
"gif" => "image/gif".to_string(),
|
||||
"webp" => "image/webp".to_string(),
|
||||
"bmp" => "image/bmp".to_string(),
|
||||
_ => "image/jpeg".to_string(),
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
image_tasks.push(task);
|
||||
}
|
||||
|
||||
// 收集所有图片信息
|
||||
let mut images_to_insert = Vec::new();
|
||||
for task in image_tasks {
|
||||
match task.await {
|
||||
Ok(image) => images_to_insert.push(image),
|
||||
Err(e) => eprintln!("图片处理任务失败: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
// 批量插入图片
|
||||
if !images_to_insert.is_empty() {
|
||||
println!(" 批量插入 {} 张图片...", images_to_insert.len());
|
||||
match db.add_images(images_to_insert).await {
|
||||
Ok(ids) => {
|
||||
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
|
||||
Ok(ids.len())
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(" 批量插入失败: {}", e);
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
/// 快速扫描图片文件
|
||||
fn scan_image_files(path: &Path, _folder_name: &str) -> Result<Vec<(String, std::path::PathBuf)>, Box<dyn std::error::Error>> {
|
||||
let mut image_files = Vec::new();
|
||||
|
||||
for entry in fs::read_dir(path)? {
|
||||
let entry = entry?;
|
||||
let image_path = entry.path();
|
||||
|
||||
if image_path.is_file() {
|
||||
if let Some(extension) = image_path.extension().and_then(|s| s.to_str()) {
|
||||
let valid_extensions = ["jpg", "jpeg", "png", "gif", "webp", "bmp"];
|
||||
|
||||
if valid_extensions.contains(&extension.to_lowercase().as_str()) {
|
||||
let filename = image_path.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("unknown.jpg")
|
||||
.to_string();
|
||||
|
||||
image_files.push((filename, image_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按数字顺序排序文件名
|
||||
image_files.sort_by(|a, b| {
|
||||
// 提取文件名中的数字部分进行排序
|
||||
let num_a = a.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
let num_b = b.0.chars()
|
||||
.take_while(|c| c.is_ascii_digit())
|
||||
.collect::<String>()
|
||||
.parse::<u32>()
|
||||
.unwrap_or(0);
|
||||
|
||||
num_a.cmp(&num_b)
|
||||
});
|
||||
|
||||
Ok(image_files)
|
||||
}
|
||||
|
||||
/// 获取图片的实际信息(文件大小、宽度、高度)
|
||||
fn get_image_info(image_path: &Path) -> Result<(i64, i32, i32), Box<dyn std::error::Error>> {
|
||||
// 获取文件大小
|
||||
|
||||
86
src/main.rs
86
src/main.rs
@@ -1,14 +1,14 @@
|
||||
mod config;
|
||||
mod models;
|
||||
mod database;
|
||||
mod image_processor;
|
||||
mod routes;
|
||||
mod import;
|
||||
mod models;
|
||||
mod routes;
|
||||
|
||||
use actix_files as afs;
|
||||
use actix_web::{web, App, HttpServer, HttpResponse};
|
||||
use std::sync::Arc;
|
||||
use actix_web::{App, HttpResponse, HttpServer, web};
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
@@ -20,10 +20,10 @@ async fn main() -> std::io::Result<()> {
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
println!("启动图片展示服务...");
|
||||
println!("服务地址: http://{}", config.server_address());
|
||||
|
||||
|
||||
// 初始化数据库
|
||||
let db = match database::Database::new(&config).await {
|
||||
Ok(db) => {
|
||||
@@ -35,21 +35,21 @@ async fn main() -> std::io::Result<()> {
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// 在后台线程中自动导入新相册(不阻塞HTTP服务启动)
|
||||
let config_clone = config.clone();
|
||||
let db_clone = Arc::clone(&db);
|
||||
tokio::spawn(async move {
|
||||
println!("[后台任务] 开始扫描图片目录并导入新相册...");
|
||||
match import::import_existing_data(&config_clone, &db_clone).await {
|
||||
match import::import_existing_data(&config_clone, db_clone).await {
|
||||
Ok(_) => println!("[后台任务] 相册导入完成"),
|
||||
Err(e) => eprintln!("[后台任务] 相册导入失败: {}", e),
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 初始化图片处理器
|
||||
let image_processor = Arc::new(image_processor::ImageProcessor::new(config.storage.clone()));
|
||||
|
||||
|
||||
println!("API接口:");
|
||||
println!(" - GET /api/albums - 获取所有相册");
|
||||
println!(" - GET /api/search?q=关键词 - 搜索相册");
|
||||
@@ -57,16 +57,21 @@ async fn main() -> std::io::Result<()> {
|
||||
println!(" - POST /api/albums - 创建新相册");
|
||||
println!(" - GET /api/thumbnail/:id - 获取缩略图");
|
||||
println!(" - GET /api/original/:id - 获取原始图片");
|
||||
println!(" - GET /api/favorites - 获取收藏列表");
|
||||
println!(" - GET /api/favorites/status/:id - 检查收藏状态");
|
||||
println!(" - POST /api/favorites - 添加收藏");
|
||||
println!(" - DELETE /api/favorites/:id - 取消收藏");
|
||||
println!(" - DELETE /api/favorites/image/:id - 通过图片ID取消收藏");
|
||||
println!("按 Ctrl+C 停止服务");
|
||||
|
||||
|
||||
// 创建数据目录
|
||||
std::fs::create_dir_all(&config.storage.images_dir).unwrap_or(());
|
||||
std::fs::create_dir_all(&config.storage.thumbnails_dir).unwrap_or(());
|
||||
std::fs::create_dir_all("./data").unwrap_or(());
|
||||
|
||||
fs::create_dir_all(&config.storage.images_dir).unwrap_or(());
|
||||
fs::create_dir_all(&config.storage.thumbnails_dir).unwrap_or(());
|
||||
fs::create_dir_all(&config.database.url[7..]).unwrap_or(());
|
||||
|
||||
// 在闭包外提取需要的路径
|
||||
let server_address = config.server_address();
|
||||
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(Arc::clone(&db)))
|
||||
@@ -77,26 +82,41 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/api/album/{id}", web::get().to(routes::get_album_detail))
|
||||
.route("/api/albums", web::post().to(routes::create_album))
|
||||
.route("/api/thumbnail/{id}", web::get().to(routes::get_thumbnail))
|
||||
.route("/api/original/{id}", web::get().to(routes::get_original_image))
|
||||
.route(
|
||||
"/api/original/{id}",
|
||||
web::get().to(routes::get_original_image),
|
||||
)
|
||||
// 收藏功能API路由
|
||||
.route("/api/favorites", web::get().to(routes::get_favorites))
|
||||
.route("/api/favorites/status/{id}", web::get().to(routes::check_favorite_status))
|
||||
.route("/api/favorites", web::post().to(routes::add_favorite))
|
||||
.route("/api/favorites/{id}", web::delete().to(routes::remove_favorite))
|
||||
.route("/api/favorites/image/{id}", web::delete().to(routes::remove_favorite_by_image))
|
||||
// 静态文件服务
|
||||
.service(afs::Files::new("/assets", "./assets").show_files_listing())
|
||||
// HTML页面路由
|
||||
.route("/", web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/index.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("首页文件不存在")
|
||||
}
|
||||
}))
|
||||
.route("/album", web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/album.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("相册页面文件不存在")
|
||||
}
|
||||
}))
|
||||
.route(
|
||||
"/",
|
||||
web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/index.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("首页文件不存在"),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/album",
|
||||
web::get().to(|| async {
|
||||
match fs::read_to_string("./assets/album.html") {
|
||||
Ok(content) => HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(content),
|
||||
Err(_) => HttpResponse::NotFound().body("相册页面文件不存在"),
|
||||
}
|
||||
}),
|
||||
)
|
||||
})
|
||||
.bind(server_address)?
|
||||
.run()
|
||||
|
||||
@@ -62,4 +62,32 @@ pub struct CreateImage {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub mime_type: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Favorite {
|
||||
pub id: Uuid,
|
||||
pub user_id: String,
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
pub created_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CreateFavorite {
|
||||
pub image_id: Uuid,
|
||||
pub album_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteWithImage {
|
||||
pub favorite: Favorite,
|
||||
pub image: Image,
|
||||
pub album_title: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct FavoriteStatus {
|
||||
pub is_favorited: bool,
|
||||
pub favorite_id: Option<Uuid>,
|
||||
}
|
||||
129
src/routes.rs
129
src/routes.rs
@@ -18,6 +18,133 @@ pub async fn get_albums(db: web::Data<Arc<Database>>) -> impl Responder {
|
||||
}
|
||||
}
|
||||
|
||||
// 收藏功能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>>,
|
||||
@@ -263,4 +390,4 @@ pub async fn get_thumbnail(
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user