Added new API endpoint for random albums
This commit is contained in:
parent
ef3d55cdbc
commit
5ed95d1417
3 changed files with 56 additions and 14 deletions
18
src/api.rs
18
src/api.rs
|
@ -19,7 +19,7 @@ use thumbnails::*;
|
|||
use utils::*;
|
||||
|
||||
const CURRENT_MAJOR_VERSION: i32 = 1;
|
||||
const CURRENT_MINOR_VERSION: i32 = 0;
|
||||
const CURRENT_MINOR_VERSION: i32 = 1;
|
||||
|
||||
#[derive(RustcEncodable)]
|
||||
struct Version {
|
||||
|
@ -60,6 +60,12 @@ pub fn get_api_handler(collection: Arc<Collection>) -> Mount {
|
|||
self::flatten(request, collection.deref())
|
||||
});
|
||||
}
|
||||
{
|
||||
let collection = collection.clone();
|
||||
auth_api_mount.mount("/random/", move |request: &mut Request| {
|
||||
self::random(request, collection.deref())
|
||||
});
|
||||
}
|
||||
{
|
||||
let collection = collection.clone();
|
||||
auth_api_mount.mount("/serve/", move |request: &mut Request| {
|
||||
|
@ -154,6 +160,16 @@ fn flatten(request: &mut Request, collection: &Collection) -> IronResult<Respons
|
|||
Ok(Response::with((status::Ok, result_json)))
|
||||
}
|
||||
|
||||
fn random(_: &mut Request, collection: &Collection) -> IronResult<Response> {
|
||||
let random_result = collection.get_random_albums(20)?;
|
||||
let result_json = json::encode(&random_result);
|
||||
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)))
|
||||
}
|
||||
|
||||
fn serve(request: &mut Request, collection: &Collection) -> IronResult<Response> {
|
||||
let virtual_path = path_from_request(request);
|
||||
let virtual_path = match virtual_path {
|
||||
|
|
|
@ -56,6 +56,10 @@ impl Collection {
|
|||
self.index.deref().flatten(virtual_path)
|
||||
}
|
||||
|
||||
pub fn get_random_albums(&self, count: u32) -> Result<Vec<Directory>> {
|
||||
self.index.deref().get_random_albums(count)
|
||||
}
|
||||
|
||||
pub fn locate(&self, virtual_path: &Path) -> Result<PathBuf> {
|
||||
self.vfs.virtual_to_real(virtual_path)
|
||||
}
|
||||
|
|
48
src/index.rs
48
src/index.rs
|
@ -480,18 +480,9 @@ impl Index {
|
|||
Ok(output)
|
||||
}
|
||||
|
||||
// List sub-directories within a directory
|
||||
fn browse_directories(&self, real_path: &Path) -> Result<Vec<CollectionFile>> {
|
||||
let db = self.connect()?;
|
||||
let mut output = Vec::new();
|
||||
|
||||
let path_string = real_path.to_string_lossy();
|
||||
let mut select =
|
||||
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
|
||||
parent = ? ORDER BY path COLLATE NOCASE ASC")?;
|
||||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
|
||||
while let State::Row = select.next()? {
|
||||
fn select_directories(&self, select: &mut Statement) -> Result<Vec<Directory>> {
|
||||
let mut output = Vec::new();
|
||||
while let State::Row = select.next()? {
|
||||
|
||||
let directory_value: String = select.read(0)?;
|
||||
let artwork_path: Value = select.read(1)?;
|
||||
|
@ -521,9 +512,24 @@ impl Index {
|
|||
artist: artist.as_string().map(|s| s.to_owned()),
|
||||
album: album.as_string().map(|s| s.to_owned()),
|
||||
};
|
||||
output.push(CollectionFile::Directory(directory));
|
||||
output.push(directory);
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
// List sub-directories within a directory
|
||||
fn browse_directories(&self, real_path: &Path) -> Result<Vec<CollectionFile>> {
|
||||
let db = self.connect()?;
|
||||
|
||||
let path_string = real_path.to_string_lossy();
|
||||
let mut select =
|
||||
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
|
||||
parent = ? ORDER BY path COLLATE NOCASE ASC")?;
|
||||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
|
||||
let output = self.select_directories(&mut select)?;
|
||||
let output = output.into_iter().map(|d| CollectionFile::Directory(d)).collect();
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
|
@ -579,6 +585,14 @@ impl Index {
|
|||
select.bind(1, &Value::String(path_string.deref().to_owned()))?;
|
||||
self.select_songs(&mut select)
|
||||
}
|
||||
|
||||
pub fn get_random_albums(&self, count: u32) -> Result<Vec<Directory>> {
|
||||
let db = self.connect()?;
|
||||
let mut select =
|
||||
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album IS NOT NULL ORDER BY RANDOM() LIMIT ?")?;
|
||||
select.bind(1, &Value::Integer(count as i64))?;
|
||||
self.select_directories(&mut select)
|
||||
}
|
||||
}
|
||||
|
||||
fn _get_test_index(name: &str) -> Index {
|
||||
|
@ -683,3 +697,11 @@ fn test_flatten() {
|
|||
let results = index.flatten(Path::new("root")).unwrap();
|
||||
assert_eq!(results.len(), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_random() {
|
||||
let index = _get_test_index("random.sqlite");
|
||||
index.update_index().unwrap();
|
||||
let results = index.get_random_albums(1).unwrap();
|
||||
assert_eq!(results.len(), 1);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue