Skip to content

Commit

Permalink
refactor: more optional for name
Browse files Browse the repository at this point in the history
Signed-off-by: simonsan <[email protected]>
  • Loading branch information
simonsan committed Jan 24, 2024
1 parent f9807e0 commit 5c94f80
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/handlers/file_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ pub(crate) async fn get_config(
/// add_config
/// Interface: POST {path}/config
pub(crate) async fn add_config(
AxumPath((path, tpe, name)): AxumPath<(Option<String>, String, String)>,
AxumPath((path, tpe)): AxumPath<(Option<String>, String)>,
auth: AuthFromRequest,
request: Request,
) -> Result<impl IntoResponse> {
tracing::debug!("[add_config] path: {path:?}, tpe: {tpe}, name: {name}");
tracing::debug!("[add_config] path: {path:?}, tpe: {tpe}");
let path = path.unwrap_or_default();
let path = PathBuf::from(&path);
let file = get_save_file(auth.user, path, &tpe, name).await?;
let file = get_save_file(auth.user, path, &tpe, None).await?;

let stream = request.into_body().into_data_stream();
save_body(file, stream).await?;
Expand Down
17 changes: 5 additions & 12 deletions src/handlers/file_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) async fn add_file(

//credential & access check executed in get_save_file()
let path = std::path::PathBuf::from(&path_str);
let file = get_save_file(auth.user, path, &tpe, name).await?;
let file = get_save_file(auth.user, path, &tpe, Some(name)).await?;

let stream = request.into_body().into_data_stream();
save_body(file, stream).await?;
Expand Down Expand Up @@ -102,22 +102,15 @@ pub(crate) async fn get_save_file(
user: String,
path: PathBuf,
tpe: &str,
name: String,
name: Option<String>,
) -> Result<impl AsyncWrite + Unpin + Finalizer> {
tracing::debug!("[get_save_file] path: {path:?}, tpe: {tpe}, name: {name}");
tracing::debug!("[get_save_file] path: {path:?}, tpe: {tpe}, name: {name:?}");

check_name(tpe, Some(&name))?;
check_name(tpe, name.as_deref())?;
check_auth_and_acl(user, tpe, path.as_path(), AccessType::Append)?;

let storage = STORAGE.get().unwrap();
let file_writer = match storage.create_file(&path, tpe, &name).await {
Ok(w) => w,
Err(_) => {
return Err(ErrorKind::GettingFileHandleFailed);
}
};

Ok(file_writer)
storage.create_file(&path, tpe, name.as_deref()).await
}

/// saves the content in the HTML request body to a file stream.
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) async fn list_files(
check_auth_and_acl(auth.user, &tpe, path, AccessType::Read)?;

let storage = STORAGE.get().unwrap();
let read_dir = storage.read_dir(path, &tpe);
let read_dir = storage.read_dir(path, Some(&tpe));

let mut res = match headers
.get(header::ACCEPT)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) async fn create_repository(
match params.create {
true => {
for tpe_i in TYPES.iter() {
storage.create_dir(path, tpe_i)?
storage.create_dir(path, Some(tpe_i))?
}

Ok((
Expand Down
49 changes: 38 additions & 11 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ pub(crate) fn init_storage(storage: impl Storage) -> Result<()> {
#[async_trait::async_trait]
//#[enum_dispatch(StorageEnum)]
pub trait Storage: Send + Sync + 'static {
fn create_dir(&self, path: &Path, tpe: &str) -> Result<()>;
fn read_dir(&self, path: &Path, tpe: &str) -> Box<dyn Iterator<Item = walkdir::DirEntry>>;
fn create_dir(&self, path: &Path, tpe: Option<&str>) -> Result<()>;
fn read_dir(
&self,
path: &Path,
tpe: Option<&str>,
) -> Box<dyn Iterator<Item = walkdir::DirEntry>>;
fn filename(&self, path: &Path, tpe: &str, name: Option<&str>) -> PathBuf;
async fn open_file(&self, path: &Path, tpe: &str, name: Option<&str>) -> Result<File>;
async fn create_file(&self, path: &Path, tpe: &str, name: &str) -> Result<WriteOrDeleteFile>;
async fn create_file(
&self,
path: &Path,
tpe: &str,
name: Option<&str>,
) -> Result<WriteOrDeleteFile>;
fn remove_file(&self, path: &Path, tpe: &str, name: Option<&str>) -> Result<()>;
fn remove_repository(&self, path: &Path) -> Result<()>;
}
Expand Down Expand Up @@ -62,9 +71,9 @@ impl LocalStorage {
}
#[async_trait::async_trait]
impl Storage for LocalStorage {
fn create_dir(&self, path: &Path, tpe: &str) -> Result<()> {
fn create_dir(&self, path: &Path, tpe: Option<&str>) -> Result<()> {
match tpe {
"data" => {
Some(tpe) if tpe == "data" => {
for i in 0..256 {
fs::create_dir_all(self.path.join(path).join(tpe).join(format!("{:02x}", i)))
.map_err(|err| {
Expand All @@ -75,14 +84,27 @@ impl Storage for LocalStorage {
}
Ok(())
}
_ => fs::create_dir_all(self.path.join(path).join(tpe)).map_err(|err| {
Some(tpe) => fs::create_dir_all(self.path.join(path).join(tpe)).map_err(|err| {
ErrorKind::CreatingDirectoryFailed(format!("Could not create directory: {err}"))
}),
None => fs::create_dir_all(self.path.join(path)).map_err(|err| {
ErrorKind::CreatingDirectoryFailed(format!("Could not create directory: {err}"))
}),
}
}

fn read_dir(&self, path: &Path, tpe: &str) -> Box<dyn Iterator<Item = walkdir::DirEntry>> {
let walker = WalkDir::new(self.path.join(path).join(tpe))
fn read_dir(
&self,
path: &Path,
tpe: Option<&str>,
) -> Box<dyn Iterator<Item = walkdir::DirEntry>> {
let path = if let Some(tpe) = tpe {
self.path.join(path).join(tpe)
} else {
self.path.join(path)
};

let walker = WalkDir::new(path)
.into_iter()
.filter_map(walkdir::Result::ok)
.filter(|e| e.file_type().is_file());
Expand All @@ -105,8 +127,13 @@ impl Storage for LocalStorage {
.map_err(|err| ErrorKind::OpeningFileFailed(format!("Could not open file: {}", err)))?)
}

async fn create_file(&self, path: &Path, tpe: &str, name: &str) -> Result<WriteOrDeleteFile> {
let file_path = self.filename(path, tpe, Some(name));
async fn create_file(
&self,
path: &Path,
tpe: &str,
name: Option<&str>,
) -> Result<WriteOrDeleteFile> {
let file_path = self.filename(path, tpe, name);
WriteOrDeleteFile::new(file_path).await
}

Expand Down Expand Up @@ -150,7 +177,7 @@ mod test {

// path must not start with slash !! that will skip the self.path from Storage!
let path = PathBuf::new().join("test_repo/");
let c = storage.read_dir(&path, "keys");
let c = storage.read_dir(&path, Some("keys"));
let mut found = false;
for a in c.into_iter() {
let file_name = a.file_name().to_string_lossy();
Expand Down

0 comments on commit 5c94f80

Please sign in to comment.