blossom-rs 0.5.6

Full-featured Blossom (BUD-01) blob storage library for Rust — embeddable server, async client, BIP-340 Nostr auth
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Image processing implementation using the `image` crate.
//!
//! Behind the `media` feature flag. Provides thumbnail generation,
//! blurhash computation, EXIF validation, and perceptual hashing.

use super::{MediaError, MediaProcessor, MediaResult};

/// Image processor using the `image`, `blurhash`, and `kamadak-exif` crates.
pub struct ImageProcessor {
    /// Maximum thumbnail dimension (width or height).
    pub thumbnail_max_size: u32,
    /// Whether to reject images with GPS EXIF data.
    pub reject_gps_exif: bool,
}

impl Default for ImageProcessor {
    fn default() -> Self {
        Self {
            thumbnail_max_size: 200,
            reject_gps_exif: true,
        }
    }
}

impl ImageProcessor {
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom thumbnail size and GPS rejection policy.
    pub fn with_config(thumbnail_max_size: u32, reject_gps_exif: bool) -> Self {
        Self {
            thumbnail_max_size,
            reject_gps_exif,
        }
    }
}

impl MediaProcessor for ImageProcessor {
    fn process(&self, data: &[u8], mime_type: &str) -> Result<MediaResult, MediaError> {
        if !mime_type.starts_with("image/") {
            return Err(MediaError::UnsupportedType(mime_type.to_string()));
        }

        if self.reject_gps_exif {
            self.validate_exif(data)?;
        }

        // Decode image.
        let img = image::load_from_memory(data)
            .map_err(|e| MediaError::ProcessingFailed(format!("decode: {e}")))?;

        let width = img.width();
        let height = img.height();

        // Generate thumbnail.
        let thumb = img.thumbnail(self.thumbnail_max_size, self.thumbnail_max_size);
        let mut thumb_bytes = Vec::new();
        thumb
            .write_to(
                &mut std::io::Cursor::new(&mut thumb_bytes),
                image::ImageFormat::Png,
            )
            .map_err(|e| MediaError::ProcessingFailed(format!("thumbnail: {e}")))?;

        // Compute blurhash.
        let blurhash = self.blurhash(data).ok();

        // Compute perceptual hash.
        let phash = self.perceptual_hash(data).ok();

        Ok(MediaResult {
            data: data.to_vec(),
            mime_type: mime_type.to_string(),
            width: Some(width),
            height: Some(height),
            blurhash,
            thumbnail: Some(thumb_bytes),
            phash,
        })
    }

    fn validate_exif(&self, data: &[u8]) -> Result<(), MediaError> {
        let reader = std::io::Cursor::new(data);
        let exif_reader = exif::Reader::new();
        if let Ok(exif) = exif_reader.read_from_container(&mut std::io::BufReader::new(reader)) {
            for field in exif.fields() {
                let tag_str = format!("{}", field.tag);
                if tag_str.contains("GPS") {
                    return Err(MediaError::SensitiveExif(format!(
                        "GPS data found: {}",
                        field.tag
                    )));
                }
            }
        }
        // No EXIF or no sensitive fields — OK.
        Ok(())
    }

    fn perceptual_hash(&self, data: &[u8]) -> Result<u64, MediaError> {
        let img = image::load_from_memory(data)
            .map_err(|e| MediaError::ProcessingFailed(format!("decode: {e}")))?;

        // Resize to 8x8 grayscale.
        let small = img.resize_exact(8, 8, image::imageops::FilterType::Lanczos3);
        let gray = small.to_luma8();

        // Compute mean.
        let pixels: Vec<u8> = gray.pixels().map(|p| p.0[0]).collect();
        let mean: f64 = pixels.iter().map(|&p| p as f64).sum::<f64>() / pixels.len() as f64;

        // Build hash: bit is 1 if pixel > mean.
        let mut hash: u64 = 0;
        for (i, &pixel) in pixels.iter().enumerate() {
            if pixel as f64 > mean {
                hash |= 1 << (63 - i);
            }
        }

        Ok(hash)
    }

    fn blurhash(&self, data: &[u8]) -> Result<String, MediaError> {
        let img = image::load_from_memory(data)
            .map_err(|e| MediaError::ProcessingFailed(format!("decode: {e}")))?;

        let small = img.resize(32, 32, image::imageops::FilterType::Lanczos3);
        let rgba = small.to_rgba8();
        let w = rgba.width();
        let h = rgba.height();
        let pixels: Vec<u8> = rgba.into_raw();

        let hash = blurhash::encode(4, 3, w, h, &pixels)
            .map_err(|e| MediaError::ProcessingFailed(format!("blurhash: {e}")))?;
        Ok(hash)
    }

