db: switch from image to imagesize

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I6a6a6964ab449f38999b5036d6e5554ccae7c049
This commit is contained in:
raf 2025-08-12 21:51:22 +03:00
commit 7d1aa21cdd
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -2,7 +2,7 @@ use std::fmt;
use std::io::{BufRead, BufReader, Read, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::str; use std::str;
use image::{GenericImageView, ImageFormat}; use imagesize::{ImageSize, ImageType};
use log::{error, info}; use log::{error, info};
use rusqlite::{Connection, OptionalExtension, params}; use rusqlite::{Connection, OptionalExtension, params};
@ -325,23 +325,19 @@ pub fn extract_id(input: &str) -> Result<u64, &'static str> {
} }
pub fn detect_mime(data: &[u8]) -> Option<String> { pub fn detect_mime(data: &[u8]) -> Option<String> {
if image::guess_format(data).is_ok() { if let Ok(img_type) = imagesize::image_type(data) {
match image::guess_format(data) { Some(
Ok(fmt) => Some( match img_type {
match fmt { ImageType::Png => "image/png",
ImageFormat::Png => "image/png", ImageType::Jpeg => "image/jpeg",
ImageFormat::Jpeg => "image/jpeg", ImageType::Gif => "image/gif",
ImageFormat::Gif => "image/gif", ImageType::Bmp => "image/bmp",
ImageFormat::Bmp => "image/bmp", ImageType::Tiff => "image/tiff",
ImageFormat::Tiff => "image/tiff", ImageType::Webp => "image/webp",
_ => "application/octet-stream", _ => "application/octet-stream",
} }
.to_string(), .to_string(),
), )
Err(_) => None,
}
} else if data.is_ascii() {
Some("text/plain".into())
} else { } else {
None None
} }
@ -350,14 +346,17 @@ pub fn detect_mime(data: &[u8]) -> Option<String> {
pub fn preview_entry(data: &[u8], mime: Option<&str>, width: u32) -> String { pub fn preview_entry(data: &[u8], mime: Option<&str>, width: u32) -> String {
if let Some(mime) = mime { if let Some(mime) = mime {
if mime.starts_with("image/") { if mime.starts_with("image/") {
if let Ok(img) = image::load_from_memory(data) { if let Ok(ImageSize {
let (w, h) = img.dimensions(); width: img_width,
height: img_height,
}) = imagesize::blob_size(data)
{
return format!( return format!(
"[[ binary data {} {} {}x{} ]]", "[[ binary data {} {} {}x{} ]]",
size_str(data.len()), size_str(data.len()),
mime, mime,
w, img_width,
h img_height
); );
} }
} else if mime == "application/json" || mime.starts_with("text/") { } else if mime == "application/json" || mime.starts_with("text/") {