feat(covers): dynamically determine image format
This commit is contained in:
@@ -52,17 +52,7 @@ pub fn write_cover(
|
|||||||
return Err(format!("Invalid cover MIME type: {}", cover.mime_type).into());
|
return Err(format!("Invalid cover MIME type: {}", cover.mime_type).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = Path::new(&base_path).join(format!("{hash}.webp"));
|
let dynamic_image = image::load_from_memory(&cover.bytes)?;
|
||||||
|
|
||||||
let dynamic_image = image::load_from_memory_with_format(
|
|
||||||
&cover.bytes,
|
|
||||||
match cover.mime_type.as_str() {
|
|
||||||
"image/png" => image::ImageFormat::Png,
|
|
||||||
"image/jpeg" => image::ImageFormat::Jpeg,
|
|
||||||
"image/webp" => image::ImageFormat::WebP,
|
|
||||||
_ => panic!("Invalid cover MIME type (this should never happen)"),
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let resized_image = if dynamic_image.width() > 640 || dynamic_image.height() > 640 {
|
let resized_image = if dynamic_image.width() > 640 || dynamic_image.height() > 640 {
|
||||||
let a = dynamic_image.resize_to_fill(640, 640, image::imageops::FilterType::Lanczos3);
|
let a = dynamic_image.resize_to_fill(640, 640, image::imageops::FilterType::Lanczos3);
|
||||||
@@ -74,6 +64,8 @@ pub fn write_cover(
|
|||||||
|
|
||||||
let webp = Encoder::from_image(&resized_image)?.encode_lossless();
|
let webp = Encoder::from_image(&resized_image)?.encode_lossless();
|
||||||
|
|
||||||
|
let path = Path::new(&base_path).join(format!("{hash}.webp"));
|
||||||
|
|
||||||
fs::write(path, webp.as_bytes())?;
|
fs::write(path, webp.as_bytes())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user