Implemented flatten
This commit is contained in:
parent
6339f94836
commit
5e23a0b5d0
2 changed files with 82 additions and 20 deletions
13
src/api.rs
13
src/api.rs
|
@ -64,7 +64,6 @@ fn browse(request: &mut Request, collection: &mut Collection) -> IronResult<Resp
|
|||
}
|
||||
let result_json = result_json.unwrap();
|
||||
|
||||
println!("{:?}", browse_result); // TMP
|
||||
Ok(Response::with((status::Ok, result_json)))
|
||||
}
|
||||
|
||||
|
@ -73,6 +72,14 @@ fn flatten(request: &mut Request, collection: &mut Collection) -> IronResult<Res
|
|||
if path.is_err() {
|
||||
return Ok(Response::with((status::BadRequest)));
|
||||
}
|
||||
collection.flatten(&path.unwrap());
|
||||
Ok(Response::with((status::Ok, "TODO Flatten data here")))
|
||||
let path = path.unwrap();
|
||||
let flatten_result = try!(collection.flatten(&path));
|
||||
|
||||
let result_json = json::encode(&flatten_result);
|
||||
if result_json.is_err() {
|
||||
return Ok(Response::with(status::InternalServerError));
|
||||
}
|
||||
let result_json = result_json.unwrap();
|
||||
|
||||
Ok(Response::with((status::Ok, result_json)))
|
||||
}
|
||||
|
|
|
@ -7,11 +7,57 @@ use error::*;
|
|||
#[derive(Debug, RustcEncodable)]
|
||||
pub struct Song {
|
||||
path: String,
|
||||
display_name: String,
|
||||
}
|
||||
|
||||
impl Song {
|
||||
pub fn read(collection: &Collection, file: &fs::DirEntry) -> Result<Song, CollectionError> {
|
||||
let file_meta = try!(file.metadata());
|
||||
assert!(file_meta.is_file());
|
||||
|
||||
let file_path = file.path();
|
||||
let file_path = file_path.as_path();
|
||||
let virtual_path = try!(collection.vfs.real_to_virtual(file_path));
|
||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||
|
||||
let display_name = virtual_path.file_stem().unwrap();
|
||||
let display_name = display_name.to_str().unwrap();
|
||||
let display_name = display_name.to_string();
|
||||
|
||||
Ok(Song {
|
||||
path: path_string.to_string(),
|
||||
display_name: display_name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, RustcEncodable)]
|
||||
pub struct Directory {
|
||||
path: String,
|
||||
display_name: String,
|
||||
}
|
||||
|
||||
impl Directory {
|
||||
pub fn read(collection: &Collection,
|
||||
file: &fs::DirEntry)
|
||||
-> Result<Directory, CollectionError> {
|
||||
let file_meta = try!(file.metadata());
|
||||
assert!(file_meta.is_dir());
|
||||
|
||||
let file_path = file.path();
|
||||
let file_path = file_path.as_path();
|
||||
let virtual_path = try!(collection.vfs.real_to_virtual(file_path));
|
||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||
|
||||
let display_name = virtual_path.iter().last().unwrap();
|
||||
let display_name = display_name.to_str().unwrap();
|
||||
let display_name = display_name.to_string();
|
||||
|
||||
Ok(Directory {
|
||||
path: path_string.to_string(),
|
||||
display_name: display_name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, RustcEncodable)]
|
||||
|
@ -36,35 +82,44 @@ impl Collection {
|
|||
pub fn browse(&self, path: &Path) -> Result<Vec<CollectionFile>, CollectionError> {
|
||||
|
||||
let full_path = try!(self.vfs.virtual_to_real(path));
|
||||
let full_path = full_path.to_str().unwrap();
|
||||
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();
|
||||
let file_path = file_path.as_path();
|
||||
if file_meta.is_file() {
|
||||
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||
let collection_file = CollectionFile::Song(Song { path: path_string.to_string() });
|
||||
out.push(collection_file);
|
||||
let song = try!(Song::read(self, &file));
|
||||
out.push(CollectionFile::Song(song));
|
||||
} else if file_meta.is_dir() {
|
||||
let virtual_path = try!(self.vfs.real_to_virtual(file_path));
|
||||
let path_string = try!(virtual_path.to_str().ok_or(CollectionError::PathDecoding));
|
||||
let collection_file =
|
||||
CollectionFile::Directory(Directory { path: path_string.to_string() });
|
||||
out.push(collection_file);
|
||||
let directory = try!(Directory::read(self, &file));
|
||||
out.push(CollectionFile::Directory(directory));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn flatten(&self, path: &Path) -> Vec<CollectionFile> {
|
||||
println!("Flatten {:?}", path);
|
||||
let out = vec![];
|
||||
out
|
||||
fn flatten_internal(&self, path: &Path) -> Result<Vec<Song>, CollectionError> {
|
||||
let files = try!(fs::read_dir(path));
|
||||
files.fold(Ok(vec![]), |acc, file| {
|
||||
let mut acc = try!(acc);
|
||||
let file: fs::DirEntry = try!(file);
|
||||
let file_meta = try!(file.metadata());
|
||||
if file_meta.is_file() {
|
||||
let song = try!(Song::read(self, &file));
|
||||
acc.push(song);
|
||||
} else {
|
||||
let explore_path = file.path();
|
||||
let explore_path = explore_path.as_path();
|
||||
let mut explore_content = try!(self.flatten_internal(explore_path));
|
||||
acc.append(&mut explore_content);
|
||||
}
|
||||
Ok(acc)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn flatten(&self, path: &Path) -> Result<Vec<Song>, CollectionError> {
|
||||
let real_path = try!(self.vfs.virtual_to_real(path));
|
||||
self.flatten_internal(real_path.as_path())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue