Skip to content

Commit

Permalink
Use walkdir for listing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Dev committed Feb 16, 2021
1 parent e8e986e commit 005d4bc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ serde = { version = "1", features = ["derive"] }
clap = "3.0.0-beta.2"
htpasswd-verify = "0.1"
toml = "0.5"
#parking_lot = "0.11"
#tempfile = "3"
walkdir = "2"
14 changes: 6 additions & 8 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use crate::helpers::WriteOrDeleteFile;
use async_std::fs::File;
use async_std::io::Result;
use walkdir::WalkDir;

#[derive(Clone)]
pub struct Storage {
Expand All @@ -29,14 +30,11 @@ impl Storage {
}
}

pub fn read_dir(&self, path: &Path, tpe: &str) -> Result<impl Iterator<Item = fs::DirEntry>> {
// TODO: error handling
Ok(self.path
.join(path)
.join(tpe)
.read_dir()?
.filter(|e| e.as_ref().unwrap().file_type().unwrap().is_file())
.filter_map(Result::ok))
pub fn read_dir(&self, path: &Path, tpe: &str) -> impl Iterator<Item = walkdir::DirEntry> {
WalkDir::new(self.path.join(path).join(tpe))
.into_iter()
.filter_map(walkdir::Result::ok)
.filter(|e| e.file_type().is_file())
}

pub fn filename(&self, path: &Path, tpe: &str, name: &str) -> PathBuf {
Expand Down
6 changes: 3 additions & 3 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,22 @@ async fn list_files(path: &str, tpe: &str, req: &Request<State>) -> tide::Result
let path = Path::new(path);
check_auth_and_acl(&req, path, tpe, AccessType::Read)?;

let read_dir = req.state().storage.read_dir(path, tpe)?;
let read_dir = req.state().storage.read_dir(path, tpe);
let mut res = Response::new(StatusCode::Ok);

// TODO: error handling
match req.header("Accept") {
Some(a) if a.as_str() == API_V2 => {
res.set_content_type(API_V2);
let read_dir_version = read_dir.map(|e| RepoPathEntry {
name: e.file_name().into_string().unwrap(),
name: e.file_name().to_str().unwrap().to_string(),
size: e.metadata().unwrap().len(),
});
res.set_body(Body::from_json(&IteratorAdapter::new(read_dir_version))?);
}
_ => {
res.set_content_type(API_V1);
let read_dir_version = read_dir.map(|e| e.file_name().into_string().unwrap());
let read_dir_version = read_dir.map(|e| e.file_name().to_str().unwrap().to_string());
res.set_body(Body::from_json(&IteratorAdapter::new(read_dir_version))?);
}
};
Expand Down

0 comments on commit 005d4bc

Please sign in to comment.