feat: compute thumbhash for images (#596)

* feat: compute thumbhash for images

Signed-off-by: newt <hi@newty.dev>

* Merge branch 'main' into feat/thumbhash

Signed-off-by: newt <hi@newty.dev>

* style: move comment onto newline

Signed-off-by: newt <hi@newty.dev>

* feat: make thumbhash optional

Signed-off-by: newt <hi@newty.dev>

---------

Signed-off-by: newt <hi@newty.dev>
Signed-off-by: newt (: <hi@newty.dev>
Signed-off-by: Tom <iamtomahawkx@gmail.com>
Co-authored-by: Tom <iamtomahawkx@gmail.com>
This commit is contained in:
newt (
2026-03-28 00:29:55 +00:00
committed by GitHub
parent 91783b9066
commit c2d4369e16
7 changed files with 34 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ jxl-oxide = "0.8.1"
kamadak-exif = "0.5.4"
# revolt_little_exif = "0.5.1"
image = { version = "0.25.2" } # avif encode requires dav1d system library: features = ["avif-native"]
thumbhash = "0.1.0"
# File processing
revolt_clamav-client = { version = "0.1.5" }

View File

@@ -19,6 +19,7 @@ pub async fn strip_metadata(
Metadata::Image {
width,
height,
thumbhash,
animated,
} => match mime {
// // little_exif does not appear to parse JPEGs correctly? had 2/2 files fail
@@ -102,6 +103,7 @@ pub async fn strip_metadata(
Metadata::Image {
width,
height,
thumbhash: thumbhash.clone(),
animated: *animated,
},
))

View File

@@ -1,5 +1,6 @@
use std::io::Cursor;
use image::{GenericImageView, ImageError, ImageReader};
use revolt_database::Metadata;
use revolt_files::{image_size, is_animated, video_size};
use tempfile::NamedTempFile;
@@ -26,6 +27,16 @@ pub fn generate_metadata(f: &NamedTempFile, mime_type: &str) -> Metadata {
.map(|(width, height)| Metadata::Image {
width: width as isize,
height: height as isize,
thumbhash: ImageReader::open(f)
.and_then(|r| r.with_guessed_format())
.map_err(ImageError::from)
.and_then(|r| r.decode())
.map(|img| img.thumbnail(100, 100))
.map(|img| (img.dimensions(), img.to_rgba8().into_raw()))
.map(|((width, height), rgba)| {
thumbhash::rgba_to_thumb_hash(width as usize, height as usize, &rgba)
})
.ok(),
animated: is_animated(f, mime_type).unwrap_or(false),
})
.unwrap_or_default()