update code level

This commit is contained in:
lychang
2026-03-25 14:06:49 +08:00
parent 448eef2319
commit e05cf48063
7 changed files with 254 additions and 192 deletions

View File

@@ -16,7 +16,7 @@
<div class="search-container"> <div class="search-container">
<div class="search-box"> <div class="search-box">
<label for="search-input"></label><input type="text" id="search-input" placeholder="搜索图片标题..." onkeypress="handleKeyPress(event)"> <label for="search-input"></label><input type="text" id="search-input" placeholder="搜索图片标题...">
<button onclick="searchAlbums()"><i class="fas fa-search"></i></button> <button onclick="searchAlbums()"><i class="fas fa-search"></i></button>
</div> </div>

View File

@@ -54,7 +54,7 @@ function initSearch() {
function initInfiniteScroll() { function initInfiniteScroll() {
let scrollTimeout; let scrollTimeout;
window.addEventListener('scroll', function() { window.addEventListener('scroll', function () {
clearTimeout(scrollTimeout); clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(handleScroll, 100); scrollTimeout = setTimeout(handleScroll, 100);
}); });
@@ -110,11 +110,6 @@ function showLoadMoreError() {
container.appendChild(errorElement); container.appendChild(errorElement);
} }
// 防抖搜索
function debouncedSearch() {
searchAlbums();
}
// 显示骨架屏 // 显示骨架屏
function showSkeleton(count = 6) { function showSkeleton(count = 6) {
const container = document.getElementById('albums-container'); const container = document.getElementById('albums-container');
@@ -222,7 +217,7 @@ async function loadAlbums(reset = false) {
showLoadingMore(); showLoadingMore();
} }
const response = await fetch(`${API_BASE}/albums?page=${currentPage}&limit=${albumsPerPage}`); const response = await fetch(`${API_BASE}/albums`);
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
@@ -264,7 +259,7 @@ async function loadAlbums(reset = false) {
// 搜索相册 // 搜索相册
// 防抖搜索函数 // 防抖搜索函数
function debouncedSearch() { async function debouncedSearch() {
const searchTerm = document.getElementById('search-input').value.toLowerCase().trim(); const searchTerm = document.getElementById('search-input').value.toLowerCase().trim();
if (searchTerm === '') { if (searchTerm === '') {
@@ -274,17 +269,8 @@ function debouncedSearch() {
return; return;
} }
// 在当前已加载的数据集中搜索 // 直接使用后端搜索API进行全站搜索
const filteredAlbums = searchInLoadedAlbums(searchTerm); await performServerSearch(searchTerm);
if (filteredAlbums.length > 0) {
// 如果当前已加载的数据中有匹配项,直接显示
displayAlbums(filteredAlbums);
document.getElementById('stats').innerHTML = `在当前页面中找到 ${filteredAlbums.length} 个包含 "${searchTerm}" 的相册`;
} else {
// 如果没有找到匹配项,显示提示信息
showSearchHint(searchTerm);
}
} }
// 在当前已加载的数据集中搜索 // 在当前已加载的数据集中搜索
@@ -461,20 +447,24 @@ function showError(message) {
document.getElementById('stats').innerHTML = ''; document.getElementById('stats').innerHTML = '';
} }
// 键盘事件处理 // 搜索相册函数
function handleKeyPress(event) { async function searchAlbums() {
if (event.key === 'Enter') { await debouncedSearch();
searchAlbums();
}
} }
// 性能监控 // 性能监控
function logPerformance() { function logPerformance() {
if ('performance' in window) { if ('performance' in window) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
setTimeout(() => { setTimeout(() => {
const navigationTiming = performance.getEntriesByType('navigation')[0]; const navigationTiming = performance.getEntriesByType('navigation')[0];
console.log('页面加载时间:', navigationTiming.loadEventEnd - navigationTiming.navigationStart, 'ms'); if (navigationTiming && navigationTiming.loadEventEnd && navigationTiming.navigationStart) {
const loadTime = navigationTiming.loadEventEnd - navigationTiming.navigationStart;
console.log('页面加载时间:', loadTime, 'ms');
} else {
console.log('页面加载时间: 无法计算');
}
}, 0); }, 0);
}); });
} }
@@ -545,7 +535,7 @@ function optimizeScrollPerformance() {
} }
} }
window.addEventListener('scroll', updateOnScroll, { passive: true }); window.addEventListener('scroll', updateOnScroll, {passive: true});
} }
// 延迟加载非关键图片 // 延迟加载非关键图片
@@ -907,7 +897,7 @@ function updatePreviewContent(newIndex) {
// 更新图片 // 更新图片
const imageElement = document.getElementById('preview-image'); const imageElement = document.getElementById('preview-image');
if (imageElement ) { if (imageElement) {
// 显示加载状态 // 显示加载状态
imageElement.style.opacity = '0'; imageElement.style.opacity = '0';
@@ -918,7 +908,7 @@ function updatePreviewContent(newIndex) {
newImage.alt = favorite.image.title || '预览图片'; newImage.alt = favorite.image.title || '预览图片';
newImage.className = 'preview-image'; newImage.className = 'preview-image';
newImage.onload = function() { newImage.onload = function () {
// 替换图片 // 替换图片
if (imageElement.parentNode) { if (imageElement.parentNode) {
imageElement.parentNode.replaceChild(newImage, imageElement); imageElement.parentNode.replaceChild(newImage, imageElement);
@@ -929,7 +919,7 @@ function updatePreviewContent(newIndex) {
newImage.style.opacity = '1'; newImage.style.opacity = '1';
}; };
newImage.onerror = function() { newImage.onerror = function () {
// 图片加载失败 // 图片加载失败
imageElement.innerHTML = '<i class="fas fa-exclamation-triangle"></i><span>图片加载失败</span>'; imageElement.innerHTML = '<i class="fas fa-exclamation-triangle"></i><span>图片加载失败</span>';
}; };
@@ -1010,7 +1000,6 @@ function openFavoriteImage(favoriteId) {
} }
// 导出函数供全局使用 // 导出函数供全局使用
window.handleKeyPress = handleKeyPress;
window.searchAlbums = searchAlbums; window.searchAlbums = searchAlbums;
window.openRandomAlbum = openRandomAlbum; window.openRandomAlbum = openRandomAlbum;
window.showFavorites = showFavorites; window.showFavorites = showFavorites;

View File

@@ -85,8 +85,8 @@ impl ImageProcessor {
// 使用快速解码和缩放 // 使用快速解码和缩放
let img = image::load_from_memory(image_data)?; let img = image::load_from_memory(image_data)?;
// 快速缩放到小尺寸(200x150) // 快速缩放到小尺寸(200x200)
let thumbnail = img.thumbnail(200, 150); let thumbnail = img.thumbnail(200, 200);
// 使用低质量JPEG压缩以加快编码速度 // 使用低质量JPEG压缩以加快编码速度
let mut output = Vec::new(); let mut output = Vec::new();
@@ -165,8 +165,8 @@ impl ImageProcessor {
// 加载图片 // 加载图片
let img = image::load_from_memory(image_data)?; let img = image::load_from_memory(image_data)?;
// 生成缩略图(400x300,保持宽高比) // 生成缩略图(600x600,保持宽高比)
let thumbnail = img.thumbnail(600, 400); let thumbnail = img.thumbnail(600, 600);
// 将缩略图数据保存到内存缓冲区 // 将缩略图数据保存到内存缓冲区
let mut buffer = std::io::Cursor::new(Vec::new()); let mut buffer = std::io::Cursor::new(Vec::new());

View File

@@ -9,6 +9,51 @@ use crate::config::Config;
use crate::database::Database; use crate::database::Database;
use crate::models::*; use crate::models::*;
/// 批量插入图片并设置封面缩略图的辅助函数
async fn batch_insert_images_and_set_cover(
db: &Database,
images_to_insert: Vec<CreateImage>,
album_id: Uuid,
album_path: &Path,
folder_name: &str,
) -> Result<usize, Box<dyn std::error::Error>> {
if images_to_insert.is_empty() {
return Ok(0);
}
println!(" 批量插入 {} 张图片...", images_to_insert.len());
match db.add_images(images_to_insert).await {
Ok(ids) => {
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
// 设置第一张图片为封面图片并生成缩略图
if let Some(first_image_id) = ids.first() {
println!(" 设置第一张图片为封面图片 (ID: {})...", first_image_id);
// 更新相册的封面图片ID
if let Err(e) = db.update_album_cover(album_id, Some(*first_image_id)).await {
eprintln!(" 更新相册封面失败: {}", e);
}
// 获取第一张图片的信息
if let Ok(Some(first_image)) = db.get_image(*first_image_id).await {
// 生成缩略图
match generate_thumbnail_for_image(&first_image, album_path, folder_name).await {
Ok(_) => println!(" 封面缩略图生成成功"),
Err(e) => eprintln!(" 封面缩略图生成失败: {}", e),
}
}
}
Ok(ids.len())
}
Err(e) => {
eprintln!(" 批量插入失败: {}", e);
Ok(0)
}
}
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct AlbumIndex { struct AlbumIndex {
title: String, title: String,
@@ -125,9 +170,6 @@ pub async fn import_existing_data(config: &Config, db: Arc<Database>) -> Result<
// 继续处理下一个相册,不中断整个导入过程 // 继续处理下一个相册,不中断整个导入过程
} }
} }
// 在每个相册导入后添加短暂延迟,减少数据库压力
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
} }
println!("🎉 导入完成!成功导入 {} 个相册,共 {} 张图片", successful_albums, total_imported); println!("🎉 导入完成!成功导入 {} 个相册,共 {} 张图片", successful_albums, total_imported);
@@ -261,42 +303,8 @@ async fn import_single_album(db: &Database, folder_name: &str, path: &Path) -> R
} }
} }
// 批量插入图片 // 使用辅助函数批量插入图片并设置封面
if !images_to_insert.is_empty() { batch_insert_images_and_set_cover(&db, images_to_insert, album_id, path, folder_name).await
println!(" 批量插入 {} 张图片...", images_to_insert.len());
match db.add_images(images_to_insert).await {
Ok(ids) => {
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
// 设置第一张图片为封面图片并生成缩略图
if let Some(first_image_id) = ids.first() {
println!(" 设置第一张图片为封面图片 (ID: {})...", first_image_id);
// 更新相册的封面图片ID
if let Err(e) = db.update_album_cover(album_id, Some(*first_image_id)).await {
eprintln!(" 更新相册封面失败: {}", e);
}
// 获取第一张图片的信息
if let Ok(Some(first_image)) = db.get_image(*first_image_id).await {
// 生成缩略图
match generate_thumbnail_for_image(&first_image, path, folder_name).await {
Ok(_) => println!(" 封面缩略图生成成功"),
Err(e) => eprintln!(" 封面缩略图生成失败: {}", e),
}
}
}
Ok(ids.len())
}
Err(e) => {
eprintln!(" 批量插入失败: {}", e);
Ok(0)
}
}
} else {
Ok(0)
}
} }
/// 快速扫描图片文件 /// 快速扫描图片文件
@@ -438,47 +446,13 @@ async fn reimport_single_album(
println!(" 发现 {} 张图片需要导入", images_to_insert.len()); println!(" 发现 {} 张图片需要导入", images_to_insert.len());
// 批量插入图片 // 使用辅助函数批量插入图片并设置封面
if !images_to_insert.is_empty() { batch_insert_images_and_set_cover(&db, images_to_insert, album_id, path, folder_name).await
println!(" 批量插入 {} 张图片...", images_to_insert.len());
match db.add_images(images_to_insert).await {
Ok(ids) => {
println!(" 批量插入成功,共插入 {} 张图片", ids.len());
// 设置第一张图片为封面图片并生成缩略图
if let Some(first_image_id) = ids.first() {
println!(" 设置第一张图片为封面图片 (ID: {})...", first_image_id);
// 更新相册的封面图片ID
if let Err(e) = db.update_album_cover(album_id, Some(*first_image_id)).await {
eprintln!(" 更新相册封面失败: {}", e);
}
// 获取第一张图片的信息
if let Ok(Some(first_image)) = db.get_image(*first_image_id).await {
// 生成缩略图
match generate_thumbnail_for_image(&first_image, path, folder_name).await {
Ok(_) => println!(" 封面缩略图生成成功"),
Err(e) => eprintln!(" 封面缩略图生成失败: {}", e),
}
}
}
Ok(ids.len())
}
Err(e) => {
eprintln!(" 批量插入失败: {}", e);
Ok(0)
}
}
} else {
Ok(0)
}
} }
/// 为图片生成缩略图(使用ImageProcessor) /// 为图片生成缩略图(使用ImageProcessor)
async fn generate_thumbnail_for_image( async fn generate_thumbnail_for_image(
image: &crate::models::Image, image: &Image,
album_path: &Path, album_path: &Path,
_folder_name: &str, _folder_name: &str,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {

View File

@@ -131,10 +131,7 @@ async fn main() -> std::io::Result<()> {
.route("/api/album/{id}", web::get().to(routes::get_album_detail)) .route("/api/album/{id}", web::get().to(routes::get_album_detail))
.route("/api/albums", web::post().to(routes::create_album)) .route("/api/albums", web::post().to(routes::create_album))
.route("/api/thumbnail/{id}", web::get().to(routes::get_thumbnail)) .route("/api/thumbnail/{id}", web::get().to(routes::get_thumbnail))
.route( .route("/api/original/{id}", web::get().to(routes::get_original_image))
"/api/original/{id}",
web::get().to(routes::get_original_image),
)
// 收藏功能API路由 // 收藏功能API路由
.route("/api/favorites", web::get().to(routes::get_favorites)) .route("/api/favorites", web::get().to(routes::get_favorites))
.route("/api/favorites/status/{id}", web::get().to(routes::check_favorite_status)) .route("/api/favorites/status/{id}", web::get().to(routes::check_favorite_status))

View File

@@ -12,7 +12,7 @@ pub struct Album {
pub updated_at: String, pub updated_at: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Image { pub struct Image {
pub id: Uuid, pub id: Uuid,
pub album_id: Uuid, pub album_id: Uuid,
@@ -147,11 +147,16 @@ impl AlbumsCache {
} }
pub fn search(&self, query: &str) -> Vec<AlbumInfo> { pub fn search(&self, query: &str) -> Vec<AlbumInfo> {
let query_lower = query.to_lowercase(); let query_lower = query.to_lowercase().trim().to_string();
if query_lower.is_empty() {
return Vec::new();
}
let mut results = Vec::new(); let mut results = Vec::new();
let mut seen_ids = std::collections::HashSet::new(); let mut seen_ids = std::collections::HashSet::new();
// 搜索完整匹配 // 1. 搜索完整匹配(最高优先级)
if let Some(matches) = self.search_index.get(&query_lower) { if let Some(matches) = self.search_index.get(&query_lower) {
for album in matches { for album in matches {
if seen_ids.insert(album.id) { if seen_ids.insert(album.id) {
@@ -160,10 +165,45 @@ impl AlbumsCache {
} }
} }
// 搜索单词匹配 // 2. 搜索单词匹配
for word in query_lower.split_whitespace() { for word in query_lower.split_whitespace() {
if let Some(matches) = self.search_index.get(word) { if word.len() >= 2 { // 只搜索长度>=2的单词
for album in matches { if let Some(matches) = self.search_index.get(word) {
for album in matches {
if seen_ids.insert(album.id) {
results.push(album.clone());
}
}
}
}
}
// 3. 模糊搜索:在相册标题和描述中搜索包含查询词的结果
if results.is_empty() {
for album in &self.albums {
let title_lower = album.title.to_lowercase();
let description_lower = album.description.as_ref()
.map(|d| d.to_lowercase())
.unwrap_or_default();
// 检查标题或描述是否包含搜索词
if title_lower.contains(&query_lower) || description_lower.contains(&query_lower) {
if seen_ids.insert(album.id) {
results.push(album.clone());
}
}
}
}
// 4. 如果还没有结果,尝试部分匹配(更宽松的搜索)
if results.is_empty() && query_lower.len() >= 2 {
for album in &self.albums {
let title_lower = album.title.to_lowercase();
// 检查标题是否包含搜索词的任何部分
if query_lower.split_whitespace().any(|word|
word.len() >= 2 && title_lower.contains(word)
) {
if seen_ids.insert(album.id) { if seen_ids.insert(album.id) {
results.push(album.clone()); results.push(album.clone());
} }

View File

@@ -26,6 +26,60 @@ pub async fn get_favorites(db: web::Data<Arc<Database>>) -> impl Responder {
} }
} }
/// 检查缩略图质量并在需要时升级为高质量版本
async fn check_and_upgrade_thumbnail_quality(
config: &crate::config::StorageConfig,
image: &Image,
original_path: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
let thumbnail_path = config.thumbnails_dir.join(image.id.to_string());
// 检查原始图片是否存在
if !original_path.exists() {
return Ok(()); // 原始图片不存在,无法升级
}
// 读取缩略图文件信息
let thumbnail_metadata = match std::fs::metadata(&thumbnail_path) {
Ok(meta) => meta,
Err(_) => return Ok(()), // 无法读取缩略图元数据
};
// 检查缩略图文件大小(简单质量检测)
// 如果缩略图小于20KB,可能质量较低,需要升级
if thumbnail_metadata.len() < 20 * 1024 {
// 读取原始图片数据
let original_data = match std::fs::read(original_path) {
Ok(data) => data,
Err(e) => {
eprintln!("读取原始图片失败,无法升级缩略图: {}", e);
return Ok(());
}
};
// 生成高质量缩略图
let image_processor = crate::image_processor::ImageProcessor::new(config.clone());
match image_processor
.generate_thumbnail_from_memory(&original_data, &image.id.to_string())
.await
{
Ok(high_quality_data) => {
// 保存高质量缩略图
if let Err(e) = std::fs::write(&thumbnail_path, &high_quality_data) {
eprintln!("保存高质量缩略图失败: {}", e);
} else {
println!("✅ 缩略图已升级为高质量版本 (ID: {})", image.id);
}
}
Err(e) => {
eprintln!("生成高质量缩略图失败: {}", e);
}
}
}
Ok(())
}
// 检查图片是否被收藏 // 检查图片是否被收藏
pub async fn check_favorite_status( pub async fn check_favorite_status(
db: web::Data<Arc<Database>>, db: web::Data<Arc<Database>>,
@@ -179,21 +233,14 @@ pub async fn get_original_image(
} }
} }
pub async fn get_albums( pub async fn get_albums(albums_cache: web::Data<Arc<RwLock<AlbumsCache>>>) -> impl Responder {
albums_cache: web::Data<Arc<RwLock<crate::models::AlbumsCache>>>,
query: web::Query<HashMap<String, String>>,
) -> impl Responder {
let page = query.get("page").and_then(|s| s.parse::<usize>().ok()).unwrap_or(1);
let limit = query.get("limit").and_then(|s| s.parse::<usize>().ok()).unwrap_or(50);
let offset = (page - 1) * limit;
// 从内存缓存获取数据 // 从内存缓存获取数据
let cache = albums_cache.read().await; let cache = albums_cache.read().await;
let total_albums = cache.albums.len(); let total_albums = cache.albums.len();
// 应用分页 // 应用分页
let start = offset.min(total_albums); let start = 0;
let end = (offset + limit).min(total_albums); let end = total_albums;
let paginated_albums = if start < end { let paginated_albums = if start < end {
cache.albums[start..end].to_vec() cache.albums[start..end].to_vec()
} else { } else {
@@ -206,8 +253,8 @@ pub async fn get_albums(
} }
pub async fn search_albums( pub async fn search_albums(
albums_cache: web::Data<Arc<RwLock<crate::models::AlbumsCache>>>, albums_cache: web::Data<Arc<RwLock<AlbumsCache>>>,
query: web::Query<std::collections::HashMap<String, String>>, query: web::Query<HashMap<String, String>>,
) -> impl Responder { ) -> impl Responder {
let q = query.get("q").map(|s| s.as_str()).unwrap_or(""); let q = query.get("q").map(|s| s.as_str()).unwrap_or("");
let page = query let page = query
@@ -350,10 +397,25 @@ pub async fn get_thumbnail(
let original_path = image_processor.config.images_dir.join(&image.filename); let original_path = image_processor.config.images_dir.join(&image.filename);
if thumbnail_path.exists() { if thumbnail_path.exists() {
// 返回缩略图 // 返回现有缩略图,并异步检查是否需要替换为高质量版本
match fs::read(&thumbnail_path) { match fs::read(&thumbnail_path) {
Ok(data) => { Ok(data) => {
let mime_type = image_processor.detect_mime_type("jpg"); // 缩略图总是JPEG格式 let mime_type = image_processor.detect_mime_type("jpg"); // 缩略图总是JPEG格式
// 异步检查缩略图质量并可能替换为高质量版本
let config_clone = image_processor.config.clone();
let image_clone = image.clone();
let original_path_clone = original_path.clone();
tokio::spawn(async move {
if let Err(e) = check_and_upgrade_thumbnail_quality(
&config_clone,
&image_clone,
&original_path_clone
).await {
eprintln!("缩略图质量检查失败: {}", e);
}
});
HttpResponse::Ok() HttpResponse::Ok()
.content_type(mime_type) .content_type(mime_type)
.insert_header(("Cache-Control", "public, max-age=31536000")) // 1年缓存 .insert_header(("Cache-Control", "public, max-age=31536000")) // 1年缓存
@@ -405,7 +467,7 @@ pub async fn get_thumbnail(
// 返回快速生成的缩略图数据 // 返回快速生成的缩略图数据
HttpResponse::Ok() HttpResponse::Ok()
.content_type("image/jpeg") .content_type("image/jpeg")
.insert_header(("Cache-Control", "public, max-age=31536000")) // 1年缓存 .insert_header(("Cache-Control", "public, max-age=30")) // 0.5分钟缓存
.insert_header(("ETag", format!("\"{}\"", image_id))) .insert_header(("ETag", format!("\"{}\"", image_id)))
.body(thumbnail_data) .body(thumbnail_data)
} }