Added API endpoint to read playlist content

This commit is contained in:
Antoine Gersant 2017-09-23 17:56:54 -07:00
parent 15d9e2f0b5
commit 66e772feb3
4 changed files with 35 additions and 15 deletions

View file

@ -155,6 +155,7 @@ fn get_endpoints(db: Arc<DB>, index_channel: Arc<Mutex<Sender<index::Command>>>)
let mut playlist_router = Router::new();
let put_db = db.clone();
let list_db = db.clone();
let read_db = db.clone();
playlist_router.put("/",
move |request: &mut Request| {
self::save_playlist(request, put_db.deref())
@ -167,6 +168,12 @@ fn get_endpoints(db: Arc<DB>, index_channel: Arc<Mutex<Sender<index::Command>>>)
},
"list_playlists");
playlist_router.get("/read/:playlist_name",
move |request: &mut Request| {
self::read_playlist(request, read_db.deref())
},
"read_playlist");
auth_api_mount.mount("/playlist/", playlist_router);
}
@ -546,3 +553,25 @@ fn list_playlists(request: &mut Request, db: &DB) -> IronResult<Response> {
};
Ok(Response::with((status::Ok, result_json)))
}
fn read_playlist(request: &mut Request, db: &DB) -> IronResult<Response> {
let username = match request.extensions.get::<SessionKey>() {
Some(s) => s.username.clone(),
None => return Err(Error::from(ErrorKind::AuthenticationRequired).into()),
};
let params = request.extensions.get::<Router>().unwrap();
let ref playlist_name = match params.find("playlist_name") {
Some(s) => s,
_ => return Err(Error::from(ErrorKind::MissingPlaylistName).into()),
};
let songs = playlist::read_playlist(&playlist_name, &username, db)?;
let result_json = serde_json::to_string(&songs);
let result_json = match result_json {
Ok(j) => j,
Err(e) => return Err(IronError::new(e, status::InternalServerError)),
};
Ok(Response::with((status::Ok, result_json)))
}

View file

@ -47,6 +47,7 @@ error_chain! {
UnsupportedFileType {}
FileNotFound {}
MissingIndexVersion {}
MissingPlaylistName {}
}
}

View file

@ -444,7 +444,7 @@ pub fn self_trigger<T>(db: &T, command_buffer: Arc<Mutex<Sender<Command>>>)
}
}
fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> {
pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option<Song> {
song.path = match vfs.real_to_virtual(Path::new(&song.path)) {
Ok(p) => p.to_string_lossy().into_owned(),
_ => return None,

View file

@ -10,7 +10,7 @@ use std::path::Path;
use db;
use db::ConnectionSource;
use db::{playlists, playlist_songs, songs, users};
use index::Song;
use index::{self, Song};
use vfs::VFSSource;
use errors::*;
@ -189,22 +189,12 @@ pub fn read_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<Vec<
}
// Map real path to virtual paths
let songs = songs
let virtual_songs = songs
.into_iter()
.filter_map(|mut s| {
let real_path = s.path.clone();
let real_path = Path::new(&real_path);
if let Ok(virtual_path) = vfs.real_to_virtual(real_path) {
if let Some(virtual_path) = virtual_path.to_str() {
s.path = virtual_path.to_owned();
}
return Some(s);
}
None
})
.filter_map(|s| index::virtualize_song(&vfs, s))
.collect();
Ok(songs)
Ok(virtual_songs)
}
pub fn delete_playlist<T>(playlist_name: &str, owner: &str, db: &T) -> Result<()>