init
This commit is contained in:
250
src/import.rs
Normal file
250
src/import.rs
Normal file
@@ -0,0 +1,250 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use image::GenericImageView;
|
||||
use serde::Deserialize;
|
||||
use crate::config::Config;
|
||||
use crate::database::Database;
|
||||
use crate::models::*;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct AlbumIndex {
|
||||
title: String,
|
||||
images: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn import_existing_data(config: &Config, db: &Database) -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("开始扫描图片目录并导入新相册...");
|
||||
|
||||
let albums_dir = Path::new(&config.storage.images_dir);
|
||||
|
||||
if !albums_dir.exists() {
|
||||
println!("albums目录不存在,跳过导入");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// 获取数据库中现有的相册列表
|
||||
let existing_albums = db.get_all_albums().await?;
|
||||
let existing_titles: Vec<String> = existing_albums.iter()
|
||||
.map(|album| album.document.to_lowercase())
|
||||
.collect();
|
||||
|
||||
println!("数据库中现有相册数量: {}", existing_albums.len());
|
||||
|
||||
let entries = fs::read_dir(albums_dir)?;
|
||||
let mut imported_count = 0;
|
||||
|
||||
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()) {
|
||||
// 检查是否已存在同名相册
|
||||
if existing_titles.contains(&folder_name.to_lowercase()) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("导入完成!共导入 {} 个新相册", imported_count);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取图片的实际信息(文件大小、宽度、高度)
|
||||
fn get_image_info(image_path: &Path) -> Result<(i64, i32, i32), Box<dyn std::error::Error>> {
|
||||
// 获取文件大小
|
||||
let metadata = fs::metadata(image_path)?;
|
||||
let file_size = metadata.len() as i64;
|
||||
|
||||
// 使用image库获取图片尺寸
|
||||
let img = image::open(image_path)?;
|
||||
let (width, height) = img.dimensions();
|
||||
|
||||
Ok((file_size, width as i32, height as i32))
|
||||
}
|
||||
Reference in New Issue
Block a user