From 7d1aa21cdd09401845d821e473e121cfb6dbaa01 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 12 Aug 2025 21:51:22 +0300 Subject: [PATCH] db: switch from `image` to `imagesize` Signed-off-by: NotAShelf Change-Id: I6a6a6964ab449f38999b5036d6e5554ccae7c049 --- src/db/mod.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 1beafbf..97d0ad0 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,7 +2,7 @@ use std::fmt; use std::io::{BufRead, BufReader, Read, Write}; use std::str; -use image::{GenericImageView, ImageFormat}; +use imagesize::{ImageSize, ImageType}; use log::{error, info}; use rusqlite::{Connection, OptionalExtension, params}; @@ -325,23 +325,19 @@ pub fn extract_id(input: &str) -> Result { } pub fn detect_mime(data: &[u8]) -> Option { - if image::guess_format(data).is_ok() { - match image::guess_format(data) { - Ok(fmt) => Some( - match fmt { - ImageFormat::Png => "image/png", - ImageFormat::Jpeg => "image/jpeg", - ImageFormat::Gif => "image/gif", - ImageFormat::Bmp => "image/bmp", - ImageFormat::Tiff => "image/tiff", - _ => "application/octet-stream", - } - .to_string(), - ), - Err(_) => None, - } - } else if data.is_ascii() { - Some("text/plain".into()) + if let Ok(img_type) = imagesize::image_type(data) { + Some( + match img_type { + ImageType::Png => "image/png", + ImageType::Jpeg => "image/jpeg", + ImageType::Gif => "image/gif", + ImageType::Bmp => "image/bmp", + ImageType::Tiff => "image/tiff", + ImageType::Webp => "image/webp", + _ => "application/octet-stream", + } + .to_string(), + ) } else { None } @@ -350,14 +346,17 @@ pub fn detect_mime(data: &[u8]) -> Option { pub fn preview_entry(data: &[u8], mime: Option<&str>, width: u32) -> String { if let Some(mime) = mime { if mime.starts_with("image/") { - if let Ok(img) = image::load_from_memory(data) { - let (w, h) = img.dimensions(); + if let Ok(ImageSize { + width: img_width, + height: img_height, + }) = imagesize::blob_size(data) + { return format!( "[[ binary data {} {} {}x{} ]]", size_str(data.len()), mime, - w, - h + img_width, + img_height ); } } else if mime == "application/json" || mime.starts_with("text/") {