Root of VFS can now be browsed
This commit is contained in:
parent
3d99845590
commit
f7726c0ad9
2 changed files with 29 additions and 26 deletions
|
@ -15,13 +15,8 @@ pub struct Song {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Song {
|
impl Song {
|
||||||
pub fn read(collection: &Collection, file: &fs::DirEntry) -> Result<Song, PError> {
|
pub fn read(collection: &Collection, path: &Path) -> Result<Song, PError> {
|
||||||
let file_meta = try!(file.metadata());
|
let virtual_path = try!(collection.vfs.real_to_virtual(path));
|
||||||
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(PError::PathDecoding));
|
let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding));
|
||||||
|
|
||||||
let display_name = virtual_path.file_stem().unwrap();
|
let display_name = virtual_path.file_stem().unwrap();
|
||||||
|
@ -43,14 +38,9 @@ pub struct Directory {
|
||||||
|
|
||||||
impl Directory {
|
impl Directory {
|
||||||
pub fn read(collection: &Collection,
|
pub fn read(collection: &Collection,
|
||||||
file: &fs::DirEntry)
|
path: &Path)
|
||||||
-> Result<Directory, PError> {
|
-> Result<Directory, PError> {
|
||||||
let file_meta = try!(file.metadata());
|
let virtual_path = try!(collection.vfs.real_to_virtual(path));
|
||||||
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(PError::PathDecoding));
|
let path_string = try!(virtual_path.to_str().ok_or(PError::PathDecoding));
|
||||||
|
|
||||||
let display_name = virtual_path.iter().last().unwrap();
|
let display_name = virtual_path.iter().last().unwrap();
|
||||||
|
@ -148,27 +138,36 @@ impl Collection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> {
|
fn mount(&mut self, name: &str, real_path: &Path) -> Result<(), PError> {
|
||||||
self.vfs.mount(name, real_path)
|
self.vfs.mount(name, real_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn browse(&self, path: &Path) -> Result<Vec<CollectionFile>, PError> {
|
pub fn browse(&self, path: &Path) -> Result<Vec<CollectionFile>, PError> {
|
||||||
|
|
||||||
let full_path = try!(self.vfs.virtual_to_real(path));
|
|
||||||
|
|
||||||
let mut out = vec![];
|
let mut out = vec![];
|
||||||
for file in try!(fs::read_dir(full_path)) {
|
|
||||||
let file = try!(file);
|
if path.components().count() == 0 {
|
||||||
let file_meta = try!(file.metadata());
|
let mount_points = self.vfs.get_mount_points();
|
||||||
if file_meta.is_file() {
|
for (_, target) in mount_points {
|
||||||
let song = try!(Song::read(self, &file));
|
let directory = try!(Directory::read(self, target.as_path()));
|
||||||
out.push(CollectionFile::Song(song));
|
|
||||||
} else if file_meta.is_dir() {
|
|
||||||
let directory = try!(Directory::read(self, &file));
|
|
||||||
out.push(CollectionFile::Directory(directory));
|
out.push(CollectionFile::Directory(directory));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
let full_path = try!(self.vfs.virtual_to_real(path));
|
||||||
|
for file in try!(fs::read_dir(full_path)) {
|
||||||
|
let file = try!(file);
|
||||||
|
let file_meta = try!(file.metadata());
|
||||||
|
if file_meta.is_file() {
|
||||||
|
let song = try!(Song::read(self, file.path().as_path()));
|
||||||
|
out.push(CollectionFile::Song(song));
|
||||||
|
} else if file_meta.is_dir() {
|
||||||
|
let directory = try!(Directory::read(self, file.path().as_path()));
|
||||||
|
out.push(CollectionFile::Directory(directory));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(out)
|
Ok(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,7 +178,7 @@ impl Collection {
|
||||||
let file: fs::DirEntry = try!(file);
|
let file: fs::DirEntry = try!(file);
|
||||||
let file_meta = try!(file.metadata());
|
let file_meta = try!(file.metadata());
|
||||||
if file_meta.is_file() {
|
if file_meta.is_file() {
|
||||||
let song = try!(Song::read(self, &file));
|
let song = try!(Song::read(self, file.path().as_path()));
|
||||||
acc.push(song);
|
acc.push(song);
|
||||||
} else {
|
} else {
|
||||||
let explore_path = file.path();
|
let explore_path = file.path();
|
||||||
|
|
|
@ -46,6 +46,10 @@ impl Vfs {
|
||||||
}
|
}
|
||||||
Err(PError::PathNotInVfs)
|
Err(PError::PathNotInVfs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_mount_points(&self) -> &HashMap<String, PathBuf> {
|
||||||
|
return &self.mount_points;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue