Implemented flatten

This commit is contained in:
Antoine Gersant 2016-10-30 17:19:57 -07:00
parent c47ae7899f
commit 462cd8babb
2 changed files with 39 additions and 3 deletions

View file

@ -52,8 +52,8 @@ impl Collection {
self.index.deref().browse(virtual_path)
}
pub fn flatten(&self, path: &Path) -> Result<Vec<Song>, PError> {
Err(PError::Unauthorized)
pub fn flatten(&self, virtual_path: &Path) -> Result<Vec<Song>, PError> {
self.index.deref().flatten(virtual_path)
}
pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf, PError> {

View file

@ -372,7 +372,7 @@ impl Index {
let mut cursor = db.prepare("SELECT path, track_number, title, year, album_artist, artist, album, artwork FROM songs WHERE parent = ?").unwrap().cursor();
cursor.bind(&[sqlite::Value::String(path_string.deref().to_owned())]).unwrap();
while let Some(row) = cursor.next().unwrap() {
while let Ok(Some(row)) = cursor.next() {
let song_path = Path::new(row[0].as_string().unwrap());
let song_path = match self.vfs.real_to_virtual(song_path) {
@ -426,4 +426,40 @@ impl Index {
Ok(output)
}
pub fn flatten(&self, virtual_path: &Path) -> Result<Vec<Song>, PError> {
let db = self.connect();
let mut output = Vec::new();
let real_path = try!(self.vfs.virtual_to_real(virtual_path));
let path_string = real_path.to_string_lossy().into_owned() + "%";
let mut cursor = db.prepare("SELECT path, track_number, title, year, album_artist, artist, album, artwork FROM songs WHERE path LIKE ?").unwrap().cursor();
cursor.bind(&[sqlite::Value::String(path_string.deref().to_owned())]).unwrap();
while let Ok(Some(row)) = cursor.next() {
let song_path = Path::new(row[0].as_string().unwrap());
let song_path = match self.vfs.real_to_virtual(song_path) {
Ok(p) => p,
_ => continue,
};
let artwork_path = row[7].as_string().map(|p| Path::new(p)).and_then(|p| self.vfs.real_to_virtual(p).ok());
let song = Song {
path: song_path.to_str().unwrap().to_owned(),
track_number: row[1].as_integer().map(|n| n as u32),
title: row[2].as_string().map(|s| s.to_owned()),
year: row[3].as_integer().map(|n| n as i32),
album_artist: row[4].as_string().map(|s| s.to_owned()),
artist: row[5].as_string().map(|s| s.to_owned()),
album: row[6].as_string().map(|s| s.to_owned()),
artwork: artwork_path.map(|p| p.to_str().unwrap().to_owned() ),
};
output.push(song);
}
Ok(output)
}
}