优化文件加载速度,采用懒加载方法

This commit is contained in:
2026-02-06 13:38:51 +06:00
parent d7cfb044d5
commit e3f6ddf851
5 changed files with 392 additions and 71 deletions

View File

@@ -749,37 +749,11 @@ async fn analyze_files(paths: Vec<String>) -> Result<Vec<InputFile>, String> {
let metadata = fs::metadata(&path).map_err(|e: std::io::Error| e.to_string())?;
let size = metadata.len();
// 生成缩略图(仅视频和图片)
let thumbnail = if file_type == FileType::Video || file_type == FileType::Image {
match generate_thumbnail(&path, &file_type).await {
Ok(thumb) => {
println!("✅ 缩略图生成成功: {}", name);
Some(thumb)
}
Err(e) => {
println!("⚠️ 缩略图生成失败 '{}': {}", name, e);
None
}
}
} else {
None
};
// 完全取消缩略图生成,提升加载速度
let thumbnail = None;
// 获取文件编码信息(视频、音频和图片)
let codec_info = if file_type == FileType::Video || file_type == FileType::Audio || file_type == FileType::Image {
match get_file_codec_info(&path, &file_type).await {
Some(info) => {
println!("✅ 编码信息获取成功: {}", name);
Some(info)
}
None => {
println!("⚠️ 编码信息获取失败: {}", name);
None
}
}
} else {
None
};
// 编码信息也改为懒加载,不在分析阶段获取
let codec_info = None;
files.push(InputFile {
id: Uuid::new_v4().to_string(),
@@ -1647,6 +1621,46 @@ fn get_task_status(task_id: String, state: tauri::State<AppState>) -> Option<Bat
tasks.get(&task_id).cloned()
}
/// 懒加载生成单个文件的缩略图
#[tauri::command]
async fn generate_file_thumbnail(path: String, file_type: FileType) -> Result<String, String> {
generate_thumbnail(&path, &file_type).await
}
/// 懒加载获取文件编码信息
#[tauri::command]
async fn get_file_info(path: String, file_type: FileType) -> Result<Option<FileCodecInfo>, String> {
Ok(get_file_codec_info(&path, &file_type).await)
}
/// 读取图片文件并转换为 base64用于直接显示
#[tauri::command]
async fn read_image_as_base64(path: String) -> Result<String, String> {
use std::fs;
// 读取文件
let data = fs::read(&path).map_err(|e| format!("读取文件失败: {}", e))?;
// 根据文件扩展名确定 MIME 类型
let mime_type = if path.to_lowercase().ends_with(".png") {
"image/png"
} else if path.to_lowercase().ends_with(".jpg") || path.to_lowercase().ends_with(".jpeg") {
"image/jpeg"
} else if path.to_lowercase().ends_with(".gif") {
"image/gif"
} else if path.to_lowercase().ends_with(".webp") {
"image/webp"
} else if path.to_lowercase().ends_with(".bmp") {
"image/bmp"
} else {
"image/jpeg" // 默认
};
// 转换为 base64
let base64_data = BASE64_STD.encode(&data);
Ok(format!("data:{};base64,{}", mime_type, base64_data))
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
@@ -1672,6 +1686,9 @@ pub fn run() {
resume_task,
cancel_task,
get_task_status,
generate_file_thumbnail,
get_file_info,
read_image_as_base64,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");