Skip to content

Commit

Permalink
refactor: simplify tokio usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcAntoine-Arnaud authored and woutersl committed Aug 14, 2024
1 parent fde3e7c commit b8c4575
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/services/storage/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use std::path::{Path, PathBuf};

use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::Storage;
use crate::model::cargo::CrateMetadata;
use crate::utils::apierror::{error_not_found, ApiError};
Expand Down Expand Up @@ -43,9 +41,7 @@ impl<'config> FsStorage<'config> {
async fn write_to_file(&self, path: &str, content: &[u8]) -> Result<(), ApiError> {
let full_path = PathBuf::from(format!("{}/{path}", self.data_dir));
tokio::fs::create_dir_all(full_path.parent().unwrap()).await?;
let file = tokio::fs::File::create(&full_path).await?;
let mut writer = tokio::io::BufWriter::new(file);
writer.write_all(content).await?;
tokio::fs::write(full_path, content).await?;
Ok(())
}

Expand All @@ -61,11 +57,8 @@ impl<'config> FsStorage<'config> {
return Err(error_not_found());
}
}
let file = tokio::fs::File::open(&full_path).await?;
let mut reader = tokio::io::BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await?;
Ok(buffer)

Ok(tokio::fs::read(full_path).await?)
}
}

Expand Down

0 comments on commit b8c4575

Please sign in to comment.