117 lines
3.4 KiB
Rust
Raw Normal View History

use std::{
fs::{self, File},
io::Error,
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, JsonPlaylist};
2022-06-19 22:55:10 +02:00
fn json_reader(path: &PathBuf) -> Result<JsonPlaylist, Error> {
let f = File::options().read(true).write(false).open(&path)?;
let p = serde_json::from_reader(f)?;
Ok(p)
}
fn json_writer(path: &PathBuf, data: JsonPlaylist) -> Result<(), Error> {
let f = File::options()
.write(true)
.truncate(true)
.create(true)
.open(&path)?;
serde_json::to_writer_pretty(f, &data)?;
Ok(())
}
2022-06-19 22:55:10 +02:00
pub async fn read_playlist(id: i64, 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
}
pub async fn write_playlist(id: i64, 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");
if playlist_path.is_file() {
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(_) => return Ok(format!("Write playlist from {date} success!")),
Err(e) => {
error!("{e}");
}
}
Err(ServiceError::InternalServerError)
}
2022-06-21 17:56:10 +02:00
pub async fn generate_playlist(id: i64, 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)
}
}
}
pub async fn delete_playlist(id: i64, 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(())
}