    fn thumbnail(
        &self,
        data: &[u8],
        max_width: u32,
        max_height: u32,
    ) -> Result<Vec<u8>, MediaError> {
        let img = image::load_from_memory(data)
            .map_err(|e| MediaError::ProcessingFailed(format!("decode: {e}")))?;

        let thumb = img.thumbnail(max_width, max_height);
        let mut bytes = Vec::new();
        thumb
            .write_to(
                &mut std::io::Cursor::new(&mut bytes),
                image::ImageFormat::Png,
            )
            .map_err(|e| MediaError::ProcessingFailed(format!("thumbnail: {e}")))?;
        Ok(bytes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Generate a simple PNG image programmatically.
    fn make_test_png(width: u32, height: u32, color: [u8; 3]) -> Vec<u8> {
        let img = image::RgbImage::from_fn(width, height, |_x, _y| image::Rgb(color));
        let mut bytes = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut bytes),
            image::ImageFormat::Png,
        )
        .unwrap();
        bytes
    }

    /// Generate a gradient PNG for perceptual hash testing.
    fn make_gradient_png(width: u32, height: u32) -> Vec<u8> {
        let img = image::RgbImage::from_fn(width, height, |x, _y| {
            let v = (x * 255 / width.max(1)) as u8;
            image::Rgb([v, v, v])
        });
        let mut bytes = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut bytes),
            image::ImageFormat::Png,
        )
        .unwrap();
        bytes
    }

    #[test]
    fn test_process_png() {
        let proc = ImageProcessor::new();
        let png = make_test_png(100, 80, [255, 0, 0]);

        let result = proc.process(&png, "image/png").unwrap();
        assert_eq!(result.width, Some(100));
        assert_eq!(result.height, Some(80));
        assert_eq!(result.mime_type, "image/png");
        assert!(result.thumbnail.is_some());
        assert!(result.blurhash.is_some());
        assert!(result.phash.is_some());
        // Thumbnail should be a valid PNG.
        let thumb_data = result.thumbnail.unwrap();
        let thumb = image::load_from_memory(&thumb_data).unwrap();
        assert!(thumb.width() <= 200);
        assert!(thumb.height() <= 200);
    }

    #[test]
    fn test_process_rejects_non_image() {
        let proc = ImageProcessor::new();
        let result = proc.process(b"not an image", "text/plain");
        assert!(matches!(result, Err(MediaError::UnsupportedType(_))));
    }

    #[test]
    fn test_process_rejects_corrupt_image() {
        let proc = ImageProcessor::new();
        let result = proc.process(b"not valid png data", "image/png");
        assert!(matches!(result, Err(MediaError::ProcessingFailed(_))));
    }

    #[test]
    fn test_thumbnail_respects_max_size() {
        let proc = ImageProcessor::with_config(50, false);
        let png = make_test_png(400, 300, [0, 128, 255]);

        let thumb_bytes = proc.thumbnail(&png, 50, 50).unwrap();
        // Verify the thumbnail is a valid image with correct dimensions.
        let thumb = image::load_from_memory(&thumb_bytes).unwrap();
        assert!(thumb.width() <= 50);
        assert!(thumb.height() <= 50);
    }

    #[test]
    fn test_thumbnail_preserves_aspect_ratio() {
        let proc = ImageProcessor::new();
        let png = make_test_png(200, 100, [0, 0, 0]);

        let thumb_bytes = proc.thumbnail(&png, 100, 100).unwrap();
        let thumb = image::load_from_memory(&thumb_bytes).unwrap();
        // 200x100 → 100x50 (aspect ratio preserved).
        assert_eq!(thumb.width(), 100);
        assert_eq!(thumb.height(), 50);
    }

    #[test]
    fn test_blurhash_produces_valid_string() {
        let proc = ImageProcessor::new();
        let png = make_test_png(64, 64, [100, 150, 200]);

        let hash = proc.blurhash(&png).unwrap();
        assert!(!hash.is_empty());
        // Blurhash strings are typically 20-30 chars.
        assert!(hash.len() > 5);
        assert!(hash.len() < 100);
    }

    #[test]
    fn test_blurhash_deterministic() {
        let proc = ImageProcessor::new();
        let png = make_test_png(32, 32, [50, 100, 150]);

        let hash1 = proc.blurhash(&png).unwrap();
        let hash2 = proc.blurhash(&png).unwrap();
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_perceptual_hash_deterministic() {
        let proc = ImageProcessor::new();
        let png = make_test_png(100, 100, [128, 64, 32]);

        let h1 = proc.perceptual_hash(&png).unwrap();
        let h2 = proc.perceptual_hash(&png).unwrap();
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_perceptual_hash_similar_images() {
        let proc = ImageProcessor::new();
        // Two nearly identical images — should have similar hashes.
        let png1 = make_test_png(100, 100, [128, 64, 32]);
        let png2 = make_test_png(100, 100, [130, 66, 34]); // Slightly different color.

        let h1 = proc.perceptual_hash(&png1).unwrap();
        let h2 = proc.perceptual_hash(&png2).unwrap();

        // Hamming distance should be small for similar images.
        let hamming = (h1 ^ h2).count_ones();
        assert!(
            hamming < 10,
            "hamming distance {} too large for similar images",
            hamming
        );
    }

    #[test]
    fn test_perceptual_hash_different_images() {
        let proc = ImageProcessor::new();
        // Solid black vs gradient — should have very different hashes.
        let black = make_test_png(100, 100, [0, 0, 0]);
        let gradient = make_gradient_png(100, 100);

        let h1 = proc.perceptual_hash(&black).unwrap();
        let h2 = proc.perceptual_hash(&gradient).unwrap();

        let hamming = (h1 ^ h2).count_ones();
        assert!(
            hamming > 10,
            "hamming distance {} too small for different images",
            hamming
        );
    }

    #[test]
    fn test_perceptual_hash_scale_invariant() {
        let proc = ImageProcessor::new();
        // Same color, different sizes — phash is scale-invariant for solid colors.
        let small = make_test_png(50, 50, [200, 100, 50]);
        let large = make_test_png(500, 500, [200, 100, 50]);

        let h1 = proc.perceptual_hash(&small).unwrap();
        let h2 = proc.perceptual_hash(&large).unwrap();
        assert_eq!(
            h1, h2,
            "phash should be identical for same solid color at different sizes"
        );
    }

    #[test]
    fn test_validate_exif_no_exif_passes() {
        let proc = ImageProcessor::new();
        let png = make_test_png(10, 10, [0, 0, 0]);
        // PNGs generated by the image crate have no EXIF data.
        proc.validate_exif(&png).unwrap();
    }

    #[test]
    fn test_validate_exif_non_image_passes() {
        let proc = ImageProcessor::new();
        // Random bytes — no EXIF container found, should pass.
        proc.validate_exif(b"not an image at all").unwrap();
    }

    #[test]
    fn test_process_with_gps_rejection_disabled() {
        let proc = ImageProcessor::with_config(100, false);
        let png = make_test_png(10, 10, [0, 0, 0]);
        // Should work fine with GPS rejection off.
        let result = proc.process(&png, "image/png").unwrap();
        assert_eq!(result.width, Some(10));
    }

    #[test]
    fn test_process_full_pipeline() {
        let proc = ImageProcessor::new();
        let png = make_test_png(256, 128, [64, 128, 192]);

        let result = proc.process(&png, "image/png").unwrap();

        // Dimensions.
        assert_eq!(result.width, Some(256));
        assert_eq!(result.height, Some(128));

        // Thumbnail generated and valid.
        let thumb_data = result.thumbnail.unwrap();
        let thumb = image::load_from_memory(&thumb_data).unwrap();
        assert!(thumb.width() <= 200);
        assert!(thumb.height() <= 200);

        // Blurhash present.
        let bh = result.blurhash.unwrap();
        assert!(!bh.is_empty());

        // Phash present.
        assert!(result.phash.is_some());
    }

    #[test]
    fn test_process_small_image() {
        let proc = ImageProcessor::new();
        // Image smaller than thumbnail max — should still work.
        let png = make_test_png(5, 5, [255, 255, 255]);

        let result = proc.process(&png, "image/png").unwrap();
        assert_eq!(result.width, Some(5));
        assert_eq!(result.height, Some(5));
        assert!(result.thumbnail.is_some());
    }

    #[test]
    fn test_process_large_image() {
        let proc = ImageProcessor::with_config(100, false);
        let png = make_test_png(1000, 800, [10, 20, 30]);

        let result = proc.process(&png, "image/png").unwrap();
        assert_eq!(result.width, Some(1000));
        assert_eq!(result.height, Some(800));

        // Thumbnail should be within bounds.
        let thumb = image::load_from_memory(&result.thumbnail.unwrap()).unwrap();
        assert!(thumb.width() <= 100);
        assert!(thumb.height() <= 100);
    }

    #[test]
    fn test_jpeg_format() {
        let proc = ImageProcessor::new();
        // Generate a JPEG.
        let img = image::RgbImage::from_fn(50, 50, |_, _| image::Rgb([100, 150, 200]));
        let mut bytes = Vec::new();
        img.write_to(
            &mut std::io::Cursor::new(&mut bytes),
            image::ImageFormat::Jpeg,
        )
        .unwrap();

        let result = proc.process(&bytes, "image/jpeg").unwrap();
        assert_eq!(result.width, Some(50));
        assert_eq!(result.height, Some(50));
        assert!(result.thumbnail.is_some());
        assert!(result.blurhash.is_some());
    }
}