reinhardt-utils 0.3.15

Utility functions aggregator for Reinhardt
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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! Local filesystem storage backend

use super::backend::Storage;
use super::errors::{StorageError, StorageResult};
use super::file::{FileMetadata, StoredFile};
use async_trait::async_trait;
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
#[cfg(not(target_arch = "wasm32"))]
use std::sync::{Arc, OnceLock};
#[cfg(target_arch = "wasm32")]
use tokio::fs;

/// Local filesystem storage
pub struct LocalStorage {
	base_path: PathBuf,
	base_url: String,
	#[cfg(not(target_arch = "wasm32"))]
	base_dir: Arc<OnceLock<Arc<cap_std::fs::Dir>>>,
}

impl LocalStorage {
	/// Create a new local filesystem storage backend
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_utils::storage::{LocalStorage, Storage};
	///
	/// let storage = LocalStorage::new("/tmp/storage", "http://localhost/media");
	/// assert_eq!(storage.url("test.txt"), "http://localhost/media/test.txt");
	/// ```
	pub fn new(base_path: impl Into<PathBuf>, base_url: impl Into<String>) -> Self {
		Self {
			base_path: base_path.into(),
			base_url: base_url.into(),
			#[cfg(not(target_arch = "wasm32"))]
			base_dir: Arc::new(OnceLock::new()),
		}
	}
	/// Ensure the base directory exists, creating it if necessary
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_utils::storage::LocalStorage;
	/// use tempfile::TempDir;
	///
	/// # tokio_test::block_on(async {
	/// let temp_dir = TempDir::new().unwrap();
	/// let storage_path = temp_dir.path().join("new_storage");
	/// let storage = LocalStorage::new(&storage_path, "http://localhost/media");
	///
	/// storage.ensure_base_dir().await.unwrap();
	/// assert!(storage_path.exists());
	/// # });
	/// ```
	pub async fn ensure_base_dir(&self) -> StorageResult<()> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			self.with_base_dir(true, |_| Ok(())).await
		}
		#[cfg(target_arch = "wasm32")]
		{
			fs::create_dir_all(&self.base_path).await?;
			Ok(())
		}
	}

	#[cfg(target_arch = "wasm32")]
	fn full_path(&self, path: &str) -> StorageResult<PathBuf> {
		Self::validate_path(path)?;
		crate::safe_path_join(&self.base_path, path)
			.map_err(|error| StorageError::InvalidPath(error.to_string()))
	}

	#[cfg(not(target_arch = "wasm32"))]
	async fn with_base_dir<T, F>(&self, create: bool, operation: F) -> StorageResult<T>
	where
		T: Send + 'static,
		F: FnOnce(cap_std::fs::Dir) -> StorageResult<T> + Send + 'static,
	{
		let base_path = self.base_path.clone();
		let base_dir = self.base_dir.clone();
		tokio::task::spawn_blocking(move || {
			if base_dir.get().is_none() {
				if create {
					std::fs::create_dir_all(&base_path)?;
				}
				let opened = Arc::new(cap_std::fs::Dir::open_ambient_dir(
					&base_path,
					cap_std::ambient_authority(),
				)?);
				let _ = base_dir.set(opened);
			}
			let directory = base_dir
				.get()
				.expect("storage directory is initialized")
				.try_clone()?;
			operation(directory)
		})
		.await
		.map_err(|error| StorageError::Io(std::io::Error::other(error.to_string())))?
	}

	/// Validate path to prevent directory traversal attacks
	fn validate_path(path: &str) -> StorageResult<()> {
		// Check for absolute paths
		if path.starts_with('/') || path.starts_with('\\') {
			return Err(StorageError::InvalidPath(format!(
				"Detected path traversal attempt in '{}'",
				path
			)));
		}

		// Check for parent directory references
		let path_obj = Path::new(path);
		for component in path_obj.components() {
			if component == std::path::Component::ParentDir {
				return Err(StorageError::InvalidPath(format!(
					"Detected path traversal attempt in '{}'",
					path
				)));
			}
		}

		// Check if path is just current dir or empty
		if path == "." || path == ".." || path.is_empty() {
			return Err(StorageError::InvalidPath(format!(
				"Could not derive file name from '{}'",
				path
			)));
		}

		Ok(())
	}

	fn compute_checksum(content: &[u8]) -> String {
		let mut hasher = Sha256::new();
		hasher.update(content);
		hex::encode(hasher.finalize())
	}
}

