From 03c904791064ac7823743b67e99bde9fbef3b323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?COMBRIAT=20K=C3=A9vin?= Date: Sun, 25 Aug 2024 14:32:39 +0900 Subject: [PATCH] fix: When converting bytes and buffers to Images we don't rely on image format guess when the extension is known --- eco-cbz/src/cbz.rs | 17 +++++++++++++---- eco-cbz/src/errors.rs | 7 +++++-- eco-cbz/src/image.rs | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/eco-cbz/src/cbz.rs b/eco-cbz/src/cbz.rs index 284dabc..55ebad7 100644 --- a/eco-cbz/src/cbz.rs +++ b/eco-cbz/src/cbz.rs @@ -5,6 +5,7 @@ use std::{ }; use camino::Utf8Path; +use image::ImageFormat; use tracing::debug; use zip::{read::ZipFile, write::FileOptions, ZipArchive, ZipWriter}; @@ -243,7 +244,7 @@ where .format() .extensions_str() .first() - .ok_or(Error::UnknownImageExtension)?; + .ok_or(Error::UnknownImageFormatExtensions)?; self.insert_with_extension_and_file_options(image, extension, FileOptions::default()) } @@ -259,7 +260,7 @@ where .format() .extensions_str() .first() - .ok_or(Error::UnknownImageExtension)?; + .ok_or(Error::UnknownImageFormatExtensions)?; self.insert_with_extension_and_file_options(image, extension, file_options) } @@ -286,7 +287,11 @@ where /// /// Same behavior as `insert_with_extension_and_file_options` pub fn insert_bytes_with_extension(&mut self, bytes: &[u8], extension: &str) -> Result<()> { - let image = bytes.try_into()?; + let image = Image::bytes_with_format( + bytes, + ImageFormat::from_extension(extension) + .ok_or_else(|| Error::InvalidImageExtension(extension.to_string()))?, + ); self.insert_with_extension(image, extension) } @@ -299,7 +304,11 @@ where extension: &str, file_options: FileOptions, ) -> Result<()> { - let image = bytes.try_into()?; + let image = Image::bytes_with_format( + bytes, + ImageFormat::from_extension(extension) + .ok_or_else(|| Error::InvalidImageExtension(extension.to_string()))?, + ); self.insert_with_extension_and_file_options(image, extension, file_options) } diff --git a/eco-cbz/src/errors.rs b/eco-cbz/src/errors.rs index 7e0c66f..ea560a3 100644 --- a/eco-cbz/src/errors.rs +++ b/eco-cbz/src/errors.rs @@ -38,8 +38,11 @@ pub enum Error { #[error("unknown image format error")] UnknownImageFormat, - #[error("unknown image extension error")] - UnknownImageExtension, + #[error("unknown image format extension error")] + UnknownImageFormatExtensions, + + #[error("unknown image extension error: {0}")] + InvalidImageExtension(String), #[cfg(feature = "metadata")] #[error("metadata error: {0}")] diff --git a/eco-cbz/src/image.rs b/eco-cbz/src/image.rs index 87549c1..89f7709 100644 --- a/eco-cbz/src/image.rs +++ b/eco-cbz/src/image.rs @@ -98,6 +98,17 @@ impl<'a> Image> { format, }) } + + #[must_use] + pub fn bytes_with_format(bytes: &'a [u8], format: ImageFormat) -> Self { + let mut reader = ImageReader::new(Cursor::new(bytes)); + reader.set_format(format); + + Self { + inner: reader.into(), + format, + } + } } impl Image>> { @@ -116,6 +127,17 @@ impl Image>> { }) } + #[must_use] + pub fn buf_with_format(buf: Vec, format: ImageFormat) -> Self { + let mut reader = ImageReader::new(Cursor::new(buf)); + reader.set_format(format); + + Self { + inner: reader.into(), + format, + } + } + #[allow(clippy::missing_errors_doc)] pub fn try_from_zip_file(mut file: ZipFile<'_>) -> Result { #[allow(clippy::cast_possible_truncation)]