-
|
I'm writing a backend service that primarily relies on I tried to do this: // use std::{collections::HashMap, io::ErrorKind, path::PathBuf};
// use tokio::{
// fs::File,
// io::{BufReader, Error, Result},
// task::spawn_blocking,
// };
// use tokio_util::io::SyncIoBridge;
async fn get_image_dimensions(
&self,
prefix: &str,
identifier: &str,
) -> Result<(u32, u32)> {
let file_path: PathBuf = self.get_path(prefix, identifier)?;
let file = File::open(&file_path).await?;
let sync_reader = SyncIoBridge::new(BufReader::new(file));
spawn_blocking(move || {
let img_reader = ImageReader::new(sync_reader);
img_reader.into_dimensions().map_err(|e| {
Error::other(format!("Unable to get image dimensions: {e}"))
})
})
.await?
}but I get this: The image is a PNG file that opens successfully with ImageMagick's This method works with the same file in the non-tokio, sync implementation. For just decoding the entire image, especially in the case of this PNG, it would be simple enough to just read the whole file to a buffer and then feed it to |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
|
Hey, it actually works if I bypass the format detection: - let img_reader = ImageReader::new(sync_reader);
+ let mut img_reader = ImageReader::new(sync_reader);
+ img_reader.set_format(image::ImageFormat::Png);I.e. my server responds with a 200 OK and expected response body. |
Beta Was this translation helpful? Give feedback.
-
|
Wait a minute, does the file format detection just look at the filename extension? And the way I'm doing this here, |
Beta Was this translation helpful? Give feedback.
-
|
There's two methods of format detection. The default is via file extension, but you can also run |
Beta Was this translation helpful? Give feedback.
-
|
Hell yeah, it works like this: async fn get_image_dimensions(
&self,
prefix: &str,
identifier: &str,
) -> Result<(u32, u32)> {
let file_path = self.get_path(prefix, identifier)?;
let file = File::open(&file_path).await?;
let sync_reader = SyncIoBridge::new(BufReader::new(file));
spawn_blocking(move || {
ImageFormat::from_path(&file_path)
.map_err(|e| Error::other(e))
.and_then(|fmt| {
let img_reader = ImageReader::with_format(sync_reader, fmt);
img_reader.into_dimensions().map_err(|e| {
Error::other(format!(
"Unable to get image dimensions: {e}"
))
})
})
})
.await?
}Sorry for the noise, user error! 😄 |
Beta Was this translation helpful? Give feedback.
Hell yeah, it works like this: