diff options
Diffstat (limited to 'src/service_storage.rs')
| -rw-r--r-- | src/service_storage.rs | 47 | +47 −0 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/service_storage.rs b/src/service_storage.rs new file mode 100644 --- /dev/null +++ b/src/service_storage.rs @@ -0,0 +1,47 @@ +use std::{io, path, sync}; +use tokio::fs; + +use super::service_config::ConfigService; + +#[derive(Clone)] +pub struct File { + pub bytes: bytes::Bytes, +} + +pub struct StorageService { + config: sync::Arc<ConfigService>, +} + +impl StorageService { + pub fn new(config: sync::Arc<ConfigService>) -> Self { + Self { config } + } + + pub async fn get(&self, filename: &str) -> Result<Option<File>, io::Error> { + let data_path = self.data_path(filename)?; + let bytes = match fs::read(&data_path).await { + Ok(bytes) => bytes, + Err(err) => { + return if err.kind() == io::ErrorKind::NotFound { + Ok(None) + } else { + Err(err) + }; + } + }; + + Ok(Some(File { + bytes: bytes.into(), + })) + } + + pub async fn put(&self, filename: &str, entry: &File) -> Result<(), io::Error> { + let data_path = self.data_path(filename)?; + fs::write(&data_path, &entry.bytes).await?; + Ok(()) + } + + fn data_path(&self, filename: &str) -> Result<path::PathBuf, io::Error> { + Ok(self.config.dirname().join(filename)) + } +} |
