Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: When converting bytes and buffers to Images we don't rely on image format guess when the extension is known #13

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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