First pass implementation for collection::browse

This commit is contained in:
Antoine Gersant 2016-08-18 01:04:06 -07:00
parent 00b4d4cb77
commit 63d10fa695
2 changed files with 48 additions and 11 deletions

View file

@ -18,8 +18,8 @@ pub fn get_api_handler() -> Mount {
fn path_from_request(request: &Request) -> Result<PathBuf, Utf8Error> {
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<Response> {
@ -27,7 +27,8 @@ fn browse(request: &mut Request) -> IronResult<Response> {
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")))
}

View file

@ -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<SFile> {
println!("Browse {:?}", path);
let out = vec![];
out
#[derive(Debug)]
pub enum CollectionFile {
Directory(PathBuf),
Song(Song),
}
pub fn flatten(path: &PathBuf) -> Vec<SFile> {
pub enum CollectionError
{
Io(io::Error),
}
impl From<io::Error> for CollectionError {
fn from(err: io::Error) -> CollectionError {
CollectionError::Io(err)
}
}
pub fn browse(path: &Path) -> Result<Vec<CollectionFile>, 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<CollectionFile> {
println!("Flatten {:?}", path);
let out = vec![];
out