get playlist

This commit is contained in:
jb-alvarado 2022-06-19 22:55:10 +02:00
parent 4834b47d74
commit fccee59d69
6 changed files with 67 additions and 22 deletions

View File

@ -7,8 +7,7 @@ use reqwest::{
use serde::{Deserialize, Serialize};
use simplelog::*;
use crate::api::{errors::ServiceError, handles::db_get_settings, utils::read_playout_config};
use crate::utils::PlayoutConfig;
use crate::api::{errors::ServiceError, utils::playout_config};
#[derive(Debug, Deserialize, Serialize, Clone)]
struct RpcObj<T> {
@ -56,18 +55,6 @@ fn create_header(auth: &str) -> HeaderMap {
headers
}
async fn playout_config(channel_id: &i64) -> Result<PlayoutConfig, ServiceError> {
if let Ok(settings) = db_get_settings(channel_id).await {
if let Ok(config) = read_playout_config(&settings.config_path) {
return Ok(config);
}
}
Err(ServiceError::BadRequest(
"Error in getting config!".to_string(),
))
}
async fn post_request<T>(id: i64, obj: RpcObj<T>) -> Result<Response, ServiceError>
where
T: Serialize,

View File

@ -4,5 +4,6 @@ pub mod control;
pub mod errors;
pub mod handles;
pub mod models;
pub mod playlist;
pub mod routes;
pub mod utils;

23
src/api/playlist.rs Normal file
View File

@ -0,0 +1,23 @@
use std::{fs::File, path::PathBuf};
use crate::api::{errors::ServiceError, utils::playout_config};
use crate::utils::JsonPlaylist;
pub async fn read_playlist(id: i64, date: String) -> Result<JsonPlaylist, ServiceError> {
let config = playout_config(&id).await?;
let mut playlist_path = PathBuf::from(&config.playlist.path);
let d: Vec<&str> = date.split('-').collect();
playlist_path = playlist_path
.join(d[0])
.join(d[1])
.join(date.clone())
.with_extension("json");
if let Ok(f) = File::options().read(true).write(false).open(&playlist_path) {
if let Ok(p) = serde_json::from_reader(f) {
return Ok(p);
}
};
Err(ServiceError::InternalServerError)
}

View File

@ -18,6 +18,7 @@ use crate::api::{
db_update_preset, db_update_settings, db_update_user,
},
models::{LoginUser, Settings, TextPreset, User},
playlist::read_playlist,
utils::{read_playout_config, Role},
};
@ -319,7 +320,7 @@ pub async fn reset_playout(id: web::Path<i64>) -> Result<impl Responder, Service
/// curl -X GET http://localhost:8080/api/control/1/media/current/
/// --header 'Content-Type: application/json' --header 'Authorization: <TOKEN>'
#[get("/control/{id}/media/current/")]
#[get("/control/{id}/media/current")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn media_current(id: web::Path<i64>) -> Result<impl Responder, ServiceError> {
match media_info(*id, "current".into()).await {
@ -330,7 +331,7 @@ pub async fn media_current(id: web::Path<i64>) -> Result<impl Responder, Service
/// curl -X GET http://localhost:8080/api/control/1/media/next/
/// --header 'Content-Type: application/json' --header 'Authorization: <TOKEN>'
#[get("/control/{id}/media/next/")]
#[get("/control/{id}/media/next")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn media_next(id: web::Path<i64>) -> Result<impl Responder, ServiceError> {
match media_info(*id, "next".into()).await {
@ -341,7 +342,7 @@ pub async fn media_next(id: web::Path<i64>) -> Result<impl Responder, ServiceErr
/// curl -X GET http://localhost:8080/api/control/1/media/last/
/// --header 'Content-Type: application/json' --header 'Authorization: <TOKEN>'
#[get("/control/{id}/media/last/")]
#[get("/control/{id}/media/last")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn media_last(id: web::Path<i64>) -> Result<impl Responder, ServiceError> {
match media_info(*id, "last".into()).await {
@ -349,3 +350,21 @@ pub async fn media_last(id: web::Path<i64>) -> Result<impl Responder, ServiceErr
Err(e) => Err(e),
}
}
/// ----------------------------------------------------------------------------
/// ffplayout playlist operations
///
/// ----------------------------------------------------------------------------
/// curl -X GET http://localhost:8080/api/playlist/1/2022-06-20
/// --header 'Content-Type: application/json' --header 'Authorization: <TOKEN>'
#[get("/playlist/{id}/{date}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn get_playlist(
params: web::Path<(i64, String)>,
) -> Result<impl Responder, ServiceError> {
match read_playlist(params.0, params.1.clone()).await {
Ok(playlist) => Ok(web::Json(playlist)),
Err(e) => Err(e),
}
}

View File

@ -6,7 +6,8 @@ use simplelog::*;
use crate::api::{
args_parse::Args,
handles::{db_add_user, db_global, db_init},
errors::ServiceError,
handles::{db_add_user, db_get_settings, db_global, db_init},
models::User,
};
use crate::utils::PlayoutConfig;
@ -120,3 +121,15 @@ pub fn read_playout_config(path: &str) -> Result<PlayoutConfig, Box<dyn Error>>
Ok(config)
}
pub async fn playout_config(channel_id: &i64) -> Result<PlayoutConfig, ServiceError> {
if let Ok(settings) = db_get_settings(channel_id).await {
if let Ok(config) = read_playout_config(&settings.config_path) {
return Ok(config);
}
}
Err(ServiceError::BadRequest(
"Error in getting config!".to_string(),
))
}

View File

@ -14,9 +14,10 @@ use ffplayout_engine::{
auth,
models::LoginUser,
routes::{
add_preset, add_user, get_playout_config, get_presets, get_settings, jump_to_last,
jump_to_next, login, media_current, media_last, media_next, patch_settings,
reset_playout, send_text_message, update_playout_config, update_preset, update_user,
add_preset, add_user, get_playlist, get_playout_config, get_presets, get_settings,
jump_to_last, jump_to_next, login, media_current, media_last, media_next,
patch_settings, reset_playout, send_text_message, update_playout_config, update_preset,
update_user,
},
utils::{db_path, init_config, run_args, Role},
},
@ -88,7 +89,8 @@ async fn main() -> std::io::Result<()> {
.service(reset_playout)
.service(media_current)
.service(media_next)
.service(media_last),
.service(media_last)
.service(get_playlist),
)
})
.bind((addr, port))?