rustrails-storage 0.1.2

File storage (ActiveStorage equivalent)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Variant transformation requests and caching.

use std::{collections::BTreeMap, time::Duration};

use bytes::Bytes;
use rustrails_support::runtime;
use serde_json::{Map, Value, json};
use thiserror::Error;
use url::Url;

use crate::{
    blob::Blob,
    detect_content_type, replace_extension,
    service::{StorageError, StorageService},
    sha256_hex,
};

/// Errors returned by variant operations.
#[derive(Debug, Error)]
pub enum VariantError {
    /// The blob content type cannot be transformed.
    #[error("blob is not variable: {0}")]
    Invariable(String),
    /// A serialization or transformation error occurred.
    #[error("invalid transformations: {0}")]
    InvalidTransformations(String),
    /// The backing storage service failed.
    #[error(transparent)]
    Storage(#[from] StorageError),
}

/// Describes a transformable blob representation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variant {
    blob: Blob,
    transformations: BTreeMap<String, Value>,
    key: String,
    filename: String,
    content_type: Option<String>,
}

impl Variant {
    /// Creates a new variant request.
    #[must_use]
    pub fn new(blob: Blob, transformations: BTreeMap<String, Value>) -> Self {
        let content_type = determine_content_type(&blob, &transformations);
        let extension = content_type
            .as_deref()
            .and_then(|value| value.rsplit('/').next())
            .unwrap_or_else(|| blob.extension().unwrap_or("bin"));
        let filename = replace_extension(blob.filename(), extension);
        let digest = sha256_hex(canonicalize_transformations(&transformations).to_string());
        let key = format!("variants/{}/{digest}", blob.key());
        Self {
            blob,
            transformations,
            key,
            filename,
            content_type,
        }
    }

    /// Returns the source blob.
    #[must_use]
    pub fn blob(&self) -> &Blob {
        &self.blob
    }

    /// Returns the variant cache key.
    #[must_use]
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Returns the normalized output filename.
    #[must_use]
    pub fn filename(&self) -> &str {
        &self.filename
    }

    /// Returns the output content type.
    #[must_use]
    pub fn content_type(&self) -> Option<&str> {
        self.content_type.as_deref()
    }

    /// Returns the canonical transformations.
    #[must_use]
    pub fn transformations(&self) -> &BTreeMap<String, Value> {
        &self.transformations
    }

    /// Returns whether the source blob can be transformed.
    #[must_use]
    pub fn is_variable(&self) -> bool {
        self.blob.is_image()
    }

    /// Returns whether the variant has already been processed.
    ///
    /// # Errors
    ///
    /// Returns an error when the storage backend existence check fails.
    pub async fn is_processed<S: StorageService + ?Sized>(
        &self,
        service: &S,
    ) -> Result<bool, VariantError> {
        Ok(service.exists(&self.key).await?)
    }

    /// Returns whether the variant has already been processed using the thread-local runtime.
    ///
    /// # Errors
    ///
    /// Returns an error when the storage backend existence check fails.
    pub fn is_processed_sync<S: StorageService + ?Sized>(
        &self,
        service: &S,
    ) -> Result<bool, VariantError> {
        runtime::block_on(self.is_processed(service))
    }

    /// Processes the variant and stores a cached placeholder object when missing.
    ///
    /// # Errors
    ///
    /// Returns an error when the blob cannot be transformed or the storage backend fails.
    pub async fn processed<S: StorageService + ?Sized>(
        &self,
        service: &S,
    ) -> Result<Self, VariantError> {
        if !self.is_variable() {
            return Err(VariantError::Invariable(
                self.blob.content_type().unwrap_or("unknown").to_owned(),
            ));
        }
        if !service.exists(&self.key).await? {
            let payload = json!({
                "source_key": self.blob.key(),
                "filename": self.filename,
                "content_type": self.content_type,
                "transformations": canonicalize_transformations(&self.transformations),
            });
            service
                .upload(&self.key, Bytes::from(payload.to_string().into_bytes()))
                .await?;
        }
        Ok(self.clone())
    }

    /// Processes the variant and stores a cached placeholder object when missing using the thread-local runtime.
    ///
    /// # Errors
    ///
    /// Returns an error when the blob cannot be transformed or the storage backend fails.
    pub fn processed_sync<S: StorageService + ?Sized>(
        &self,
        service: &S,
    ) -> Result<Self, VariantError> {
        runtime::block_on(self.processed(service))
    }

    /// Generates a storage-backed URL for the processed variant.
    ///
    /// # Errors
    ///
    /// Returns an error when the storage backend cannot generate the URL.
    pub async fn url<S: StorageService + ?Sized>(
        &self,
        service: &S,
        expires_in: Duration,
    ) -> Result<Url, VariantError> {
        Ok(service.url(&self.key, expires_in).await?)
    }

    /// Generates a storage-backed URL for the processed variant using the thread-local runtime.
    ///
    /// # Errors
    ///
    /// Returns an error when the storage backend cannot generate the URL.
    pub fn url_sync<S: StorageService + ?Sized>(
        &self,
        service: &S,
        expires_in: Duration,
    ) -> Result<Url, VariantError> {
        runtime::block_on(self.url(service, expires_in))
    }
}

