cargolifter_storage_filesystem/
lib.rs1use std::io::{Read, Write};
2use std::path::Path;
3
4use async_trait::async_trait;
5
6pub struct FileSystemStorage {
7 root_folder: String,
8}
9
10impl FileSystemStorage {
11 pub fn new(root_folder: &str) -> Self {
12 Self {
13 root_folder: root_folder.into(),
14 }
15 }
16}
17
18#[async_trait]
19impl cargolifter_core::Storage for FileSystemStorage {
20 async fn get(
21 &self,
22 crate_name: &str,
23 crate_version: &str,
24 ) -> Result<Vec<u8>, cargolifter_core::models::StorageError> {
25 let root_path = Path::new(&self.root_folder);
26 let path = root_path.join(cargolifter_core::get_crate_path(crate_name));
27 let path = path.join(crate_version);
28 tracing::info!("trying to get '{}'", path.to_str().unwrap());
29
30 let mut data = Vec::new();
31 let mut file = std::fs::File::open(path)?;
32 file.read_to_end(&mut data)?;
33
34 Ok(data)
35 }
36
37 async fn put(
38 &mut self,
39 crate_name: &str,
40 crate_version: &str,
41 data: &[u8],
42 ) -> Result<(), cargolifter_core::models::StorageError> {
43 let root_path = Path::new(&self.root_folder);
44 let path = root_path.join(cargolifter_core::get_crate_path(crate_name));
45 std::fs::create_dir_all(path.clone()).unwrap();
46 let path = path.join(crate_version);
47 tracing::info!("adding '{}' to storage", path.to_str().unwrap());
48
49 let mut file = std::fs::File::create(path)?;
50 file.write_all(data)?;
51
52 Ok(())
53 }
54}