#[async_trait]
impl Storage for LocalStorage {
	async fn save(&self, path: &str, content: &[u8]) -> StorageResult<FileMetadata> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			let stored_path = path.to_string();
			let content = content.to_vec();
			return self
				.with_base_dir(true, move |directory| {
					use std::io::Write;
					if let Some(parent) = relative_path.parent()
						&& !parent.as_os_str().is_empty()
					{
						directory.create_dir_all(parent)?;
					}
					let mut file = directory.create(&relative_path)?;
					file.write_all(&content)?;
					let size = file.metadata()?.len();
					Ok(FileMetadata::new(stored_path, size)
						.with_checksum(Self::compute_checksum(&content)))
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			// Create parent directories if needed
			if let Some(parent) = full_path.parent() {
				fs::create_dir_all(parent).await?;
			}

			// Write file
			fs::write(&full_path, content).await?;

			// Get file metadata
			let file_meta = fs::metadata(&full_path).await?;
			let size = file_meta.len();
			let checksum = Self::compute_checksum(content);

			Ok(FileMetadata::new(path.to_string(), size).with_checksum(checksum))
		}
	}

	async fn read(&self, path: &str) -> StorageResult<StoredFile> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			let stored_path = path.to_string();
			return self
				.with_base_dir(false, move |directory| {
					use std::io::Read;
					let mut file = directory.open(&relative_path).map_err(|error| {
						if error.kind() == std::io::ErrorKind::NotFound {
							StorageError::NotFound(stored_path.clone())
						} else {
							StorageError::Io(error)
						}
					})?;
					let mut content = Vec::new();
					file.read_to_end(&mut content)?;
					let metadata = FileMetadata::new(stored_path, file.metadata()?.len());
					Ok(StoredFile::new(metadata, content))
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			let content = fs::read(&full_path).await?;
			let file_meta = fs::metadata(&full_path).await?;
			let size = file_meta.len();

			let metadata = FileMetadata::new(path.to_string(), size);
			Ok(StoredFile::new(metadata, content))
		}
	}

	async fn delete(&self, path: &str) -> StorageResult<()> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			let stored_path = path.to_string();
			return self
				.with_base_dir(false, move |directory| {
					directory.remove_file(relative_path).map_err(|error| {
						if error.kind() == std::io::ErrorKind::NotFound {
							StorageError::NotFound(stored_path)
						} else {
							StorageError::Io(error)
						}
					})
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			fs::remove_file(&full_path).await?;
			Ok(())
		}
	}

	async fn exists(&self, path: &str) -> StorageResult<bool> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			return self
				.with_base_dir(false, move |directory| {
					match directory.metadata(relative_path) {
						Ok(_) => Ok(true),
						Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
						Err(error) => Err(error.into()),
					}
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;
			Ok(full_path.exists())
		}
	}

	async fn metadata(&self, path: &str) -> StorageResult<FileMetadata> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			let stored_path = path.to_string();
			return self
				.with_base_dir(false, move |directory| {
					let file = directory.open(relative_path).map_err(|error| {
						if error.kind() == std::io::ErrorKind::NotFound {
							StorageError::NotFound(stored_path.clone())
						} else {
							StorageError::Io(error)
						}
					})?;
					Ok(FileMetadata::new(stored_path, file.metadata()?.len()))
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			let file_meta = fs::metadata(&full_path).await?;
			let size = file_meta.len();

			Ok(FileMetadata::new(path.to_string(), size))
		}
	}

