raisfast 0.2.23

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Local filesystem storage backend.
//!
//! Files are written to `{upload_dir}/{key}`, served via `/uploads/{key}` static file serving.

use async_trait::async_trait;
use std::path::PathBuf;

use crate::errors::app_error::{AppError, AppResult};
use crate::storage::Storage;

/// Local filesystem storage.
#[derive(Debug)]
pub struct LocalStorage {
    base_dir: PathBuf,
    base_url: String,
}

impl LocalStorage {
    /// Create a local storage instance; automatically creates the root directory.
    pub fn new(upload_dir: &str, base_url: &str) -> AppResult<Self> {
        let base_dir = PathBuf::from(upload_dir);
        std::fs::create_dir_all(&base_dir)
            .map_err(|e| AppError::Internal(anyhow::anyhow!("failed to create upload dir: {e}")))?;

        let base_url = base_url.trim_end_matches('/').to_string();
        tracing::info!(base_dir = %base_dir.display(), base_url = %base_url, "LocalStorage initialized");

        Ok(Self { base_dir, base_url })
    }
}

#[async_trait]
impl Storage for LocalStorage {
    async fn put(&self, key: &str, data: &[u8], _content_type: &str) -> AppResult<()> {
        let path = self.base_dir.join(key);
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent)
                .await
                .map_err(|e| AppError::Internal(anyhow::anyhow!("failed to create dir: {e}")))?;
        }
        tokio::fs::write(&path, data)
            .await
            .map_err(|e| AppError::Internal(anyhow::anyhow!("failed to write file: {e}")))?;
        tracing::debug!(key = %key, size = data.len(), "file stored locally");
        Ok(())
    }

    async fn get(&self, key: &str) -> AppResult<Vec<u8>> {
        let path = self.base_dir.join(key);
        tokio::fs::read(&path)
            .await
            .map_err(|e| AppError::Internal(anyhow::anyhow!("failed to read file: {e}")))
    }

    async fn delete(&self, key: &str) -> AppResult<()> {
        let path = self.base_dir.join(key);
        let Err(e) = tokio::fs::remove_file(&path).await else {
            return Ok(());
        };
        if e.kind() != std::io::ErrorKind::NotFound {
            return Err(AppError::Internal(anyhow::anyhow!(
                "failed to delete file: {e}"
            )));
        }
        Ok(())
    }

    async fn url(&self, key: &str) -> AppResult<String> {
        Ok(format!("{}/uploads/{key}", self.base_url))
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use crate::config::app::AppConfig;
    use crate::storage::{self, Storage};

    fn setup_storage() -> (tempfile::TempDir, std::sync::Arc<dyn Storage>) {
        let dir = tempfile::tempdir().expect("create temp dir");
        let base_url = "http://localhost:3000";
        let storage =
            crate::storage::local::LocalStorage::new(dir.path().to_str().unwrap(), base_url)
                .expect("create local storage");
        (dir, std::sync::Arc::new(storage))
    }

    fn setup_config(dir: &tempfile::TempDir) -> AppConfig {
        let mut config = AppConfig::test_defaults();
        config.upload_dir = dir.path().to_str().unwrap().to_string();
        config.base_url = "http://localhost:3000".into();
        config.storage_driver = "local".into();
        config
    }

    // ── LocalStorage basic CRUD ─────────────────────────────────────

    #[tokio::test]
    async fn local_put_and_get() {
        let (_dir, storage) = setup_storage();
        let key = "blog/2026/04/test.jpg";
        let data = b"\xFF\xD8\xFF\xE0test-image-data";

        storage
            .put(key, data, "image/jpeg")
            .await
            .expect("put should succeed");

        let result = storage.get(key).await.expect("get should succeed");
        assert_eq!(result, data);
    }

    #[tokio::test]
    async fn local_put_creates_subdirectories() {
        let (_dir, storage) = setup_storage();
        let key = "blog/2026/04/deep/nested/dir/file.txt";
        let data = b"hello";

        storage
            .put(key, data, "text/plain")
            .await
            .expect("put should create nested dirs");

        let result = storage.get(key).await.expect("get should succeed");
        assert_eq!(result, b"hello");
    }

    #[tokio::test]
    async fn local_put_overwrites_existing() {
        let (_dir, storage) = setup_storage();
        let key = "blog/test.txt";

        storage
            .put(key, b"version1", "text/plain")
            .await
            .expect("put v1");
        storage
            .put(key, b"version2", "text/plain")
            .await
            .expect("put v2");

        let result = storage.get(key).await.expect("get should succeed");
        assert_eq!(result, b"version2");
    }

    #[tokio::test]
    async fn local_delete_removes_file() {
        let (_dir, storage) = setup_storage();
        let key = "blog/to-delete.txt";

        storage.put(key, b"data", "text/plain").await.expect("put");
        storage.delete(key).await.expect("delete should succeed");

        let result = storage.get(key).await;
        assert!(result.is_err(), "file should be deleted");
    }

    #[tokio::test]
    async fn local_delete_nonexistent_is_ok() {
        let (_dir, storage) = setup_storage();
        storage
            .delete("nonexistent/file.txt")
            .await
            .expect("deleting nonexistent file should not error");
    }

    #[tokio::test]
    async fn local_url_format() {
        let (_dir, storage) = setup_storage();
        let url = storage
            .url("blog/2026/04/test.jpg")
            .await
            .expect("url should succeed");
        assert_eq!(url, "http://localhost:3000/uploads/blog/2026/04/test.jpg");
    }

    #[tokio::test]
    async fn local_url_no_trailing_slash_in_base() {
        let dir = tempfile::tempdir().expect("create temp dir");
        let storage = crate::storage::local::LocalStorage::new(
            dir.path().to_str().unwrap(),
            "http://localhost:3000/",
        )
        .expect("create storage with trailing slash");
        let url = storage.url("test.txt").await.expect("url");
        assert_eq!(url, "http://localhost:3000/uploads/test.txt");
    }

    #[tokio::test]
    async fn local_get_nonexistent_returns_error() {
        let (_dir, storage) = setup_storage();
        let result = storage.get("does/not/exist.txt").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn local_presigned_upload_returns_empty() {
        let (_dir, storage) = setup_storage();
        let url = storage
            .presigned_upload("test.txt", Duration::from_secs(300))
            .await
            .expect("presigned should succeed");
        assert!(url.is_empty());
    }

    // ── Concurrent multi-file writes ────────────────────────────────────────────────

    #[tokio::test]
    async fn local_concurrent_puts() {
        let (_dir, storage) = setup_storage();
        let mut handles = Vec::new();

        for i in 0..10 {
            let s = storage.clone();
            handles.push(tokio::spawn(async move {
                let key = format!("concurrent/file_{i}.txt");
                let data = format!("data-{i}").into_bytes();
                s.put(&key, &data, "text/plain").await.expect("put");
                let result = s.get(&key).await.expect("get");
                assert_eq!(result, data);
            }));
        }

        for h in handles {
            h.await.expect("task should complete");
        }
    }

    // ── Large file writes ────────────────────────────────────────────────────

    #[tokio::test]
    async fn local_large_file() {
        let (_dir, storage) = setup_storage();
        let data = vec![0xAB_u8; 10 * 1024 * 1024]; // 10 MB
        let key = "blog/large-file.bin";

        storage
            .put(key, &data, "application/octet-stream")
            .await
            .expect("put large file");

        let result = storage.get(key).await.expect("get large file");
        assert_eq!(result.len(), 10 * 1024 * 1024);
        assert!(result.iter().all(|&b| b == 0xAB));
    }

    // ── Empty files ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn local_empty_file() {
        let (_dir, storage) = setup_storage();
        let key = "blog/empty.txt";

        storage
            .put(key, b"", "text/plain")
            .await
            .expect("put empty");

        let result = storage.get(key).await.expect("get empty");
        assert!(result.is_empty());
    }

    // ── Special character keys ──────────────────────────────────────────────────

    #[tokio::test]
    async fn local_key_with_spaces_and_unicode() {
        let (_dir, storage) = setup_storage();
        let key = "blog/2026/04/你好世界 file name.txt";

        storage
            .put(key, b"unicode", "text/plain")
            .await
            .expect("put with unicode key");

        let result = storage.get(key).await.expect("get unicode key");
        assert_eq!(result, b"unicode");
    }

    // ── create_storage factory function ──────────────────────────────────────

    #[test]
    fn create_storage_local() {
        let dir = tempfile::tempdir().expect("temp dir");
        let config = setup_config(&dir);
        let storage = storage::create_storage(&config).expect("create local storage");

        let key = "factory/test.txt";
        let rt = tokio::runtime::Runtime::new().expect("runtime");
        rt.block_on(async {
            storage
                .put(key, b"factory-test", "text/plain")
                .await
                .expect("put");
            let data = storage.get(key).await.expect("get");
            assert_eq!(data, b"factory-test");
        });
    }

    #[test]
    fn create_storage_rejects_unknown_driver() {
        let dir = tempfile::tempdir().expect("temp dir");
        let mut config = setup_config(&dir);
        config.storage_driver = "ftp".into();

        let result = storage::create_storage(&config);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("unknown STORAGE_DRIVER: ftp"), "error: {err}");
    }

    #[test]
    fn create_storage_rejects_s3_without_feature() {
        #[cfg(not(feature = "storage-s3"))]
        {
            let dir = tempfile::tempdir().expect("temp dir");
            let mut config = setup_config(&dir);
            config.storage_driver = "s3".into();

            let result = storage::create_storage(&config);
            assert!(result.is_err());
            let err = result.unwrap_err().to_string();
            assert!(
                err.contains("storage-s3 feature not enabled"),
                "error: {err}"
            );
        }
    }

    // ── storage_key generation format ─────────────────────────────────────────

    #[test]
    fn storage_key_format() {
        use crate::services::media::storage_key;
        let key = storage_key("blog", "jpg");
        assert!(key.starts_with("blog/"), "key: {key}");
        assert!(key.ends_with(".jpg"), "key: {key}");

        let parts: Vec<&str> = key.split('/').collect();
        assert_eq!(parts.len(), 4, "expected 4 parts: {key}");
        assert_eq!(parts[0], "blog");
        assert!(
            parts[1].len() == 4 && parts[1].parse::<u32>().is_ok(),
            "year part: {}",
            parts[1]
        );
        assert!(
            parts[2].len() == 2 && parts[2].parse::<u32>().is_ok(),
            "month part: {}",
            parts[2]
        );
    }

    // ── mime_to_ext mapping ─────────────────────────────────────────────

    #[test]
    fn mime_to_ext_mapping() {
        use crate::services::media::mime_to_ext;
        // Images
        assert_eq!(mime_to_ext("image/jpeg"), "jpg");
        assert_eq!(mime_to_ext("image/png"), "png");
        assert_eq!(mime_to_ext("image/gif"), "gif");
        assert_eq!(mime_to_ext("image/webp"), "webp");
        assert_eq!(mime_to_ext("image/svg+xml"), "svg");
        // Videos
        assert_eq!(mime_to_ext("video/mp4"), "mp4");
        assert_eq!(mime_to_ext("video/webm"), "webm");
        assert_eq!(mime_to_ext("video/quicktime"), "mov");
        // Audio
        assert_eq!(mime_to_ext("audio/mpeg"), "mp3");
        assert_eq!(mime_to_ext("audio/ogg"), "ogg");
        assert_eq!(mime_to_ext("audio/wav"), "wav");
        assert_eq!(mime_to_ext("audio/aac"), "aac");
        // Documents
        assert_eq!(mime_to_ext("application/pdf"), "pdf");
        assert_eq!(mime_to_ext("application/msword"), "doc");
        assert_eq!(
            mime_to_ext("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
            "docx"
        );
        assert_eq!(mime_to_ext("application/vnd.ms-excel"), "xls");
        assert_eq!(
            mime_to_ext("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
            "xlsx"
        );
        // Archives
        assert_eq!(mime_to_ext("application/zip"), "zip");
        assert_eq!(mime_to_ext("application/x-tar"), "tar");
        assert_eq!(mime_to_ext("application/gzip"), "gz");
        // Text
        assert_eq!(mime_to_ext("text/plain"), "txt");
        assert_eq!(mime_to_ext("text/csv"), "csv");
        assert_eq!(mime_to_ext("text/markdown"), "md");
        // Unknown
        assert_eq!(mime_to_ext("application/unknown"), "bin");
    }

    // ── magic bytes validation ─────────────────────────────────────────────

    #[test]
    fn validate_magic_bytes_jpeg() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("image/jpeg", b"\xFF\xD8\xFF\xE0data"));
    }

    #[test]
    fn validate_magic_bytes_png() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("image/png", b"\x89PNG\r\n\x1a\ndata"));
    }

    #[test]
    fn validate_magic_bytes_gif87a() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("image/gif", b"GIF87a..."));
    }

    #[test]
    fn validate_magic_bytes_gif89a() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("image/gif", b"GIF89a..."));
    }

    #[test]
    fn validate_magic_bytes_webp() {
        use crate::services::media::validate_magic_bytes;
        let mut data = vec![0u8; 12];
        data[0..4].copy_from_slice(b"RIFF");
        data[8..12].copy_from_slice(b"WEBP");
        assert!(validate_magic_bytes("image/webp", &data));
    }

    #[test]
    fn validate_magic_bytes_mp4() {
        use crate::services::media::validate_magic_bytes;
        let mut data = vec![0u8; 32];
        let len = data.len() as u32;
        data[0..4].copy_from_slice(&len.to_be_bytes());
        data[4..8].copy_from_slice(b"ftyp");
        data[8..12].copy_from_slice(b"isom");
        assert!(validate_magic_bytes("video/mp4", &data));
    }

    #[test]
    fn validate_magic_bytes_webm() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes(
            "video/webm",
            b"\x1a\x45\xdf\xa3\x00\x00"
        ));
    }

    #[test]
    fn validate_magic_bytes_pdf() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes(
            "application/pdf",
            b"%PDF-1.7 rest of document"
        ));
    }

    #[test]
    fn validate_magic_bytes_rejects_mismatch() {
        use crate::services::media::validate_magic_bytes;
        assert!(!validate_magic_bytes(
            "image/jpeg",
            b"\x89PNG\r\n\x1a\nfake"
        ));
        assert!(!validate_magic_bytes("image/png", b"\xFF\xD8\xFF\xE0fake"));
    }

    #[test]
    fn validate_magic_bytes_rejects_empty() {
        use crate::services::media::validate_magic_bytes;
        assert!(!validate_magic_bytes("image/jpeg", b""));
        assert!(!validate_magic_bytes("image/png", b""));
    }

    #[test]
    fn validate_magic_bytes_rejects_short_data() {
        use crate::services::media::validate_magic_bytes;
        assert!(!validate_magic_bytes("image/jpeg", b"\xFF"));
        assert!(!validate_magic_bytes("image/png", b"\x89"));
    }

    #[test]
    fn validate_magic_bytes_rejects_unknown_type() {
        use crate::services::media::validate_magic_bytes;
        assert!(!validate_magic_bytes(
            "application/x-executable",
            b"\x7FELF"
        ));
    }

    #[test]
    fn validate_magic_bytes_zip() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("application/zip", b"PK\x03\x04rest"));
    }

    #[test]
    fn validate_magic_bytes_gzip() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes(
            "application/gzip",
            b"\x1F\x8B\x08data"
        ));
    }

    #[test]
    fn validate_magic_bytes_rar() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes(
            "application/x-rar-compressed",
            b"Rar!\x1A\x07rest"
        ));
    }

    #[test]
    fn validate_magic_bytes_mp3_frame() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("audio/mpeg", b"\xFF\xFB\x90data"));
    }

    #[test]
    fn validate_magic_bytes_mp3_id3() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("audio/mpeg", b"ID3\x03tagdata"));
    }

    #[test]
    fn validate_magic_bytes_ogg() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("audio/ogg", b"OggS\x00data"));
    }

    #[test]
    fn validate_magic_bytes_wav() {
        use crate::services::media::validate_magic_bytes;
        let mut data = vec![0u8; 16];
        data[0..4].copy_from_slice(b"RIFF");
        data[8..12].copy_from_slice(b"WAVE");
        assert!(validate_magic_bytes("audio/wav", &data));
    }

    #[test]
    fn validate_magic_bytes_text_types_skip() {
        use crate::services::media::validate_magic_bytes;
        assert!(validate_magic_bytes("text/plain", b"hello"));
        assert!(validate_magic_bytes("text/csv", b"a,b,c"));
        assert!(validate_magic_bytes("text/markdown", b"# heading"));
        assert!(validate_magic_bytes("image/svg+xml", b"<svg>"));
    }

    #[test]
    fn validate_magic_bytes_text_types_reject_empty() {
        use crate::services::media::validate_magic_bytes;
        assert!(!validate_magic_bytes("text/plain", b""));
        assert!(!validate_magic_bytes("text/csv", b""));
    }

    // ── File type whitelist ────────────────────────────────────────────────

    #[test]
    fn allowed_types_includes_image_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"image/jpeg"));
        assert!(ALLOWED_TYPES.contains(&"image/png"));
        assert!(ALLOWED_TYPES.contains(&"image/gif"));
        assert!(ALLOWED_TYPES.contains(&"image/webp"));
        assert!(ALLOWED_TYPES.contains(&"image/svg+xml"));
    }

    #[test]
    fn allowed_types_includes_video_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"video/mp4"));
        assert!(ALLOWED_TYPES.contains(&"video/webm"));
        assert!(ALLOWED_TYPES.contains(&"video/quicktime"));
    }

    #[test]
    fn allowed_types_includes_audio_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"audio/mpeg"));
        assert!(ALLOWED_TYPES.contains(&"audio/ogg"));
        assert!(ALLOWED_TYPES.contains(&"audio/wav"));
        assert!(ALLOWED_TYPES.contains(&"audio/aac"));
    }

    #[test]
    fn allowed_types_includes_document_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"application/pdf"));
        assert!(ALLOWED_TYPES.contains(&"application/msword"));
        assert!(
            ALLOWED_TYPES.contains(
                &"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            )
        );
        assert!(ALLOWED_TYPES.contains(&"application/vnd.ms-excel"));
        assert!(
            ALLOWED_TYPES
                .contains(&"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        );
    }

    #[test]
    fn allowed_types_includes_archive_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"application/zip"));
        assert!(ALLOWED_TYPES.contains(&"application/x-tar"));
        assert!(ALLOWED_TYPES.contains(&"application/gzip"));
        assert!(ALLOWED_TYPES.contains(&"application/x-rar-compressed"));
    }

    #[test]
    fn allowed_types_includes_text_types() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(ALLOWED_TYPES.contains(&"text/plain"));
        assert!(ALLOWED_TYPES.contains(&"text/csv"));
        assert!(ALLOWED_TYPES.contains(&"text/markdown"));
    }

    #[test]
    fn allowed_types_excludes_executables() {
        use crate::services::media::ALLOWED_TYPES;
        assert!(!ALLOWED_TYPES.contains(&"application/x-executable"));
        assert!(!ALLOWED_TYPES.contains(&"application/x-sh"));
    }

    // ── put → get → delete full lifecycle ──────────────────────────────

    #[tokio::test]
    async fn local_lifecycle_put_get_delete() {
        let (_dir, storage) = setup_storage();
        let key = "lifecycle/test.txt";
        let data = b"lifecycle-data";

        storage.put(key, data, "text/plain").await.expect("put");

        let url = storage.url(key).await.expect("url");
        assert!(url.contains(key), "url should contain key: {url}");

        let fetched = storage.get(key).await.expect("get");
        assert_eq!(fetched, data);

        storage.delete(key).await.expect("delete");
        assert!(storage.get(key).await.is_err(), "should be deleted");
    }

    // ── Different bucket isolation ─────────────────────────────────────────────

    #[tokio::test]
    async fn local_different_buckets_isolated() {
        let (_dir, storage) = setup_storage();

        storage
            .put("blog/a.txt", b"blog-data", "text/plain")
            .await
            .expect("put blog");
        storage
            .put("avatar/b.txt", b"avatar-data", "text/plain")
            .await
            .expect("put avatar");
        storage
            .put("product/c.txt", b"product-data", "text/plain")
            .await
            .expect("put product");

        assert_eq!(
            storage.get("blog/a.txt").await.expect("get blog"),
            b"blog-data"
        );
        assert_eq!(
            storage.get("avatar/b.txt").await.expect("get avatar"),
            b"avatar-data"
        );
        assert_eq!(
            storage.get("product/c.txt").await.expect("get product"),
            b"product-data"
        );
    }

    // ── url with different keys ──────────────────────────────────────────────────

    #[tokio::test]
    async fn local_url_various_keys() {
        let (_dir, storage) = setup_storage();

        let u1 = storage.url("a.txt").await.expect("url");
        assert_eq!(u1, "http://localhost:3000/uploads/a.txt");

        let u2 = storage.url("blog/2026/04/img.jpg").await.expect("url");
        assert_eq!(u2, "http://localhost:3000/uploads/blog/2026/04/img.jpg");
    }
}