fn canonicalize_transformations(transformations: &BTreeMap<String, Value>) -> Value {
    let mut map = Map::new();
    for (key, value) in transformations {
        map.insert(key.clone(), canonical_value(value));
    }
    Value::Object(map)
}

fn canonical_value(value: &Value) -> Value {
    match value {
        Value::Array(values) => Value::Array(values.iter().map(canonical_value).collect()),
        Value::Object(values) => {
            let mut map = Map::new();
            let mut keys: Vec<_> = values.keys().cloned().collect();
            keys.sort();
            for key in keys {
                map.insert(key.clone(), canonical_value(&values[&key]));
            }
            Value::Object(map)
        }
        _ => value.clone(),
    }
}

fn determine_content_type(
    blob: &Blob,
    transformations: &BTreeMap<String, Value>,
) -> Option<String> {
    transformations
        .get("format")
        .and_then(Value::as_str)
        .and_then(|format| detect_content_type(&format!("file.{format}"), None))
        .or_else(|| blob.content_type().map(ToOwned::to_owned))
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use rustrails_support::runtime;
    use serde_json::json;

    use super::*;
    use crate::{blob::Blob, service::memory::MemoryService, test_support::run_sync_test};

    fn blob(filename: &str, content_type: Option<&str>) -> Blob {
        Blob::create(
            Bytes::from(filename.as_bytes().to_vec()),
            filename.to_owned(),
            content_type,
            BTreeMap::new(),
            "memory",
        )
        .expect("blob should build")
    }

    fn map(pairs: &[(&str, Value)]) -> BTreeMap<String, Value> {
        pairs
            .iter()
            .map(|(key, value)| ((*key).to_owned(), value.clone()))
            .collect()
    }

    #[test]
    fn test_same_transformations_hash_to_same_key() {
        let blob = blob("racecar.jpg", Some("image/jpeg"));
        let first = Variant::new(blob.clone(), map(&[("resize_to_limit", json!([100, 100]))]));
        let second = Variant::new(blob, map(&[("resize_to_limit", json!([100, 100]))]));
        assert_eq!(first.key(), second.key());
    }

    #[test]
    fn test_transformations_order_does_not_change_key() {
        let blob = blob("racecar.jpg", Some("image/jpeg"));
        let first = Variant::new(blob.clone(), map(&[("a", json!(1)), ("b", json!(2))]));
        let second = Variant::new(blob, map(&[("b", json!(2)), ("a", json!(1))]));
        assert_eq!(first.key(), second.key());
    }

    #[test]
    fn test_nested_transformation_order_does_not_change_key() {
        let blob = blob("racecar.jpg", Some("image/jpeg"));
        let first = Variant::new(
            blob.clone(),
            map(&[("resize", json!({"width": 100, "height": 200}))]),
        );
        let second = Variant::new(
            blob,
            map(&[("resize", json!({"height": 200, "width": 100}))]),
        );
        assert_eq!(first.key(), second.key());
    }

    #[test]
    fn test_different_transformations_change_key() {
        let blob = blob("racecar.jpg", Some("image/jpeg"));
        let first = Variant::new(blob.clone(), map(&[("resize_to_limit", json!([100, 100]))]));
        let second = Variant::new(blob, map(&[("resize_to_limit", json!([200, 200]))]));
        assert_ne!(first.key(), second.key());
    }

    #[test]
    fn test_variant_key_is_scoped_to_source_blob_key() {
        let blob = blob("racecar.jpg", Some("image/jpeg"));
        let prefix = format!("variants/{}/", blob.key());

        let variant = Variant::new(blob, BTreeMap::new());

        assert!(variant.key().starts_with(&prefix));
    }

    #[test]
    fn test_format_transformation_updates_filename() {
        let variant = Variant::new(
            blob("racecar.jpg", Some("image/jpeg")),
            map(&[("format", json!("png"))]),
        );
        assert_eq!(variant.filename(), "racecar.png");
        assert_eq!(variant.content_type(), Some("image/png"));
    }

    #[test]
    fn test_format_transformation_normalizes_uppercase_format() {
        let variant = Variant::new(
            blob("racecar.jpg", Some("image/jpeg")),
            map(&[("format", json!("PNG"))]),
        );

        assert_eq!(variant.filename(), "racecar.png");
        assert_eq!(variant.content_type(), Some("image/png"));
    }

    #[test]
    fn test_variant_defaults_to_blob_content_type() {
        let variant = Variant::new(blob("racecar.jpg", Some("image/jpeg")), BTreeMap::new());
        assert_eq!(variant.content_type(), Some("image/jpeg"));
    }

    #[test]
    fn test_variant_uses_blob_content_type_extension_for_extensionless_filename() {
        let variant = Variant::new(blob("image", Some("image/gif")), BTreeMap::new());

        assert_eq!(variant.filename(), "image.gif");
        assert_eq!(variant.content_type(), Some("image/gif"));
    }

    #[test]
    fn test_variant_falls_back_to_bin_extension_without_content_type() {
        let variant = Variant::new(blob("mystery", None), BTreeMap::new());

        assert_eq!(variant.filename(), "mystery.bin");
        assert_eq!(variant.content_type(), None);
    }

    #[tokio::test]
    async fn test_processed_uploads_variant_placeholder() {
        let service = MemoryService::new("memory").expect("service should build");
        let variant = Variant::new(
            blob("racecar.jpg", Some("image/jpeg")),
            map(&[("resize_to_limit", json!([100, 100]))]),
        );
        let processed = variant
            .processed(&service)
            .await
            .expect("processing should succeed");
        assert!(
            service
                .exists(processed.key())
                .await
                .expect("exists should succeed")
        );
    }

    #[tokio::test]
    async fn test_processed_payload_records_canonicalized_fields() {
        let service = MemoryService::new("memory").expect("service should build");
        let source = blob("racecar.jpg", Some("image/jpeg"));
        let variant = Variant::new(
            source.clone(),
            map(&[
                ("resize", json!({"width": 100, "height": 200})),
                ("format", json!("PNG")),
            ]),
        );

        let processed = variant
            .processed(&service)
            .await
            .expect("processing should succeed");
        let payload = service
            .download(processed.key())
            .await
            .expect("download should succeed");
        let payload: Value = serde_json::from_slice(&payload).expect("payload should decode");

        assert_eq!(processed, variant);
        assert_eq!(
            payload,
            json!({
                "source_key": source.key(),
                "filename": "racecar.png",
                "content_type": "image/png",
                "transformations": {
                    "format": "PNG",
                    "resize": {
                        "height": 200,
                        "width": 100
                    }
                }
            })
        );
    }

    #[tokio::test]
    async fn test_processed_is_idempotent() {
        let service = MemoryService::new("memory").expect("service should build");
        let variant = Variant::new(
            blob("racecar.jpg", Some("image/jpeg")),
            map(&[("resize_to_limit", json!([100, 100]))]),
        );
        let _ = variant
            .processed(&service)
            .await
            .expect("processing should succeed");
        let count_before = service.len();
        let _ = variant
            .processed(&service)
            .await
            .expect("processing should succeed");
        assert_eq!(service.len(), count_before);
    }

    #[tokio::test]
    async fn test_processed_rejects_invariable_blob() {
        let service = MemoryService::new("memory").expect("service should build");
        let variant = Variant::new(blob("report.pdf", Some("application/pdf")), BTreeMap::new());
        let error = variant
            .processed(&service)
            .await
            .expect_err("processing should fail");
        assert!(
            matches!(error, VariantError::Invariable(content_type) if content_type == "application/pdf")
        );
    }

    #[tokio::test]
    async fn test_is_processed_reports_state() {
        let service = MemoryService::new("memory").expect("service should build");
        let variant = Variant::new(blob("racecar.jpg", Some("image/jpeg")), BTreeMap::new());
        assert!(
            !variant
                .is_processed(&service)
                .await
                .expect("status should succeed")
        );
        let _ = variant
            .processed(&service)
            .await
            .expect("processing should succeed");
        assert!(
            variant
                .is_processed(&service)
                .await
                .expect("status should succeed")
        );
    }

    #[test]
    fn test_is_processed_sync_reports_state() {
        run_sync_test(|| {
            let service = MemoryService::new("memory").expect("service should build");
            let variant = Variant::new(blob("racecar.jpg", Some("image/jpeg")), BTreeMap::new());
            assert!(
                !variant
                    .is_processed_sync(&service)
                    .expect("status should succeed")
            );
            runtime::block_on(service.upload(variant.key(), Bytes::from_static(b"variant")))
                .expect("upload should succeed");
            assert!(
                variant
                    .is_processed_sync(&service)
                    .expect("status should succeed")
            );
        });
    }

    #[test]
    fn test_processed_sync_uploads_variant_placeholder() {
        run_sync_test(|| {
            let service = MemoryService::new("memory").expect("service should build");
            let variant = Variant::new(
                blob("racecar.jpg", Some("image/jpeg")),
                map(&[("resize_to_limit", json!([100, 100]))]),
            );

            let processed = variant
                .processed_sync(&service)
                .expect("processing should succeed");

            assert!(
                runtime::block_on(service.exists(processed.key())).expect("exists should succeed")
            );
        });
    }

    #[tokio::test]
    async fn test_url_delegates_to_storage_service() {
        let service = MemoryService::new("memory").expect("service should build");
        let variant = Variant::new(blob("racecar.jpg", Some("image/jpeg")), BTreeMap::new());
        let url = variant
            .url(&service, Duration::from_secs(60))
            .await
            .expect("url should build");
        assert!(url.as_str().contains("expires_in=60"));
    }

    #[test]
    fn test_url_sync_delegates_to_storage_service() {
        run_sync_test(|| {
            let service = MemoryService::new("memory").expect("service should build");
            let variant = Variant::new(blob("racecar.jpg", Some("image/jpeg")), BTreeMap::new());
            let url = variant
                .url_sync(&service, Duration::from_secs(60))
                .expect("url should build");
            assert!(url.as_str().contains("expires_in=60"));
        });
    }
}