diff --git a/src/api/control.rs b/src/api/control.rs index d803c6af..4f8b8ad8 100644 --- a/src/api/control.rs +++ b/src/api/control.rs @@ -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 { @@ -56,18 +55,6 @@ fn create_header(auth: &str) -> HeaderMap { headers } -async fn playout_config(channel_id: &i64) -> Result { - 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(id: i64, obj: RpcObj) -> Result where T: Serialize, diff --git a/src/api/mod.rs b/src/api/mod.rs index 7bef2175..acc618e1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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; diff --git a/src/api/playlist.rs b/src/api/playlist.rs new file mode 100644 index 00000000..b2bdbdec --- /dev/null +++ b/src/api/playlist.rs @@ -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 { + 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) +} diff --git a/src/api/routes.rs b/src/api/routes.rs index 3bc14007..fcaf0433 100644 --- a/src/api/routes.rs +++ b/src/api/routes.rs @@ -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) -> Result' -#[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) -> Result { match media_info(*id, "current".into()).await { @@ -330,7 +331,7 @@ pub async fn media_current(id: web::Path) -> Result' -#[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) -> Result { match media_info(*id, "next".into()).await { @@ -341,7 +342,7 @@ pub async fn media_next(id: web::Path) -> Result' -#[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) -> Result { match media_info(*id, "last".into()).await { @@ -349,3 +350,21 @@ pub async fn media_last(id: web::Path) -> Result Err(e), } } + +/// ---------------------------------------------------------------------------- +/// ffplayout playlist operations +/// +/// ---------------------------------------------------------------------------- + +/// curl -X GET http://localhost:8080/api/playlist/1/2022-06-20 +/// --header 'Content-Type: application/json' --header 'Authorization: ' +#[get("/playlist/{id}/{date}")] +#[has_any_role("Role::Admin", "Role::User", type = "Role")] +pub async fn get_playlist( + params: web::Path<(i64, String)>, +) -> Result { + match read_playlist(params.0, params.1.clone()).await { + Ok(playlist) => Ok(web::Json(playlist)), + Err(e) => Err(e), + } +} diff --git a/src/api/utils.rs b/src/api/utils.rs index 1838fc19..231a157b 100644 --- a/src/api/utils.rs +++ b/src/api/utils.rs @@ -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> Ok(config) } + +pub async fn playout_config(channel_id: &i64) -> Result { + 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(), + )) +} diff --git a/src/bin/ffpapi.rs b/src/bin/ffpapi.rs index b66d6bfb..216dfe56 100644 --- a/src/bin/ffpapi.rs +++ b/src/bin/ffpapi.rs @@ -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))?