	async fn list(&self, path: &str) -> StorageResult<Vec<FileMetadata>> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			if !path.is_empty() {
				Self::validate_path(path)?;
			}
			let relative_path = if path.is_empty() {
				PathBuf::from(".")
			} else {
				PathBuf::from(path)
			};
			let listed_path = path.to_string();
			return self
				.with_base_dir(false, move |directory| {
					let listed_directory = directory.open_dir(relative_path)?;
					let mut results = Vec::new();
					for entry in listed_directory.entries()? {
						let entry = entry?;
						let metadata = entry.metadata()?;
						if metadata.is_file() {
							let relative_path = Path::new(&listed_path).join(entry.file_name());
							results.push(FileMetadata::new(
								relative_path.to_string_lossy().to_string(),
								metadata.len(),
							));
						}
					}
					Ok(results)
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			// Validate path to prevent directory traversal
			// Empty path is allowed to list the base directory
			if !path.is_empty() {
				Self::validate_path(path)?;
			}

			let full_path = if path.is_empty() {
				self.base_path.canonicalize()?
			} else {
				self.full_path(path)?
			};
			let mut entries = fs::read_dir(&full_path).await?;
			let mut results = Vec::new();

			while let Some(entry) = entries.next_entry().await? {
				let metadata = entry.metadata().await?;
				if metadata.is_file() {
					let file_name = entry.file_name().to_string_lossy().to_string();
					let relative_path = Path::new(path).join(&file_name);
					results.push(FileMetadata::new(
						relative_path.to_string_lossy().to_string(),
						metadata.len(),
					));
				}
			}

			Ok(results)
		}
	}

	fn url(&self, path: &str) -> String {
		format!(
			"{}/{}",
			self.base_url.trim_end_matches('/'),
			path.trim_start_matches('/')
		)
	}

	fn path(&self, name: &str) -> String {
		name.to_string()
	}

	async fn get_accessed_time(&self, path: &str) -> StorageResult<chrono::DateTime<chrono::Utc>> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			return self
				.with_base_dir(false, move |directory| {
					let accessed = directory.open(relative_path)?.metadata()?.accessed()?;
					Ok(accessed.into_std().into())
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			let file_meta = fs::metadata(&full_path).await?;
			let accessed = file_meta.accessed()?;
			let datetime: chrono::DateTime<chrono::Utc> = accessed.into();
			Ok(datetime)
		}
	}

	async fn get_created_time(&self, path: &str) -> StorageResult<chrono::DateTime<chrono::Utc>> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			return self
				.with_base_dir(false, move |directory| {
					let created = directory.open(relative_path)?.metadata()?.created()?;
					Ok(created.into_std().into())
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			let file_meta = fs::metadata(&full_path).await?;
			let created = file_meta.created()?;
			let datetime: chrono::DateTime<chrono::Utc> = created.into();
			Ok(datetime)
		}
	}

