111 lines
3.2 KiB
Rust
Raw Normal View History

use std::{fs, path::PathBuf};
use simplelog::*;
2022-06-19 22:55:10 +02:00
2022-06-21 23:10:38 +02:00
use crate::utils::{errors::ServiceError, playout_config};
use ffplayout_lib::utils::{
generate_playlist as playlist_generator, json_reader, json_writer, JsonPlaylist,
};
2022-10-05 09:22:52 +02:00
pub async fn read_playlist(id: i32, date: String) -> Result<JsonPlaylist, ServiceError> {
2022-06-21 17:56:10 +02:00
let (config, _) = playout_config(&id).await?;
2022-06-19 22:55:10 +02:00
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");
match json_reader(&playlist_path) {
Ok(p) => Ok(p),
Err(e) => Err(ServiceError::NoContent(e.to_string())),
}
2022-06-19 22:55:10 +02:00
}
2022-10-05 09:22:52 +02:00
pub async fn write_playlist(id: i32, json_data: JsonPlaylist) -> Result<String, ServiceError> {
2022-06-21 17:56:10 +02:00
let (config, _) = playout_config(&id).await?;
let date = json_data.date.clone();
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");
let mut file_exists = false;
if let Some(p) = playlist_path.parent() {
fs::create_dir_all(p)?;
}
if playlist_path.is_file() {
file_exists = true;
if let Ok(existing_data) = json_reader(&playlist_path) {
if json_data == existing_data {
return Err(ServiceError::Conflict(format!(
"Playlist from {date}, already exists!"
)));
}
}
}
match json_writer(&playlist_path, json_data) {
Ok(_) => {
let mut msg = format!("Write playlist from {date} success!");
if file_exists {
msg = format!("Update playlist from {date} success!");
}
return Ok(msg);
}
Err(e) => {
error!("{e}");
}
}
Err(ServiceError::InternalServerError)
}
2022-10-05 09:22:52 +02:00
pub async fn generate_playlist(id: i32, date: String) -> Result<JsonPlaylist, ServiceError> {
let (mut config, channel) = playout_config(&id).await?;
config.general.generate = Some(vec![date.clone()]);
2022-06-21 17:56:10 +02:00
match playlist_generator(&config, Some(channel.name)) {
2022-06-21 17:56:10 +02:00
Ok(playlists) => {
if !playlists.is_empty() {
Ok(playlists[0].clone())
} else {
Err(ServiceError::Conflict(
"Playlist could not be written, possible already exists!".into(),
))
}
}
Err(e) => {
error!("{e}");
Err(ServiceError::InternalServerError)
}
}
}
2022-10-05 09:22:52 +02:00
pub async fn delete_playlist(id: i32, date: &str) -> Result<(), ServiceError> {
2022-06-21 17:56:10 +02:00
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)
.with_extension("json");
if playlist_path.is_file() {
if let Err(e) = fs::remove_file(playlist_path) {
error!("{e}");
return Err(ServiceError::InternalServerError);
};
}
Ok(())
}