fix update playlist

This commit is contained in:
jb-alvarado 2024-04-15 21:11:17 +02:00
parent e68516d219
commit b1881807a8
3 changed files with 16 additions and 8 deletions

View File

@ -8,7 +8,7 @@
///
/// 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::PathBuf};
use std::{collections::HashMap, env, path::PathBuf};
use actix_files;
use actix_multipart::Multipart;
@ -32,7 +32,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize};
use simplelog::*;
use sqlx::{Pool, Sqlite};
use tokio::task;
use tokio::{fs, task};
use crate::db::{
handles,
@ -1084,6 +1084,7 @@ async fn import_playlist(
) -> Result<HttpResponse, ServiceError> {
let file = obj.file.file_name().unwrap_or_default();
let path = env::temp_dir().join(file);
let path_clone = path.clone();
let (config, _) = playout_config(&pool.clone().into_inner(), &id).await?;
let channel = handles::select_channel(&pool.clone().into_inner(), &id).await?;
let size: u64 = req
@ -1094,9 +1095,11 @@ async fn import_playlist(
.unwrap_or(0);
upload(&pool.into_inner(), *id, size, payload, &path, true).await?;
import_file(&config, &obj.date, Some(channel.name), &path)?;
fs::remove_file(path)?;
task::spawn_blocking(move || import_file(&config, &obj.date, Some(channel.name), &path_clone))
.await??;
fs::remove_file(path).await?;
Ok(HttpResponse::Ok().into())
}

View File

@ -81,3 +81,9 @@ impl From<sqlx::Error> for ServiceError {
ServiceError::BadRequest(err.to_string())
}
}
impl From<tokio::task::JoinError> for ServiceError {
fn from(err: tokio::task::JoinError) -> ServiceError {
ServiceError::BadRequest(err.to_string())
}
}

View File

@ -61,11 +61,10 @@ pub fn import_file(
if playlist_file.is_file() {
file_exists = true;
let existing_data = json_reader(playlist_file)?;
let mut existing_data = json_reader(playlist_file)?;
existing_data.program.append(&mut playlist.program);
if playlist == existing_data {
return Ok(format!("Playlist from {date}, already exists!"));
}
playlist.program = existing_data.program;
};
let mut msg = format!("Write playlist from {date} success!");