	async fn get_modified_time(&self, path: &str) -> StorageResult<chrono::DateTime<chrono::Utc>> {
		#[cfg(not(target_arch = "wasm32"))]
		{
			Self::validate_path(path)?;
			let relative_path = PathBuf::from(path);
			return self
				.with_base_dir(false, move |directory| {
					let modified = directory.open(relative_path)?.metadata()?.modified()?;
					Ok(modified.into_std().into())
				})
				.await;
		}
		#[cfg(target_arch = "wasm32")]
		{
			let full_path = self.full_path(path)?;

			if !full_path.exists() {
				return Err(StorageError::NotFound(path.to_string()));
			}

			let file_meta = fs::metadata(&full_path).await?;
			let modified = file_meta.modified()?;
			let datetime: chrono::DateTime<chrono::Utc> = modified.into();
			Ok(datetime)
		}
	}
}

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

	async fn create_test_storage() -> (LocalStorage, TempDir) {
		let temp_dir = TempDir::new().unwrap();
		let storage = LocalStorage::new(temp_dir.path(), "http://localhost/media");
		storage.ensure_base_dir().await.unwrap();
		(storage, temp_dir)
	}

	#[tokio::test]
	async fn test_local_storage_path() {
		let storage = LocalStorage::new("/tmp/storage", "http://localhost/media");
		assert_eq!(storage.url("test.txt"), "http://localhost/media/test.txt");
	}

	#[tokio::test]
	async fn test_file_access_options() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test that file doesn't exist initially
		assert!(!storage.exists("storage_test").await.unwrap());

		// Save a file
		let content = b"storage contents";
		storage.save("storage_test", content).await.unwrap();

		// Check file exists
		assert!(storage.exists("storage_test").await.unwrap());

		// Read file content
		let file = storage.read("storage_test").await.unwrap();
		assert_eq!(file.content, content);

		// Delete file
		storage.delete("storage_test").await.unwrap();
		assert!(!storage.exists("storage_test").await.unwrap());
	}

	#[tokio::test]
	async fn test_file_save_with_path() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Saving a pathname should create intermediate directories
		assert!(!storage.exists("path/to").await.unwrap());

		storage
			.save("path/to/test.file", b"file saved with path")
			.await
			.unwrap();

		assert!(storage.exists("path/to/test.file").await.unwrap());

		let file = storage.read("path/to/test.file").await.unwrap();
		assert_eq!(file.content, b"file saved with path");

		storage.delete("path/to/test.file").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_size() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("file.txt", b"test").await.unwrap();
		let metadata = storage.metadata("file.txt").await.unwrap();
		assert_eq!(metadata.size, 4);

		storage.delete("file.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_exists() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("dir/subdir/file.txt", b"test").await.unwrap();
		assert!(storage.exists("dir/subdir/file.txt").await.unwrap());

		storage.delete("dir/subdir/file.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_delete() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("dir/subdir/file.txt", b"test").await.unwrap();
		storage
			.save("dir/subdir/other_file.txt", b"test")
			.await
			.unwrap();

		assert!(storage.exists("dir/subdir/file.txt").await.unwrap());
		assert!(storage.exists("dir/subdir/other_file.txt").await.unwrap());

		storage.delete("dir/subdir/other_file.txt").await.unwrap();
		assert!(!storage.exists("dir/subdir/other_file.txt").await.unwrap());

		storage.delete("dir/subdir/file.txt").await.unwrap();
		assert!(!storage.exists("dir/subdir/file.txt").await.unwrap());
	}

	#[tokio::test]
	async fn test_delete_missing_file() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Deleting a missing file should return an error
		let result = storage.delete("missing_file.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::NotFound(_)));
	}

	#[tokio::test]
	async fn test_file_url() {
		let storage = LocalStorage::new("/tmp/storage", "http://localhost/media");

		assert_eq!(storage.url("test.file"), "http://localhost/media/test.file");

		// Test URL with base_url without trailing slash
		let storage2 = LocalStorage::new("/tmp/storage", "http://localhost/media/");
		assert_eq!(
			storage2.url("test.file"),
			"http://localhost/media/test.file"
		);
	}

	#[tokio::test]
	async fn test_base_url() {
		// Test with no trailing slash in base_url
		let storage = LocalStorage::new("/tmp/storage", "http://localhost/no_ending_slash");
		assert_eq!(
			storage.url("test.file"),
			"http://localhost/no_ending_slash/test.file"
		);
	}

	#[tokio::test]
	async fn test_listdir() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage
			.save("storage_test_1", b"custom content")
			.await
			.unwrap();
		storage
			.save("storage_test_2", b"custom content")
			.await
			.unwrap();
		storage.save("dir/file_c.txt", b"test").await.unwrap();

		let files = storage.list("").await.unwrap();
		let file_names: Vec<String> = files
			.iter()
			.map(|f| {
				std::path::Path::new(&f.path)
					.file_name()
					.unwrap()
					.to_string_lossy()
					.to_string()
			})
			.collect();

		assert!(file_names.contains(&"storage_test_1".to_string()));
		assert!(file_names.contains(&"storage_test_2".to_string()));

		// Cleanup
		storage.delete("storage_test_1").await.unwrap();
		storage.delete("storage_test_2").await.unwrap();
		storage.delete("dir/file_c.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_open_missing_file() {
		let (storage, _temp_dir) = create_test_storage().await;

		let result = storage.read("missing.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::NotFound(_)));
	}

	#[tokio::test]
	async fn test_large_file_saving() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Create a large file (3 * 64KB)
		let large_content = vec![b'A'; 64 * 1024 * 3];
		storage
			.save("large_file.txt", &large_content)
			.await
			.unwrap();

		let metadata = storage.metadata("large_file.txt").await.unwrap();
		assert_eq!(metadata.size, large_content.len() as u64);

		storage.delete("large_file.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_checksum() {
		let (storage, _temp_dir) = create_test_storage().await;

		let metadata = storage.save("file.txt", b"test").await.unwrap();
		assert!(metadata.checksum.is_some());

		// Same content should produce same checksum
		let metadata2 = storage.save("file2.txt", b"test").await.unwrap();
		assert_eq!(metadata.checksum, metadata2.checksum);

		storage.delete("file.txt").await.unwrap();
		storage.delete("file2.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_get_accessed_time() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("test.file", b"custom contents").await.unwrap();

		let atime = storage.get_accessed_time("test.file").await.unwrap();
		let now = chrono::Utc::now();

		// Access time should be close to current time
		let diff = (now - atime).num_seconds().abs();
		assert!(
			diff < 5,
			"Access time difference too large: {} seconds",
			diff
		);

		storage.delete("test.file").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_get_created_time() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("test.file", b"custom contents").await.unwrap();

		let ctime = storage.get_created_time("test.file").await.unwrap();
		let now = chrono::Utc::now();

		// Creation time should be close to current time
		let diff = (now - ctime).num_seconds().abs();
		assert!(
			diff < 5,
			"Creation time difference too large: {} seconds",
			diff
		);

		storage.delete("test.file").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_get_modified_time() {
		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("test.file", b"custom contents").await.unwrap();

		let mtime = storage.get_modified_time("test.file").await.unwrap();
		let now = chrono::Utc::now();

		// Modified time should be close to current time
		let diff = (now - mtime).num_seconds().abs();
		assert!(
			diff < 5,
			"Modified time difference too large: {} seconds",
			diff
		);

		storage.delete("test.file").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_modified_time_changes() {
		use tokio::time::{Duration, sleep};

		let (storage, _temp_dir) = create_test_storage().await;

		storage.save("file.txt", b"test").await.unwrap();
		let modified_time = storage.get_modified_time("file.txt").await.unwrap();

		// Wait a bit
		sleep(Duration::from_millis(100)).await;

		// Modify the file
		storage.save("file.txt", b"new content").await.unwrap();

		let new_modified_time = storage.get_modified_time("file.txt").await.unwrap();
		assert!(
			new_modified_time > modified_time,
			"Modified time should increase after file change"
		);

		storage.delete("file.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_file_storage_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.save("../test.txt", b"test").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.save("/etc/passwd", b"test").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[cfg(unix)]
	#[tokio::test]
	async fn storage_rejects_symlinks_that_escape_the_base_directory() {
		use std::os::unix::fs::symlink;

		let (storage, temp_dir) = create_test_storage().await;
		let outside = TempDir::new().expect("outside directory should be created");
		tokio::fs::write(outside.path().join("secret.txt"), b"secret")
			.await
			.expect("outside fixture should be written");
		symlink(outside.path(), temp_dir.path().join("escape"))
			.expect("test symlink should be created");

		let read_result = storage.read("escape/secret.txt").await;
		let save_result = storage.save("escape/new.txt", b"outside write").await;

		assert!(read_result.is_err());
		assert!(save_result.is_err());
		assert!(!outside.path().join("new.txt").exists());
	}

	#[cfg(unix)]
	#[tokio::test]
	async fn storage_rejects_broken_symlink_writes_outside_the_base_directory() {
		use std::os::unix::fs::symlink;

		let (storage, temp_dir) = create_test_storage().await;
		let outside = TempDir::new().expect("outside directory should be created");
		let outside_file = outside.path().join("new.txt");
		symlink(&outside_file, temp_dir.path().join("escape.txt"))
			.expect("test symlink should be created");

		assert!(storage.save("escape.txt", b"outside write").await.is_err());
		assert!(!outside_file.exists());
	}

	#[cfg(unix)]
	#[tokio::test]
	async fn storage_reuses_the_initialized_root_after_path_replacement() {
		use std::os::unix::fs::symlink;

		let temp = TempDir::new().expect("temporary directory should be created");
		let base = temp.path().join("storage");
		let moved = temp.path().join("moved");
		let outside = TempDir::new().expect("outside directory should be created");
		let storage = LocalStorage::new(&base, "http://localhost/media");
		storage
			.ensure_base_dir()
			.await
			.expect("storage root should be initialized");
		std::fs::rename(&base, &moved).expect("storage root should be moved");
		symlink(outside.path(), &base).expect("replacement symlink should be created");

		storage
			.save("trusted.txt", b"trusted")
			.await
			.expect("the retained root should remain writable");

		assert_eq!(
			std::fs::read(moved.join("trusted.txt")).expect("retained root file should exist"),
			b"trusted"
		);
		assert!(!outside.path().join("trusted.txt").exists());
	}

	#[tokio::test]
	async fn test_storage_dangerous_paths() {
		let (storage, _temp_dir) = create_test_storage().await;

		let dangerous_paths = vec!["..", ".", "", "../path", "tmp/../path", "/tmp/path"];

		for path in dangerous_paths {
			let result = storage.save(path, b"test").await;
			assert!(
				result.is_err(),
				"Path '{}' should be rejected but was accepted",
				path
			);
			assert!(
				matches!(result.unwrap_err(), StorageError::InvalidPath(_)),
				"Path '{}' should return InvalidPath error",
				path
			);
		}
	}

	#[tokio::test]
	async fn test_path_with_dots_in_filename() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Valid path with dots in directory and filename should work
		storage.save("my.dir/test.file.txt", b"test").await.unwrap();
		assert!(storage.exists("my.dir/test.file.txt").await.unwrap());

		storage.delete("my.dir/test.file.txt").await.unwrap();
	}

	#[tokio::test]
	async fn test_url_encoding() {
		let storage = LocalStorage::new("/tmp/storage", "http://localhost/media");

		// Basic file
		assert_eq!(storage.url("test.file"), "http://localhost/media/test.file");

		// File with special characters (note: basic implementation doesn't encode)
		// This test documents current behavior
		let url = storage.url("test file.txt");
		assert!(url.contains("test file.txt"));
	}

	#[tokio::test]
	async fn test_read_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.read("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.read("/etc/passwd").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test embedded parent directory
		let result = storage.read("tmp/../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[tokio::test]
	async fn test_delete_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.delete("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.delete("/etc/passwd").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[tokio::test]
	async fn test_exists_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.exists("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.exists("/etc/passwd").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[tokio::test]
	async fn test_metadata_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.metadata("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.metadata("/etc/passwd").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[tokio::test]
	async fn test_list_prevents_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test parent directory traversal
		let result = storage.list("../test").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test absolute path
		let result = storage.list("/etc").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}

	#[tokio::test]
	async fn test_get_time_operations_prevent_directory_traversal() {
		let (storage, _temp_dir) = create_test_storage().await;

		// Test get_accessed_time
		let result = storage.get_accessed_time("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test get_created_time
		let result = storage.get_created_time("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));

		// Test get_modified_time
		let result = storage.get_modified_time("../test.txt").await;
		assert!(result.is_err());
		assert!(matches!(result.unwrap_err(), StorageError::InvalidPath(_)));
	}
}