1016 lines
30 KiB
Rust
Raw Normal View History

/// ### Possible endpoints
///
/// Run the API thru the systemd service, or like:
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// ffpapi -l 127.0.0.1:8787
/// ```
///
/// For all endpoints an (Bearer) authentication is required.\
/// `{id}` represent the channel id, and at default is 1.
use std::{collections::HashMap, env, fs, path::Path};
use actix_multipart::Multipart;
use actix_web::{delete, get, http::StatusCode, patch, post, put, web, HttpResponse, Responder};
use actix_web_grants::{permissions::AuthDetails, proc_macro::has_any_role};
2022-06-10 16:12:30 +02:00
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, SaltString},
Argon2, PasswordHasher, PasswordVerifier,
};
2022-11-13 21:18:48 +01:00
use chrono::{DateTime, Datelike, Duration, Local, NaiveDateTime, TimeZone, Utc};
2022-11-13 17:34:01 +01:00
use regex::Regex;
use serde::{Deserialize, Serialize};
2022-06-07 22:05:35 +02:00
use simplelog::*;
2022-11-18 14:13:24 +01:00
use sqlx::{Pool, Sqlite};
2022-06-06 23:07:11 +02:00
use crate::auth::{create_jwt, Claims};
use crate::db::{
handles,
models::{Channel, LoginUser, TextPreset, User},
};
2022-06-21 23:10:38 +02:00
use crate::utils::{
2022-07-18 22:55:18 +02:00
channels::{create_channel, delete_channel},
2022-06-24 17:41:55 +02:00
control::{control_service, control_state, media_info, send_message, Process},
2022-06-21 23:10:38 +02:00
errors::ServiceError,
files::{
browser, create_directory, remove_file_or_folder, rename_file, upload, MoveObject,
PathObject,
},
2022-11-13 17:34:01 +01:00
naive_date_time_from_str,
2022-06-21 23:10:38 +02:00
playlist::{delete_playlist, generate_playlist, read_playlist, write_playlist},
playout_config, read_log_file, read_playout_config, Role,
2022-06-09 18:59:14 +02:00
};
2022-11-13 17:34:01 +01:00
use ffplayout_lib::{
utils::{
get_date_range, import::import_file, sec_to_time, time_to_sec, JsonPlaylist, PlayoutConfig,
},
vec_strings,
};
2022-06-06 23:07:11 +02:00
2022-06-08 18:06:40 +02:00
#[derive(Serialize)]
struct ResponseObj<T> {
message: String,
status: i32,
data: Option<T>,
2022-06-07 18:11:46 +02:00
}
2022-06-08 18:06:40 +02:00
#[derive(Serialize)]
struct UserObj<T> {
message: String,
user: Option<T>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DateObj {
#[serde(default)]
date: String,
}
#[derive(Debug, Deserialize, Serialize)]
struct FileObj {
#[serde(default)]
path: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ImportObj {
#[serde(default)]
file: String,
#[serde(default)]
date: String,
}
2022-11-13 21:18:48 +01:00
#[derive(Debug, Deserialize, Clone)]
2022-11-13 17:34:01 +01:00
pub struct ProgramObj {
#[serde(default = "time_after", deserialize_with = "naive_date_time_from_str")]
2022-11-13 21:18:48 +01:00
start_after: NaiveDateTime,
#[serde(default = "time_before", deserialize_with = "naive_date_time_from_str")]
2022-11-13 21:18:48 +01:00
start_before: NaiveDateTime,
}
fn time_after() -> NaiveDateTime {
2022-11-13 21:18:48 +01:00
let today = Utc::now();
chrono::Local
2022-11-19 20:34:29 +01:00
.with_ymd_and_hms(today.year(), today.month(), today.day(), 0, 0, 0)
.unwrap()
2022-11-13 21:18:48 +01:00
.naive_local()
}
fn time_before() -> NaiveDateTime {
2022-11-13 21:18:48 +01:00
let today = Utc::now();
chrono::Local
2022-11-19 20:34:29 +01:00
.with_ymd_and_hms(today.year(), today.month(), today.day(), 23, 59, 59)
.unwrap()
2022-11-13 21:18:48 +01:00
.naive_local()
2022-11-13 17:34:01 +01:00
}
#[derive(Debug, Serialize)]
2022-11-13 21:18:48 +01:00
struct ProgramItem {
2022-11-13 17:34:01 +01:00
source: String,
start: String,
r#in: f64,
out: f64,
duration: f64,
category: String,
}
/// #### User Handling
///
/// **Login**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/auth/login/ -H "Content-Type: application/json" \
/// -d '{ "username": "<USER>", "password": "<PASS>" }'
/// ```
/// **Response:**
///
/// ```JSON
/// {
/// "id": 1,
/// "mail": "user@example.org",
/// "username": "<USER>",
/// "token": "<TOKEN>"
/// }
/// ```
2022-06-22 21:25:46 +02:00
#[post("/auth/login/")]
2022-11-19 20:54:46 +01:00
pub async fn login(pool: web::Data<Pool<Sqlite>>, credentials: web::Json<User>) -> impl Responder {
let conn = pool.into_inner();
match handles::select_login(&conn, &credentials.username).await {
2022-06-22 21:25:46 +02:00
Ok(mut user) => {
let pass = user.password.clone();
let hash = PasswordHash::new(&pass).unwrap();
user.password = "".into();
user.salt = None;
if Argon2::default()
.verify_password(credentials.password.as_bytes(), &hash)
.is_ok()
{
2022-11-19 20:54:46 +01:00
let role = handles::select_role(&conn, &user.role_id.unwrap_or_default())
2022-06-22 21:25:46 +02:00
.await
.unwrap_or_else(|_| "guest".to_string());
let claims = Claims::new(user.id, user.username.clone(), role.clone());
if let Ok(token) = create_jwt(claims) {
user.token = Some(token);
};
info!("user {} login, with role: {role}", credentials.username);
web::Json(UserObj {
2022-06-22 21:25:46 +02:00
message: "login correct!".into(),
user: Some(user),
2022-06-22 21:25:46 +02:00
})
.customize()
.with_status(StatusCode::OK)
} else {
error!("Wrong password for {}!", credentials.username);
web::Json(UserObj {
2022-06-22 21:25:46 +02:00
message: "Wrong password!".into(),
user: None,
2022-06-22 21:25:46 +02:00
})
.customize()
.with_status(StatusCode::FORBIDDEN)
}
}
Err(e) => {
error!("Login {} failed! {e}", credentials.username);
web::Json(UserObj {
2022-06-22 21:25:46 +02:00
message: format!("Login {} failed!", credentials.username),
user: None,
2022-06-22 21:25:46 +02:00
})
.customize()
.with_status(StatusCode::BAD_REQUEST)
2022-06-22 21:25:46 +02:00
}
}
}
/// From here on all request **must** contain the authorization header:\
/// `"Authorization: Bearer <TOKEN>"`
/// **Get current User**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET 'http://127.0.0.1:8787/api/user' -H 'Content-Type: application/json' \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/user")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn get_user(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
user: web::ReqData<LoginUser>,
) -> Result<impl Responder, ServiceError> {
match handles::select_user(&pool.into_inner(), &user.username).await {
Ok(user) => Ok(web::Json(user)),
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}
/// **Update current User**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X PUT http://127.0.0.1:8787/api/user/1 -H 'Content-Type: application/json' \
2022-11-08 15:42:17 +01:00
/// -d '{"mail": "<MAIL>", "password": "<PASS>"}' -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-22 21:25:46 +02:00
#[put("/user/{id}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn update_user(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-06-22 21:25:46 +02:00
user: web::ReqData<LoginUser>,
data: web::Json<User>,
) -> Result<impl Responder, ServiceError> {
if id.into_inner() == user.id {
let mut fields = String::new();
if let Some(mail) = data.mail.clone() {
fields.push_str(format!("mail = '{mail}'").as_str());
2022-06-22 21:25:46 +02:00
}
if !data.password.is_empty() {
if !fields.is_empty() {
fields.push_str(", ");
}
let salt = SaltString::generate(&mut OsRng);
let password_hash = Argon2::default()
.hash_password(data.password.clone().as_bytes(), &salt)
.unwrap();
fields.push_str(format!("password = '{}', salt = '{salt}'", password_hash).as_str());
}
2022-11-18 14:13:24 +01:00
if handles::update_user(&pool.into_inner(), user.id, fields)
.await
.is_ok()
{
2022-06-22 21:25:46 +02:00
return Ok("Update Success");
};
return Err(ServiceError::InternalServerError);
}
Err(ServiceError::Unauthorized)
}
/// **Add User**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST 'http://127.0.0.1:8787/api/user/' -H 'Content-Type: application/json' \
/// -d '{"mail": "<MAIL>", "username": "<USER>", "password": "<PASS>", "role_id": 1, "channel_id": 1}' \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-22 21:25:46 +02:00
#[post("/user/")]
#[has_any_role("Role::Admin", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn add_user(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
data: web::Json<User>,
) -> Result<impl Responder, ServiceError> {
match handles::insert_user(&pool.into_inner(), data.into_inner()).await {
2022-06-22 21:25:46 +02:00
Ok(_) => Ok("Add User Success"),
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}
/// #### ffpapi Settings
///
2022-07-18 22:55:18 +02:00
/// **Get Settings from Channel**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/channel/1 -H "Authorization: Bearer <TOKEN>"
/// ```
///
/// **Response:**
///
/// ```JSON
/// {
/// "id": 1,
/// "name": "Channel 1",
/// "preview_url": "http://localhost/live/preview.m3u8",
/// "config_path": "/etc/ffplayout/ffplayout.yml",
/// "extra_extensions": "jpg,jpeg,png",
2022-09-04 17:07:16 +02:00
/// "service": "ffplayout.service",
/// "utc_offset": "+120"
/// }
/// ```
2022-07-18 22:55:18 +02:00
#[get("/channel/{id}")]
2022-06-13 18:29:37 +02:00
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn get_channel(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
if let Ok(channel) = handles::select_channel(&pool.into_inner(), &id).await {
2022-07-18 22:55:18 +02:00
return Ok(web::Json(channel));
}
Err(ServiceError::InternalServerError)
}
2022-07-18 22:55:18 +02:00
/// **Get settings from all Channels**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/channels -H "Authorization: Bearer <TOKEN>"
/// ```
2022-07-18 22:55:18 +02:00
#[get("/channels")]
#[has_any_role("Role::Admin", type = "Role")]
2022-11-19 20:54:46 +01:00
async fn get_all_channels(pool: web::Data<Pool<Sqlite>>) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
if let Ok(channel) = handles::select_all_channels(&pool.into_inner()).await {
2022-07-18 22:55:18 +02:00
return Ok(web::Json(channel));
2022-06-12 22:37:29 +02:00
}
Err(ServiceError::InternalServerError)
}
2022-07-18 22:55:18 +02:00
/// **Update Channel**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X PATCH http://127.0.0.1:8787/api/channel/1 -H "Content-Type: application/json" \
/// -d '{ "id": 1, "name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \
2022-09-04 17:07:16 +02:00
/// "config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png"}' \
2022-06-12 22:37:29 +02:00
/// -H "Authorization: Bearer <TOKEN>"
/// ```
2022-07-18 22:55:18 +02:00
#[patch("/channel/{id}")]
2022-06-13 18:29:37 +02:00
#[has_any_role("Role::Admin", type = "Role")]
2022-07-18 22:55:18 +02:00
async fn patch_channel(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-07-18 22:55:18 +02:00
data: web::Json<Channel>,
2022-06-12 22:37:29 +02:00
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
if handles::update_channel(&pool.into_inner(), *id, data.into_inner())
.await
.is_ok()
{
2022-06-12 22:37:29 +02:00
return Ok("Update Success");
};
Err(ServiceError::InternalServerError)
2022-06-09 18:59:14 +02:00
}
2022-07-18 22:55:18 +02:00
/// **Create new Channel**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/channel/ -H "Content-Type: application/json" \
/// -d '{ "name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \
2022-07-18 22:55:18 +02:00
/// "config_path": "/etc/ffplayout/channel2.yml", "extra_extensions": "jpg,jpeg,png",
2022-09-04 17:07:16 +02:00
/// "service": "ffplayout@channel2.service" }' \
2022-07-18 22:55:18 +02:00
/// -H "Authorization: Bearer <TOKEN>"
/// ```
#[post("/channel/")]
#[has_any_role("Role::Admin", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn add_channel(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
data: web::Json<Channel>,
) -> Result<impl Responder, ServiceError> {
match create_channel(&pool.into_inner(), data.into_inner()).await {
2022-07-18 22:55:18 +02:00
Ok(c) => Ok(web::Json(c)),
Err(e) => Err(e),
}
}
/// **Delete Channel**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X DELETE http://127.0.0.1:8787/api/channel/2 -H "Authorization: Bearer <TOKEN>"
2022-07-18 22:55:18 +02:00
/// ```
#[delete("/channel/{id}")]
#[has_any_role("Role::Admin", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn remove_channel(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
if delete_channel(&pool.into_inner(), *id).await.is_ok() {
2022-07-18 22:55:18 +02:00
return Ok("Delete Channel Success");
}
Err(ServiceError::InternalServerError)
}
/// #### ffplayout Config
///
/// **Get Config**
///
/// ```BASH
2022-11-08 15:42:17 +01:00
/// curl -X GET http://127.0.0.1:8787/api/playout/config/1 -H 'Authorization: Bearer <TOKEN>'
/// ```
///
/// Response is a JSON object from the ffplayout.yml
2022-06-13 18:29:37 +02:00
#[get("/playout/config/{id}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn get_playout_config(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-06-15 17:32:39 +02:00
_details: AuthDetails<Role>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
if let Ok(channel) = handles::select_channel(&pool.into_inner(), &id).await {
2022-07-18 22:55:18 +02:00
if let Ok(config) = read_playout_config(&channel.config_path) {
2022-06-15 17:32:39 +02:00
return Ok(web::Json(config));
}
2022-06-13 18:29:37 +02:00
};
Err(ServiceError::InternalServerError)
}
/// **Update Config**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X PUT http://127.0.0.1:8787/api/playout/config/1 -H "Content-Type: application/json" \
2022-11-08 15:42:17 +01:00
/// -d { <CONFIG DATA> } -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-15 17:32:39 +02:00
#[put("/playout/config/{id}")]
#[has_any_role("Role::Admin", type = "Role")]
async fn update_playout_config(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-06-15 17:32:39 +02:00
data: web::Json<PlayoutConfig>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
if let Ok(channel) = handles::select_channel(&pool.into_inner(), &id).await {
2022-06-15 17:32:39 +02:00
if let Ok(f) = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(channel.config_path)
2022-06-15 17:32:39 +02:00
{
serde_yaml::to_writer(f, &data).unwrap();
return Ok("Update playout config success.");
} else {
return Err(ServiceError::InternalServerError);
};
};
Err(ServiceError::InternalServerError)
}
/// #### Text Presets
///
/// Text presets are made for sending text messages to the ffplayout engine, to overlay them as a lower third.
///
/// **Get all Presets**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/presets/ -H 'Content-Type: application/json' \
2022-11-08 15:42:17 +01:00
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/presets/{id}")]
2022-06-16 19:34:43 +02:00
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn get_presets(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
if let Ok(presets) = handles::select_presets(&pool.into_inner(), *id).await {
2022-06-16 19:34:43 +02:00
return Ok(web::Json(presets));
}
Err(ServiceError::InternalServerError)
}
/// **Update Preset**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X PUT http://127.0.0.1:8787/api/presets/1 -H 'Content-Type: application/json' \
/// -d '{ "name": "<PRESET NAME>", "text": "<TEXT>", "x": "<X>", "y": "<Y>", "fontsize": 24, \
/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0, "channel_id": 1 }' \
2022-11-08 15:42:17 +01:00
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-16 19:34:43 +02:00
#[put("/presets/{id}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn update_preset(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-06-17 16:21:03 +02:00
data: web::Json<TextPreset>,
2022-06-16 19:34:43 +02:00
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
if handles::update_preset(&pool.into_inner(), &id, data.into_inner())
.await
.is_ok()
{
2022-06-16 19:34:43 +02:00
return Ok("Update Success");
}
Err(ServiceError::InternalServerError)
}
/// **Add new Preset**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/presets/ -H 'Content-Type: application/json' \
/// -d '{ "name": "<PRESET NAME>", "text": "TEXT>", "x": "<X>", "y": "<Y>", "fontsize": 24, \
/// "line_spacing": 4, "fontcolor": "#ffffff", "box": 1, "boxcolor": "#000000", "boxborderw": 4, "alpha": 1.0, "channel_id": 1 }' \
2022-11-08 15:42:17 +01:00
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-16 19:34:43 +02:00
#[post("/presets/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn add_preset(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
data: web::Json<TextPreset>,
) -> Result<impl Responder, ServiceError> {
if handles::insert_preset(&pool.into_inner(), data.into_inner())
.await
.is_ok()
{
2022-06-16 19:34:43 +02:00
return Ok("Add preset Success");
}
Err(ServiceError::InternalServerError)
}
/// **Delete Preset**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X DELETE http://127.0.0.1:8787/api/presets/1 -H 'Content-Type: application/json' \
2022-11-08 15:42:17 +01:00
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
#[delete("/presets/{id}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
async fn delete_preset(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
if handles::delete_preset(&pool.into_inner(), &id)
.await
.is_ok()
{
return Ok("Delete preset Success");
}
Err(ServiceError::InternalServerError)
}
/// ### ffplayout controlling
2022-06-17 16:21:03 +02:00
///
/// here we communicate with the engine for:
/// - jump to last or next clip
/// - reset playlist state
/// - get infos about current, next, last clip
/// - send text to the engine, for overlaying it (as lower third etc.)
///
/// **Send Text to ffplayout**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/control/1/text/ \
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>' \
/// -d '{"text": "Hello from ffplayout", "x": "(w-text_w)/2", "y": "(h-text_h)/2", \
/// "fontsize": "24", "line_spacing": "4", "fontcolor": "#ffffff", "box": "1", \
/// "boxcolor": "#000000", "boxborderw": "4", "alpha": "1.0"}'
/// ```
#[post("/control/{id}/text/")]
2022-06-17 16:21:03 +02:00
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn send_text_message(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<HashMap<String, String>>,
2022-06-17 16:21:03 +02:00
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match send_message(&pool.into_inner(), *id, data.into_inner()).await {
Ok(res) => Ok(res.text().await.unwrap_or_else(|_| "Success".into())),
2022-06-17 16:21:03 +02:00
Err(e) => Err(e),
}
}
/// **Control Playout**
///
/// - next
/// - back
/// - reset
///
/// ```BASH
/// curl -X POST http://127.0.0.1:8787/api/control/1/playout/ -H 'Content-Type: application/json'
2022-11-08 15:42:17 +01:00
/// -d '{ "command": "reset" }' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[post("/control/{id}/playout/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-07-07 21:45:33 +02:00
pub async fn control_playout(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-07-07 21:45:33 +02:00
control: web::Json<Process>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match control_state(&pool.into_inner(), *id, control.command.clone()).await {
Ok(res) => Ok(res.text().await.unwrap_or_else(|_| "Success".into())),
Err(e) => Err(e),
}
}
/// **Get current Clip**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/control/1/media/current
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
///
/// **Response:**
///
/// ```JSON
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "current_media": {
/// "category": "",
/// "duration": 154.2,
/// "out": 154.2,
/// "seek": 0.0,
/// "source": "/opt/tv-media/clip.mp4"
/// },
/// "index": 39,
/// "play_mode": "playlist",
/// "played_sec": 67.80771999300123,
/// "remaining_sec": 86.39228000699876,
/// "start_sec": 24713.631999999998,
/// "start_time": "06:51:53.631"
/// },
/// "id": 1
/// }
/// ```
2022-06-19 22:55:10 +02:00
#[get("/control/{id}/media/current")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
pub async fn media_current(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
match media_info(&pool.into_inner(), *id, "current".into()).await {
Ok(res) => Ok(res.text().await.unwrap_or_else(|_| "Success".into())),
Err(e) => Err(e),
}
}
/// **Get next Clip**
///
/// ```BASH
2022-11-08 15:42:17 +01:00
/// curl -X GET http://127.0.0.1:8787/api/control/1/media/next/ -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-19 22:55:10 +02:00
#[get("/control/{id}/media/next")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
pub async fn media_next(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
match media_info(&pool.into_inner(), *id, "next".into()).await {
Ok(res) => Ok(res.text().await.unwrap_or_else(|_| "Success".into())),
Err(e) => Err(e),
}
}
/// **Get last Clip**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/control/1/media/last/
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-19 22:55:10 +02:00
#[get("/control/{id}/media/last")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
2022-11-18 14:13:24 +01:00
pub async fn media_last(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-18 14:13:24 +01:00
id: web::Path<i32>,
) -> Result<impl Responder, ServiceError> {
match media_info(&pool.into_inner(), *id, "last".into()).await {
Ok(res) => Ok(res.text().await.unwrap_or_else(|_| "Success".into())),
Err(e) => Err(e),
}
}
2022-06-19 22:55:10 +02:00
/// #### ffplayout Process Control
///
/// Control ffplayout process, like:
/// - start
/// - stop
/// - restart
/// - status
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/control/1/process/
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
2022-06-24 17:41:55 +02:00
/// -d '{"command": "start"}'
/// ```
2022-06-24 17:41:55 +02:00
#[post("/control/{id}/process/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn process_control(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
2022-06-24 17:41:55 +02:00
proc: web::Json<Process>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
control_service(&pool.into_inner(), *id, &proc.command).await
2022-06-24 17:41:55 +02:00
}
/// #### ffplayout Playlist Operations
2022-06-19 22:55:10 +02:00
///
/// **Get playlist**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/playlist/1?date=2022-06-20
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/playlist/{id}")]
2022-06-19 22:55:10 +02:00
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn get_playlist(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
obj: web::Query<DateObj>,
2022-06-19 22:55:10 +02:00
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match read_playlist(&pool.into_inner(), *id, obj.date.clone()).await {
2022-06-19 22:55:10 +02:00
Ok(playlist) => Ok(web::Json(playlist)),
Err(e) => Err(e),
}
}
/// **Save playlist**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/playlist/1/
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// -- data "{<JSON playlist data>}"
/// ```
#[post("/playlist/{id}/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn save_playlist(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<JsonPlaylist>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match write_playlist(&pool.into_inner(), *id, data.into_inner()).await {
Ok(res) => Ok(res),
Err(e) => Err(e),
}
}
/// **Generate Playlist**
///
/// A new playlist will be generated and response.
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X GET http://127.0.0.1:8787/api/playlist/1/generate/2022-06-20
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-06-21 17:56:10 +02:00
#[get("/playlist/{id}/generate/{date}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn gen_playlist(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
params: web::Path<(i32, String)>,
2022-06-21 17:56:10 +02:00
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match generate_playlist(&pool.into_inner(), params.0, params.1.clone()).await {
2022-06-21 17:56:10 +02:00
Ok(playlist) => Ok(web::Json(playlist)),
Err(e) => Err(e),
}
}
/// **Delete Playlist**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X DELETE http://127.0.0.1:8787/api/playlist/1/2022-06-20
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[delete("/playlist/{id}/{date}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn del_playlist(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
params: web::Path<(i32, String)>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match delete_playlist(&pool.into_inner(), params.0, &params.1).await {
Ok(_) => Ok(format!("Delete playlist from {} success!", params.1)),
Err(e) => Err(e),
}
}
/// ### Log file
///
/// **Read Log Life**
///
/// ```BASH
2022-11-13 17:34:01 +01:00
/// curl -X GET http://127.0.0.1:8787/api/log/1
2022-11-08 15:42:17 +01:00
/// -H 'Content-Type: application/json' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[get("/log/{id}")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn get_log(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
log: web::Query<DateObj>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
read_log_file(&pool.into_inner(), &id, &log.date).await
}
/// ### File Operations
///
/// **Get File/Folder List**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/file/1/browse/ -H 'Content-Type: application/json'
2022-11-08 15:42:17 +01:00
/// -d '{ "source": "/" }' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[post("/file/{id}/browse/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn file_browser(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<PathObject>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match browser(&pool.into_inner(), *id, &data.into_inner()).await {
2022-06-20 22:19:41 +02:00
Ok(obj) => Ok(web::Json(obj)),
Err(e) => Err(e),
}
}
/// **Create Folder**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/file/1/create-folder/ -H 'Content-Type: application/json'
2022-11-08 15:42:17 +01:00
/// -d '{"source": "<FOLDER PATH>"}' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[post("/file/{id}/create-folder/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn add_dir(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<PathObject>,
) -> Result<HttpResponse, ServiceError> {
2022-11-18 14:13:24 +01:00
create_directory(&pool.into_inner(), *id, &data.into_inner()).await
}
/// **Rename File**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/file/1/rename/ -H 'Content-Type: application/json'
2022-11-08 15:42:17 +01:00
/// -d '{"source": "<SOURCE>", "target": "<TARGET>"}' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[post("/file/{id}/rename/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn move_rename(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<MoveObject>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match rename_file(&pool.into_inner(), *id, &data.into_inner()).await {
Ok(obj) => Ok(web::Json(obj)),
Err(e) => Err(e),
}
}
/// **Remove File/Folder**
///
/// ```BASH
2022-07-25 17:26:49 +02:00
/// curl -X POST http://127.0.0.1:8787/api/file/1/remove/ -H 'Content-Type: application/json'
2022-11-08 15:42:17 +01:00
/// -d '{"source": "<SOURCE>"}' -H 'Authorization: Bearer <TOKEN>'
/// ```
#[post("/file/{id}/remove/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
pub async fn remove(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
data: web::Json<PathObject>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
match remove_file_or_folder(&pool.into_inner(), *id, &data.into_inner().source).await {
Ok(obj) => Ok(web::Json(obj)),
Err(e) => Err(e),
}
}
/// **Upload File**
///
/// ```BASH
2022-11-13 17:34:01 +01:00
/// curl -X PUT http://127.0.0.1:8787/api/file/1/upload/ -H 'Authorization: Bearer <TOKEN>'
/// -F "file=@file.mp4"
/// ```
#[put("/file/{id}/upload/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn save_file(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
payload: Multipart,
obj: web::Query<FileObj>,
) -> Result<HttpResponse, ServiceError> {
2022-11-18 14:13:24 +01:00
upload(&pool.into_inner(), *id, payload, &obj.path, false).await
}
/// **Import playlist**
///
/// Import text/m3u file and convert it to a playlist
/// lines with leading "#" will be ignore
///
/// ```BASH
2022-11-13 17:34:01 +01:00
/// curl -X PUT http://127.0.0.1:8787/api/file/1/import/ -H 'Authorization: Bearer <TOKEN>'
/// -F "file=@list.m3u"
/// ```
#[put("/file/{id}/import/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn import_playlist(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-10-05 09:22:52 +02:00
id: web::Path<i32>,
payload: Multipart,
obj: web::Query<ImportObj>,
) -> Result<HttpResponse, ServiceError> {
let file = Path::new(&obj.file).file_name().unwrap_or_default();
let path = env::temp_dir().join(file).to_string_lossy().to_string();
2022-11-18 14:13:24 +01:00
let (config, _) = playout_config(&pool.clone().into_inner(), &id).await?;
let channel = handles::select_channel(&pool.clone().into_inner(), &id).await?;
2022-11-18 14:13:24 +01:00
upload(&pool.into_inner(), *id, payload, &path, true).await?;
import_file(&config, &obj.date, Some(channel.name), &path)?;
fs::remove_file(path)?;
Ok(HttpResponse::Ok().into())
}
2022-11-13 17:34:01 +01:00
2022-11-13 21:21:30 +01:00
/// **Program info**
2022-11-13 17:34:01 +01:00
///
2022-11-13 21:21:14 +01:00
/// Get program infos about given date, or current day
2022-11-13 17:34:01 +01:00
///
/// Examples:
///
/// * get program from current day
/// ```BASH
2022-11-16 15:57:32 +01:00
/// curl -X GET http://127.0.0.1:8787/api/program/1/ -H 'Authorization: Bearer <TOKEN>'
/// ```
///
/// * get a program range between two dates
2022-11-13 17:34:01 +01:00
/// ```BASH
2022-11-16 15:57:32 +01:00
/// curl -X GET http://127.0.0.1:8787/api/program/1/?start_after=2022-11-13T12:00:00&start_before=2022-11-20T11:59:59 \
2022-11-13 17:34:01 +01:00
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
///
/// * get program from give day
/// ```BASH
2022-11-16 15:57:32 +01:00
/// curl -X GET http://127.0.0.1:8787/api/program/1/?start_after=2022-11-13T10:00:00 \
/// -H 'Authorization: Bearer <TOKEN>'
/// ```
2022-11-13 17:34:01 +01:00
#[get("/program/{id}/")]
#[has_any_role("Role::Admin", "Role::User", type = "Role")]
async fn get_program(
2022-11-19 20:54:46 +01:00
pool: web::Data<Pool<Sqlite>>,
2022-11-13 17:34:01 +01:00
id: web::Path<i32>,
obj: web::Query<ProgramObj>,
) -> Result<impl Responder, ServiceError> {
2022-11-18 14:13:24 +01:00
let (config, _) = playout_config(&pool.clone().into_inner(), &id).await?;
2022-11-13 17:34:01 +01:00
let start_sec = config.playlist.start_sec.unwrap();
let mut days = 0;
let mut program = vec![];
2022-11-13 21:18:48 +01:00
let after = obj.start_after;
let mut before = obj.start_before;
if after > before {
before = chrono::Local
2022-11-19 20:34:29 +01:00
.with_ymd_and_hms(after.year(), after.month(), after.day(), 23, 59, 59)
.unwrap()
.naive_local()
}
2022-11-13 17:34:01 +01:00
if start_sec > time_to_sec(&after.format("%H:%M:%S").to_string()) {
days = 1;
}
let date_range = get_date_range(&vec_strings![
(after - Duration::days(days)).format("%Y-%m-%d"),
"-",
before.format("%Y-%m-%d")
]);
for date in date_range {
2022-11-18 14:13:24 +01:00
let conn = pool.clone().into_inner();
2022-11-13 17:34:01 +01:00
let mut naive = NaiveDateTime::parse_from_str(
&format!("{date} {}", sec_to_time(start_sec)),
"%Y-%m-%d %H:%M:%S%.3f",
)
.unwrap();
2022-11-18 14:13:24 +01:00
let playlist = match read_playlist(&conn, *id, date.clone()).await {
2022-11-13 17:34:01 +01:00
Ok(p) => p,
Err(e) => {
2022-11-13 21:18:48 +01:00
error!("Error in Playlist from {date}: {e}");
2022-11-13 17:34:01 +01:00
continue;
}
};
for item in playlist.program {
let start: DateTime<Local> = Local.from_local_datetime(&naive).unwrap();
let source = match Regex::new(&config.text.regex)
.ok()
.and_then(|r| r.captures(&item.source))
{
Some(t) => t[1].to_string(),
None => item.source,
};
2022-11-13 21:18:48 +01:00
let p_item = ProgramItem {
2022-11-13 17:34:01 +01:00
source,
start: start.format("%Y-%m-%d %H:%M:%S%.3f%:z").to_string(),
r#in: item.seek,
out: item.out,
duration: item.duration,
category: item.category,
};
if naive >= after && naive <= before {
program.push(p_item);
}
naive += Duration::milliseconds(((item.out - item.seek) * 1000.0) as i64);
}
}
Ok(web::Json(program))
}