feat(backend): resize and thumbnail custom artist images
This commit is contained in:
1 file changed
+46
-12
@@ -4218,12 +4218,18 @@ pub async fn v2_library_cache_artist_image(
|
|||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, serde::Serialize)]
|
||||||
|
pub struct CustomArtistImageResult {
|
||||||
|
pub image_path: String,
|
||||||
|
pub thumbnail_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn v2_library_set_custom_artist_image(
|
pub async fn v2_library_set_custom_artist_image(
|
||||||
artist_name: String,
|
artist_name: String,
|
||||||
custom_image_path: String,
|
custom_image_path: String,
|
||||||
state: State<'_, LibraryState>,
|
state: State<'_, LibraryState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<CustomArtistImageResult, String> {
|
||||||
let artwork_dir = get_artwork_cache_dir();
|
let artwork_dir = get_artwork_cache_dir();
|
||||||
let source = std::path::Path::new(&custom_image_path);
|
let source = std::path::Path::new(&custom_image_path);
|
||||||
if !source.exists() {
|
if !source.exists() {
|
||||||
@@ -4232,20 +4238,43 @@ pub async fn v2_library_set_custom_artist_image(
|
|||||||
custom_image_path
|
custom_image_path
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let extension = source.extension().and_then(|e| e.to_str()).unwrap_or("jpg");
|
|
||||||
use md5::{Digest, Md5};
|
// Validate extension
|
||||||
|
let extension = source
|
||||||
|
.extension()
|
||||||
|
.and_then(|e| e.to_str())
|
||||||
|
.map(|e| e.to_lowercase())
|
||||||
|
.unwrap_or_default();
|
||||||
|
if !["png", "jpg", "jpeg", "webp"].contains(&extension.as_str()) {
|
||||||
|
return Err(format!(
|
||||||
|
"Unsupported image format: {}. Use png, jpg, jpeg, or webp.",
|
||||||
|
extension
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate filename using MD5 hash of artist name
|
||||||
let mut hasher = Md5::new();
|
let mut hasher = Md5::new();
|
||||||
hasher.update(artist_name.as_bytes());
|
hasher.update(artist_name.as_bytes());
|
||||||
let artist_hash = format!("{:x}", hasher.finalize());
|
let artist_hash = format!("{:x}", hasher.finalize());
|
||||||
let filename = format!(
|
let timestamp = chrono::Utc::now().timestamp();
|
||||||
"artist_custom_{}_{}.{}",
|
let filename = format!("artist_custom_{}_{}.jpg", artist_hash, timestamp);
|
||||||
artist_hash,
|
let dest_path = artwork_dir.join(&filename);
|
||||||
chrono::Utc::now().timestamp(),
|
|
||||||
extension
|
|
||||||
);
|
|
||||||
let dest_path = artwork_dir.join(filename);
|
|
||||||
std::fs::copy(source, &dest_path).map_err(|e| format!("Failed to copy artwork: {}", e))?;
|
|
||||||
|
|
||||||
|
// Decode, resize to max 1000x1000, save as JPEG
|
||||||
|
let img = image::ImageReader::open(source)
|
||||||
|
.map_err(|e| format!("Failed to open image: {}", e))?
|
||||||
|
.decode()
|
||||||
|
.map_err(|e| format!("Failed to decode image: {}", e))?;
|
||||||
|
let resized = img.resize(1000, 1000, image::imageops::FilterType::Lanczos3);
|
||||||
|
resized
|
||||||
|
.save(&dest_path)
|
||||||
|
.map_err(|e| format!("Failed to save resized image: {}", e))?;
|
||||||
|
|
||||||
|
// Generate 500x500 thumbnail using qbz-library
|
||||||
|
let thumbnail_path = thumbnails::generate_thumbnail(&dest_path)
|
||||||
|
.map_err(|e| format!("Failed to generate thumbnail: {}", e))?;
|
||||||
|
|
||||||
|
// Update database
|
||||||
let guard = state.db.lock().await;
|
let guard = state.db.lock().await;
|
||||||
let db = guard.as_ref().ok_or("No active session - please log in")?;
|
let db = guard.as_ref().ok_or("No active session - please log in")?;
|
||||||
db.cache_artist_image(
|
db.cache_artist_image(
|
||||||
@@ -4254,7 +4283,12 @@ pub async fn v2_library_set_custom_artist_image(
|
|||||||
"custom",
|
"custom",
|
||||||
Some(&dest_path.to_string_lossy()),
|
Some(&dest_path.to_string_lossy()),
|
||||||
)
|
)
|
||||||
.map_err(|e| e.to_string())
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
Ok(CustomArtistImageResult {
|
||||||
|
image_path: dest_path.to_string_lossy().into_owned(),
|
||||||
|
thumbnail_path: thumbnail_path.to_string_lossy().into_owned(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
|
|||||||
Reference in new issue
Block a user