images: move fetch to fn

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-04-29 10:37:47 -04:00
parent a4ec0982d2
commit faec75e1b6

View File

@@ -183,32 +183,40 @@ fn fetch_img_from_disk(
let url = url.to_owned(); let url = url.to_owned();
let path = path.to_owned(); let path = path.to_owned();
Promise::spawn_async(async move { Promise::spawn_async(
match cache_type { async move { async_fetch_img_from_disk(ctx, url, &path, cache_type).await },
MediaCacheType::Image => { )
let data = fs::read(path).await?; }
let image_buffer =
image::load_from_memory(&data).map_err(notedeck::Error::Image)?;
let img = buffer_to_color_image( async fn async_fetch_img_from_disk(
image_buffer.as_flat_samples_u8(), ctx: egui::Context,
image_buffer.width(), url: String,
image_buffer.height(), path: &path::Path,
); cache_type: MediaCacheType,
Ok(TexturedImage::Static(ctx.load_texture( ) -> Result<TexturedImage, notedeck::Error> {
&url, match cache_type {
img, MediaCacheType::Image => {
Default::default(), let data = fs::read(path).await?;
))) let image_buffer = image::load_from_memory(&data).map_err(notedeck::Error::Image)?;
}
MediaCacheType::Gif => { let img = buffer_to_color_image(
let gif_bytes = fs::read(path.clone()).await?; // Read entire file into a Vec<u8> image_buffer.as_flat_samples_u8(),
generate_gif(ctx, url, &path, gif_bytes, false, |i| { image_buffer.width(),
buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height()) image_buffer.height(),
}) );
} Ok(TexturedImage::Static(ctx.load_texture(
&url,
img,
Default::default(),
)))
} }
}) MediaCacheType::Gif => {
let gif_bytes = fs::read(path).await?; // Read entire file into a Vec<u8>
generate_gif(ctx, url, path, gif_bytes, false, |i| {
buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height())
})
}
}
} }
fn generate_gif( fn generate_gif(