From 66e772feb3da86d6acc5f71f07518e1a1e4429c9 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sat, 23 Sep 2017 17:56:54 -0700 Subject: [PATCH] Added API endpoint to read playlist content --- src/api.rs | 29 +++++++++++++++++++++++++++++ src/errors.rs | 1 + src/index.rs | 2 +- src/playlist.rs | 18 ++++-------------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/api.rs b/src/api.rs index 987524d..7b80f8d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -155,6 +155,7 @@ fn get_endpoints(db: Arc, index_channel: Arc>>) 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, index_channel: Arc>>) }, "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 { }; Ok(Response::with((status::Ok, result_json))) } + +fn read_playlist(request: &mut Request, db: &DB) -> IronResult { + let username = match request.extensions.get::() { + Some(s) => s.username.clone(), + None => return Err(Error::from(ErrorKind::AuthenticationRequired).into()), + }; + + let params = request.extensions.get::().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))) +} diff --git a/src/errors.rs b/src/errors.rs index c63362e..1607e80 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -47,6 +47,7 @@ error_chain! { UnsupportedFileType {} FileNotFound {} MissingIndexVersion {} + MissingPlaylistName {} } } diff --git a/src/index.rs b/src/index.rs index 91d8337..1bd70e3 100644 --- a/src/index.rs +++ b/src/index.rs @@ -444,7 +444,7 @@ pub fn self_trigger(db: &T, command_buffer: Arc>>) } } -fn virtualize_song(vfs: &VFS, mut song: Song) -> Option { +pub fn virtualize_song(vfs: &VFS, mut song: Song) -> Option { song.path = match vfs.real_to_virtual(Path::new(&song.path)) { Ok(p) => p.to_string_lossy().into_owned(), _ => return None, diff --git a/src/playlist.rs b/src/playlist.rs index 2ad37c9..691a843 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -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(playlist_name: &str, owner: &str, db: &T) -> Result(playlist_name: &str, owner: &str, db: &T) -> Result<()>