From 7e8e688e2e58f9c944b941e768ab7b034a348a1f Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 1 Aug 2024 19:48:37 +0300 Subject: 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. --- src/media/storage/file.rs | 11 +++++------ src/media/storage/mod.rs | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src/media/storage') 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 for MediaStoreError { } impl FileStore { - pub fn new>(base: T) -> Self { - Self { base: base.into() } - } - async fn mktemp(&self) -> Result<(PathBuf, BufWriter)> { 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 { + Ok(Self { base: url.path().into() }) + } #[tracing::instrument(skip(self, content))] async fn write_streaming( @@ -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 = std::result::Result; #[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; async fn write_streaming( &self, domain: &str, -- cgit 1.4.1