From 63d10fa695ffebc0ecd717c9a16dd6231c1e919e Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Thu, 18 Aug 2016 01:04:06 -0700 Subject: [PATCH] First pass implementation for collection::browse --- src/api/mod.rs | 7 +++--- src/collection/mod.rs | 52 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 7eb8445..4de3e3d 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -18,8 +18,8 @@ pub fn get_api_handler() -> Mount { fn path_from_request(request: &Request) -> Result { let path_string = request.url.path().join("/"); - let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8(); - decoded_path.map(|s| PathBuf::from(s.deref())) + let decoded_path = try!(percent_decode(path_string.as_bytes()).decode_utf8()); + Ok(PathBuf::from(decoded_path.deref())) } fn browse(request: &mut Request) -> IronResult { @@ -27,7 +27,8 @@ fn browse(request: &mut Request) -> IronResult { if path.is_err() { return Ok(Response::with((status::BadRequest))); } - collection::browse(&path.unwrap()); + let browse_result = collection::browse(&path.unwrap()); + println!("{:?}", browse_result.unwrap_or(vec![])); // TMP Ok(Response::with((status::Ok, "TODO browse data here"))) } diff --git a/src/collection/mod.rs b/src/collection/mod.rs index 5844eeb..06bc937 100644 --- a/src/collection/mod.rs +++ b/src/collection/mod.rs @@ -1,17 +1,53 @@ +use std::fs; +use std::io; +use std::path::Path; use std::path::PathBuf; -pub enum SFile { - // Directory, - // Song, +#[derive(Debug)] +pub struct Song { + path: PathBuf, } -pub fn browse(path: &PathBuf) -> Vec { - println!("Browse {:?}", path); - let out = vec![]; - out +#[derive(Debug)] +pub enum CollectionFile { + Directory(PathBuf), + Song(Song), } -pub fn flatten(path: &PathBuf) -> Vec { +pub enum CollectionError +{ + Io(io::Error), +} + +impl From for CollectionError { + fn from(err: io::Error) -> CollectionError { + CollectionError::Io(err) + } +} + +pub fn browse(path: &Path) -> Result, CollectionError> { + + let full_path = "samplemusic/".to_string() + path.to_str().unwrap(); // TMP use mount directories + println!("Browsing: {}", full_path); + + let mut out = vec![]; + for file in try!(fs::read_dir(full_path)) { + let file = try!(file); + let file_meta = try!(file.metadata()); + let file_path = file.path().to_owned(); + if file_meta.is_file() { + let collection_file = CollectionFile::Song(Song {path: file_path }); + out.push(collection_file); + } else if file_meta.is_dir() { + let collection_file = CollectionFile::Directory(file_path); + out.push(collection_file); + } + } + + Ok(out) +} + +pub fn flatten(path: &Path) -> Vec { println!("Flatten {:?}", path); let out = vec![]; out