improvement(indexing): cover extraction performance gains

This commit is contained in:
2024-11-28 03:42:36 +01:00
parent 05b2e11ab4
commit d7b226025e
2 changed files with 26 additions and 32 deletions

View File

@@ -1,8 +1,8 @@
use std::{fs, path::Path, time::Instant};
use std::{fs, path::Path};
use image::EncodableLayout;
use webp::Encoder;
use axum::Router;
use tower_http::services::ServeDir;
use webp::Encoder;
use crate::music::metadata::CoverData;
@@ -40,9 +40,7 @@ pub fn get_all_cover_hashes() -> Vec<String> {
hashes
}
pub fn write_cover(hash: String, cover: CoverData) -> Result<(), Box<dyn std::error::Error>> {
let now = Instant::now();
pub fn write_cover(hash: &str, cover: &CoverData, base_path: &str) -> Result<(), Box<dyn std::error::Error>> {
if cover.mime_type != "image/jpeg"
&& cover.mime_type != "image/png"
&& cover.mime_type != "image/webp"
@@ -50,8 +48,6 @@ pub fn write_cover(hash: String, cover: CoverData) -> Result<(), Box<dyn std::er
return Err(format!("Invalid cover MIME type: {}", cover.mime_type).into());
}
let base_path = get_cover_base_path();
let path = Path::new(&base_path).join(format!("{hash}.webp"));
let dynamic_image = image::load_from_memory_with_format(
@@ -65,19 +61,16 @@ pub fn write_cover(hash: String, cover: CoverData) -> Result<(), Box<dyn std::er
)?;
let resized_image = if dynamic_image.width() > 640 || dynamic_image.height() > 640 {
dynamic_image.resize_to_fill(640, 640, image::imageops::FilterType::Lanczos3)
let a = dynamic_image.resize_to_fill(640, 640, image::imageops::FilterType::Lanczos3);
drop(dynamic_image);
a
} else {
dynamic_image
};
let webp_encoder = Encoder::from_image(&resized_image)?;
let encoded_image = webp_encoder.encode_lossless().to_vec();
let webp = Encoder::from_image(&resized_image)?.encode_lossless();
fs::write(path, encoded_image)?;
let elapsed = now.elapsed();
println!("Writing '{}' cover took {:.?}", hash, elapsed);
fs::write(path, webp.as_bytes())?;
Ok(())
}