-
Notifications
You must be signed in to change notification settings - Fork 7.3k
fix: encoding of image URL #8964
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use std::num::NonZeroUsize; | ||
| use std::path::Path; | ||
| use std::path::PathBuf; | ||
| use std::sync::LazyLock; | ||
|
|
||
| use crate::error::ImageProcessingError; | ||
|
|
@@ -30,6 +31,12 @@ pub struct EncodedImage { | |
| pub height: u32, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct InlineImageData { | ||
| pub mime: Option<String>, | ||
| pub bytes: Vec<u8>, | ||
| } | ||
|
|
||
| impl EncodedImage { | ||
| pub fn into_data_url(self) -> String { | ||
| let encoded = BASE64_STANDARD.encode(&self.bytes); | ||
|
|
@@ -45,6 +52,84 @@ pub fn load_and_resize_to_fit(path: &Path) -> Result<EncodedImage, ImageProcessi | |
|
|
||
| let file_bytes = read_file_bytes(path, &path_buf)?; | ||
|
|
||
| process_image_bytes(file_bytes, path_buf) | ||
| } | ||
|
|
||
| pub fn load_and_resize_bytes( | ||
| bytes: Vec<u8>, | ||
| path_for_error: PathBuf, | ||
| ) -> Result<EncodedImage, ImageProcessingError> { | ||
| process_image_bytes(bytes, path_for_error) | ||
| } | ||
|
|
||
| pub fn decode_data_url(image_url: &str) -> Result<InlineImageData, ImageProcessingError> { | ||
| let Some(rest) = image_url.strip_prefix("data:") else { | ||
| return Err(ImageProcessingError::DataUrl { | ||
| message: "missing data URL prefix".to_string(), | ||
| }); | ||
| }; | ||
| let Some((header, data)) = rest.split_once(',') else { | ||
| return Err(ImageProcessingError::DataUrl { | ||
| message: "missing data URL header separator".to_string(), | ||
| }); | ||
| }; | ||
|
|
||
| let mut mime = None; | ||
| let mut is_base64 = false; | ||
|
|
||
| let mut parts = header.split(';'); | ||
| if let Some(first) = parts.next() | ||
| && !first.is_empty() | ||
| { | ||
| mime = Some(first.to_string()); | ||
| } | ||
| for part in parts { | ||
| if part == "base64" { | ||
| is_base64 = true; | ||
| } | ||
| } | ||
|
|
||
| if !is_base64 { | ||
| return Err(ImageProcessingError::DataUrl { | ||
| message: "data URL is not base64 encoded".to_string(), | ||
| }); | ||
|
Comment on lines
+92
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This change rejects any Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| let decoded = | ||
| BASE64_STANDARD | ||
| .decode(data.trim()) | ||
| .map_err(|err| ImageProcessingError::DataUrl { | ||
| message: format!("invalid base64 image data: {err}"), | ||
| })?; | ||
|
|
||
| Ok(InlineImageData { | ||
| mime, | ||
| bytes: decoded, | ||
| }) | ||
| } | ||
|
|
||
| fn read_file_bytes(path: &Path, path_for_error: &Path) -> Result<Vec<u8>, ImageProcessingError> { | ||
| match tokio::runtime::Handle::try_current() { | ||
| // If we're inside a Tokio runtime, avoid block_on (it panics on worker threads). | ||
| // Use block_in_place and do a standard blocking read safely. | ||
| Ok(_) => tokio::task::block_in_place(|| std::fs::read(path)).map_err(|source| { | ||
| ImageProcessingError::Read { | ||
| path: path_for_error.to_path_buf(), | ||
| source, | ||
| } | ||
| }), | ||
| // Outside a runtime, just read synchronously. | ||
| Err(_) => std::fs::read(path).map_err(|source| ImageProcessingError::Read { | ||
| path: path_for_error.to_path_buf(), | ||
| source, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| fn process_image_bytes( | ||
| file_bytes: Vec<u8>, | ||
| path_for_error: PathBuf, | ||
| ) -> Result<EncodedImage, ImageProcessingError> { | ||
| let key = sha1_digest(&file_bytes); | ||
|
|
||
| IMAGE_CACHE.get_or_try_insert_with(key, move || { | ||
|
|
@@ -56,7 +141,7 @@ pub fn load_and_resize_to_fit(path: &Path) -> Result<EncodedImage, ImageProcessi | |
|
|
||
| let dynamic = image::load_from_memory(&file_bytes).map_err(|source| { | ||
| ImageProcessingError::Decode { | ||
| path: path_buf.clone(), | ||
| path: path_for_error.clone(), | ||
| source, | ||
| } | ||
| })?; | ||
|
|
@@ -99,24 +184,6 @@ pub fn load_and_resize_to_fit(path: &Path) -> Result<EncodedImage, ImageProcessi | |
| }) | ||
| } | ||
|
|
||
| fn read_file_bytes(path: &Path, path_for_error: &Path) -> Result<Vec<u8>, ImageProcessingError> { | ||
| match tokio::runtime::Handle::try_current() { | ||
| // If we're inside a Tokio runtime, avoid block_on (it panics on worker threads). | ||
| // Use block_in_place and do a standard blocking read safely. | ||
| Ok(_) => tokio::task::block_in_place(|| std::fs::read(path)).map_err(|source| { | ||
| ImageProcessingError::Read { | ||
| path: path_for_error.to_path_buf(), | ||
| source, | ||
| } | ||
| }), | ||
| // Outside a runtime, just read synchronously. | ||
| Err(_) => std::fs::read(path).map_err(|source| ImageProcessingError::Read { | ||
| path: path_for_error.to_path_buf(), | ||
| source, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| fn encode_image( | ||
| image: &DynamicImage, | ||
| preferred_format: ImageFormat, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.