diff options
author | Vika <vika@fireburn.ru> | 2024-08-01 19:48:37 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2024-08-01 20:40:00 +0300 |
commit | 7e8e688e2e58f9c944b941e768ab7b034a348a1f (patch) | |
tree | 1068469c6b9b97bac407038276fd8971b2101e48 /src/media/storage | |
parent | 57a9c3c7e520714928904fc7e2ff3d62ac2b2467 (diff) | |
download | kittybox-7e8e688e2e58f9c944b941e768ab7b034a348a1f.tar.zst |
treewide: create a common method for state initialization
Now the database objects can be uniformly created from a URI. They can also optionally do sanity checks and one-time initialization.
Diffstat (limited to 'src/media/storage')
-rw-r--r-- | src/media/storage/file.rs | 11 | ||||
-rw-r--r-- | src/media/storage/mod.rs | 2 |
2 files changed, 7 insertions, 6 deletions
diff --git a/src/media/storage/file.rs b/src/media/storage/file.rs index a910eca..7250a6b 100644 --- a/src/media/storage/file.rs +++ b/src/media/storage/file.rs @@ -31,10 +31,6 @@ impl From<tokio::io::Error> for MediaStoreError { } impl FileStore { - pub fn new<T: Into<PathBuf>>(base: T) -> Self { - Self { base: base.into() } - } - async fn mktemp(&self) -> Result<(PathBuf, BufWriter<tokio::fs::File>)> { kittybox_util::fs::mktemp(&self.base, "temp", 16) .await @@ -45,6 +41,9 @@ impl FileStore { #[async_trait] impl MediaStore for FileStore { + async fn new(url: &'_ url::Url) -> Result<Self> { + Ok(Self { base: url.path().into() }) + } #[tracing::instrument(skip(self, content))] async fn write_streaming<T>( @@ -261,7 +260,7 @@ mod tests { #[tracing_test::traced_test] async fn test_ranges() { let tempdir = tempfile::tempdir().expect("Failed to create tempdir"); - let store = FileStore::new(tempdir.path()); + let store = FileStore { base: tempdir.path().to_path_buf() }; let file: &[u8] = include_bytes!("./file.rs"); let stream = tokio_stream::iter(file.chunks(100).map(|i| Ok(bytes::Bytes::copy_from_slice(i)))); @@ -372,7 +371,7 @@ mod tests { #[tracing_test::traced_test] async fn test_streaming_read_write() { let tempdir = tempfile::tempdir().expect("Failed to create tempdir"); - let store = FileStore::new(tempdir.path()); + let store = FileStore { base: tempdir.path().to_path_buf() }; let file: &[u8] = include_bytes!("./file.rs"); let stream = tokio_stream::iter(file.chunks(100).map(|i| Ok(bytes::Bytes::copy_from_slice(i)))); diff --git a/src/media/storage/mod.rs b/src/media/storage/mod.rs index 020999c..38410e6 100644 --- a/src/media/storage/mod.rs +++ b/src/media/storage/mod.rs @@ -86,6 +86,8 @@ pub type Result<T> = std::result::Result<T, MediaStoreError>; #[async_trait] pub trait MediaStore: 'static + Send + Sync + Clone { + // Initialize self from a URL, possibly performing asynchronous initialization. + async fn new(url: &'_ url::Url) -> Result<Self>; async fn write_streaming<T>( &self, domain: &str, |