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
|
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!()
}
}
|