Skip to content

Commit

Permalink
Merge pull request #13 from gaku-sei/fix-image-format-issues
Browse files Browse the repository at this point in the history
fix: When converting bytes and buffers to Images we don't rely on image format guess when the extension is known
  • Loading branch information
gaku-sei authored Aug 25, 2024
2 parents f868134 + 03c9047 commit a6561ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
17 changes: 13 additions & 4 deletions eco-cbz/src/cbz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use camino::Utf8Path;
use image::ImageFormat;
use tracing::debug;
use zip::{read::ZipFile, write::FileOptions, ZipArchive, ZipWriter};

Expand Down Expand Up @@ -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())
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
7 changes: 5 additions & 2 deletions eco-cbz/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down
22 changes: 22 additions & 0 deletions eco-cbz/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ impl<'a> Image<Cursor<&'a [u8]>> {
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<Cursor<Vec<u8>>> {
Expand All @@ -116,6 +127,17 @@ impl Image<Cursor<Vec<u8>>> {
})
}

#[must_use]
pub fn buf_with_format(buf: Vec<u8>, 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<Self> {
#[allow(clippy::cast_possible_truncation)]
Expand Down

0 comments on commit a6561ad

Please sign in to comment.