about summary refs log tree commit diff
path: root/kittybox-rs/src/media/storage/file.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-07-07 00:32:33 +0300
committerVika <vika@fireburn.ru>2022-07-07 00:36:39 +0300
commit7f23ec84bc05c236c1bf40c2f0d72412af711516 (patch)
treef0ba64804fffce29a8f04e5b6c76f9863de81dd2 /kittybox-rs/src/media/storage/file.rs
parent5cfac54aa4fb3c207ea2cbbeccd4571fa204a09b (diff)
treewide: rewrite using Axum
Axum has streaming bodies and allows to write simpler code. It also
helps enforce stronger types and looks much more neat.

This allows me to progress on the media endpoint and add streaming
reads and writes to the MediaStore trait.

Metrics are temporarily not implemented. Everything else was
preserved, and the tests still pass, after adjusting for new calling
conventions.

TODO: create method routers for protocol endpoints
Diffstat (limited to 'kittybox-rs/src/media/storage/file.rs')
-rw-r--r--kittybox-rs/src/media/storage/file.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/kittybox-rs/src/media/storage/file.rs b/kittybox-rs/src/media/storage/file.rs
new file mode 100644
index 0000000..8c0ddf0
--- /dev/null
+++ b/kittybox-rs/src/media/storage/file.rs
@@ -0,0 +1,61 @@
+use super::{ErrorKind, MediaStore, MediaStoreError, Result};
+use async_trait::async_trait;
+use std::path::PathBuf;
+use tokio::fs::OpenOptions;
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
+
+#[derive(Clone)]
+pub struct FileStore {
+    base: PathBuf,
+}
+
+impl From<tokio::io::Error> for MediaStoreError {
+    fn from(source: tokio::io::Error) -> Self {
+        Self {
+            source: Some(Box::new(source)),
+            msg: "file I/O error".to_owned(),
+            kind: ErrorKind::Backend,
+        }
+    }
+}
+
+#[async_trait]
+impl MediaStore for FileStore {
+    async fn write_streaming(
+        &self, domain: url::Host, filename: &str,
+        content: axum::extract::multipart::Field<'_>
+    ) -> Result<()> {
+        todo!()
+    }
+
+    async fn read_streaming(&self, domain: url::Host, filename: &str) -> Result<tokio_util::io::ReaderStream<Box<dyn tokio::io::AsyncRead>>> {
+        todo!()
+    }
+    
+    async fn write(&self, domain: url::Host, filename: &str, content: &[u8]) -> Result<()> {
+        let path = self.base.join(format!("{}/{}", domain, filename));
+
+        let mut file = OpenOptions::new()
+            .create_new(true)
+            .write(true)
+            .open(path)
+            .await?;
+
+        Ok(file.write_all(content).await?)
+    }
+
+    async fn read(&self, domain: url::Host, filename: &str) -> Result<Vec<u8>> {
+        let path = self.base.join(format!("{}/{}", domain, filename));
+
+        let mut file = OpenOptions::new().read(true).open(path).await?;
+
+        let mut buf: Vec<u8> = Vec::default();
+        file.read_to_end(&mut buf).await?;
+
+        Ok(buf)
+    }
+
+    async fn delete(&self, domain: url::Host, filename: &str) -> Result<()> {
+        todo!()
+    }
+}