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,12 +183,21 @@ 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(
async move { async_fetch_img_from_disk(ctx, url, &path, cache_type).await },
)
}
async fn async_fetch_img_from_disk(
ctx: egui::Context,
url: String,
path: &path::Path,
cache_type: MediaCacheType,
) -> Result<TexturedImage, notedeck::Error> {
match cache_type { match cache_type {
MediaCacheType::Image => { MediaCacheType::Image => {
let data = fs::read(path).await?; let data = fs::read(path).await?;
let image_buffer = let image_buffer = image::load_from_memory(&data).map_err(notedeck::Error::Image)?;
image::load_from_memory(&data).map_err(notedeck::Error::Image)?;
let img = buffer_to_color_image( let img = buffer_to_color_image(
image_buffer.as_flat_samples_u8(), image_buffer.as_flat_samples_u8(),
@@ -202,13 +211,12 @@ fn fetch_img_from_disk(
))) )))
} }
MediaCacheType::Gif => { MediaCacheType::Gif => {
let gif_bytes = fs::read(path.clone()).await?; // Read entire file into a Vec<u8> let gif_bytes = fs::read(path).await?; // Read entire file into a Vec<u8>
generate_gif(ctx, url, &path, gif_bytes, false, |i| { generate_gif(ctx, url, path, gif_bytes, false, |i| {
buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height()) buffer_to_color_image(i.as_flat_samples_u8(), i.width(), i.height())
}) })
} }
} }
})
} }
fn generate_gif( fn generate_gif(