Implement video thumbnail generation and Windows 11 UI
This commit is contained in:
1 parent
a1af08aa00
commit
74dd0a9cf5
8 files changed
+229
-37
No files matched your search
+1
@@ -4239,6 +4239,7 @@ name = "tauri-app"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.23.1",
|
||||
"image",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
@@ -26,6 +26,7 @@ anyhow = "1.0.104"
|
||||
tokio = { version = "1.54.1", features = ["full"] }
|
||||
image = "0.25.10"
|
||||
tauri-plugin-dialog = "2.8.4"
|
||||
base64 = "0.23.1"
|
||||
|
||||
|
||||
# Read the optimization guideline for more details: https://tauri.app/concept/size/#cargo-configuration
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
mod media;
|
||||
mod thumbnails;
|
||||
|
||||
use media::{list_files, FileInfo};
|
||||
use thumbnails::{ThumbnailCache, get_thumbnail};
|
||||
use tauri::State;
|
||||
|
||||
#[tauri::command]
|
||||
async fn list_files_command(dir_path: String) -> Result<Vec<FileInfo>, String> {
|
||||
list_files(&dir_path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_thumbnail_command(path: String, cache: State<'_, ThumbnailCache>) -> Result<String, String> {
|
||||
get_thumbnail(&path, cache.inner()).await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
@@ -17,7 +25,8 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.invoke_handler(tauri::generate_handler![greet, list_files_command])
|
||||
.manage(ThumbnailCache::default())
|
||||
.invoke_handler(tauri::generate_handler![greet, list_files_command, get_thumbnail_command])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,55 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use image::GenericImageView;
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ThumbnailCache {
|
||||
pub cache: Mutex<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
impl ThumbnailCache {
|
||||
pub fn get(&self, path: &str) -> Option<String> {
|
||||
let cache = self.cache.lock().unwrap();
|
||||
cache.get(path).cloned()
|
||||
}
|
||||
|
||||
pub fn set(&self, path: String, thumbnail: String) {
|
||||
let mut cache = self.cache.lock().unwrap();
|
||||
cache.insert(path, thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_thumbnail(path: &str, cache: &ThumbnailCache) -> Result<String> {
|
||||
let path_str = path.to_string();
|
||||
|
||||
// Check cache first
|
||||
if let Some(cached) = cache.get(path_str.as_str()) {
|
||||
return Ok(cached);
|
||||
}
|
||||
|
||||
// Generate thumbnail
|
||||
let thumbnail_base64 = generate_thumbnail_base64(path).await?;
|
||||
|
||||
// Store in cache
|
||||
cache.set(path_str, thumbnail_base64.clone());
|
||||
|
||||
Ok(thumbnail_base64)
|
||||
}
|
||||
|
||||
async fn generate_thumbnail_base64(path: &str) -> Result<String> {
|
||||
let img = image::open(path).map_err(|e| anyhow!("Failed to open image: {}", e))?;
|
||||
|
||||
// Resize to 128x128
|
||||
let thumbnail = img.thumbnail(128, 128);
|
||||
|
||||
let mut buffer = std::io::Cursor::new(Vec::new());
|
||||
thumbnail.write_to(&mut buffer, image::ImageFormat::Png)
|
||||
.map_err(|e| anyhow!("Failed to write thumbnail: {}", e))?;
|
||||
|
||||
let bytes = buffer.into_inner();
|
||||
let base64_str = general_purpose::STANDARD.encode(bytes);
|
||||
|
||||
Ok(format!("data:image/png;base64,{}", base64_str))
|
||||
}
|
||||
Reference in new issue
Block a user