diff --git a/src/input/playlist.rs b/src/input/playlist.rs index e7ba909c..3393a28a 100644 --- a/src/input/playlist.rs +++ b/src/input/playlist.rs @@ -80,42 +80,9 @@ impl CurrentProgram { } else if Path::new(&self.json_path.clone().unwrap()).is_file() || is_remote(&self.json_path.clone().unwrap()) { - let mut is_playlist_changed = false; + let mod_time = modified_time(&self.json_path.clone().unwrap()); - if is_remote(&self.json_path.clone().unwrap()) { - let resp = reqwest::blocking::Client::new() - .head(self.json_path.clone().unwrap()) - .send(); - match resp { - Ok(resp) => { - if resp.status().is_success() { - match resp.headers().get(reqwest::header::LAST_MODIFIED) { - Some(last_modified) => { - if !last_modified - .to_str() - .unwrap() - .eq(&self.json_mod.clone().unwrap()) - { - is_playlist_changed = true - } - } - None => {} - } - } - } - Err(_) => self.on_check_update_error(), - }; - } else { - let mod_time = modified_time(&self.json_path.clone().unwrap()); - - if let Some(m) = mod_time { - if !m.to_string().eq(&self.json_mod.clone().unwrap()) { - is_playlist_changed = true; - } - } - } - - if is_playlist_changed { + if self.json_mod != mod_time { // when playlist has changed, reload it info!( "Reload playlist {}", @@ -137,27 +104,23 @@ impl CurrentProgram { self.index.fetch_add(1, Ordering::SeqCst); } } else { - self.on_check_update_error(); + error!( + "Playlist {} not exists!", + self.json_path.clone().unwrap() + ); + let mut media = Media::new(0, String::new(), false); + media.begin = Some(get_sec()); + media.duration = DUMMY_LEN; + media.out = DUMMY_LEN; + + self.json_path = None; + *self.nodes.lock().unwrap() = vec![media.clone()]; + self.current_node = media; + self.playout_stat.list_init.store(true, Ordering::SeqCst); + self.index.store(0, Ordering::SeqCst); } } - fn on_check_update_error(&mut self) { - error!( - "Playlist {} not exists!", - self.json_path.clone().unwrap() - ); - let mut media = Media::new(0, String::new(), false); - media.begin = Some(get_sec()); - media.duration = DUMMY_LEN; - media.out = DUMMY_LEN; - - self.json_path = None; - *self.nodes.lock().unwrap() = vec![media.clone()]; - self.current_node = media; - self.playout_stat.list_init.store(true, Ordering::SeqCst); - self.index.store(0, Ordering::SeqCst); - } - // Check if day is past and it is time for a new playlist. fn check_for_next_playlist(&mut self) { let current_time = get_sec(); diff --git a/src/utils/json_serializer.rs b/src/utils/json_serializer.rs index 369d82b1..fbc2931e 100644 --- a/src/utils/json_serializer.rs +++ b/src/utils/json_serializer.rs @@ -8,7 +8,9 @@ use std::{ use simplelog::*; -use crate::utils::{get_date, is_remote, modified_time, validate_playlist, GlobalConfig, Media}; +use crate::utils::{ + get_date, is_remote, modified_time, time_from_header, validate_playlist, GlobalConfig, Media, +}; pub const DUMMY_LEN: f64 = 60.0; @@ -39,7 +41,7 @@ impl Playlist { date, start_sec: Some(start), current_file: None, - modified: Some(String::new()), + modified: None, program: vec![media], } } @@ -59,7 +61,7 @@ pub fn read_json( let mut start_sec = config.playlist.start_sec.unwrap(); let date = get_date(seek, start_sec, next_start); - if playlist_path.is_dir() { + if playlist_path.is_dir() || is_remote(&config.playlist.path) { let d: Vec<&str> = date.split('-').collect(); playlist_path = playlist_path .join(d[0]) @@ -75,48 +77,36 @@ pub fn read_json( current_file = p } - let mut playlist: Playlist; + let mut playlist = Playlist::new(date, start_sec); if is_remote(¤t_file) { - let resp = reqwest::blocking::Client::new().get(¤t_file).send(); + let response = reqwest::blocking::Client::new().get(¤t_file).send(); - match resp { - Ok(resp) => { - if resp.status().is_success() { - info!("Read Remote Playlist: {current_file}"); + if let Ok(resp) = response { + if resp.status().is_success() { + info!("Read Remote Playlist: {current_file}"); - let headers = resp.headers().clone(); - let body = resp.text().unwrap(); + let headers = resp.headers().clone(); + if let Ok(body) = resp.text() { playlist = - serde_json::from_str(&body).expect("Could not read json playlist str."); + serde_json::from_str(&body).expect("Could't read remote json playlist."); - match headers.get(reqwest::header::LAST_MODIFIED) { - Some(t) => { - playlist.modified = Some(t.to_str().unwrap().to_string()); - } - None => {} + if let Some(time) = time_from_header(&headers) { + playlist.modified = Some(time.to_string()); } - } else { - error!( - "Get Remote Playlist {current_file} not success!: {}", - resp.text().unwrap() - ); - - return Playlist::new(date, start_sec); } } - Err(e) => { - error!("Remote Playlist {current_file}: {}", e); + } else { + error!("Can't read remote playlist {current_file}"); - return Playlist::new(date, start_sec); - } - }; + return playlist; + } } else { if !playlist_path.is_file() { error!("Playlist {current_file} not exists!"); - return Playlist::new(date, start_sec); + return playlist; } info!("Read Playlist: {current_file}"); @@ -126,13 +116,8 @@ pub fn read_json( .write(false) .open(¤t_file) .expect("Could not open json playlist file."); - playlist = serde_json::from_reader(f).expect("Could not read json playlist file."); - - let modify = modified_time(¤t_file); - - if let Some(modi) = modify { - playlist.modified = Some(modi.to_string()); - } + playlist = serde_json::from_reader(f).expect("Could't read json playlist file."); + playlist.modified = modified_time(¤t_file); } playlist.current_file = Some(current_file); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c7a2b8a1..5e01c6f7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,7 +8,9 @@ use std::{ use chrono::{prelude::*, Duration}; use ffprobe::{ffprobe, Format, Stream}; +use jsonrpc_http_server::hyper::HeaderMap; use regex::Regex; +use reqwest::header; use serde::{Deserialize, Serialize}; use serde_json::json; use simplelog::*; @@ -233,11 +235,37 @@ pub fn get_date(seek: bool, start: f64, next_start: f64) -> String { local.format("%Y-%m-%d").to_string() } +pub fn time_from_header(headers: &HeaderMap) -> Option> { + if let Some(time) = headers.get(header::LAST_MODIFIED) { + if let Ok(t) = time.to_str() { + let time = DateTime::parse_from_rfc2822(t); + let date_time: DateTime = time.unwrap().into(); + return Some(date_time); + }; + } + + None +} + /// Get file modification time. -pub fn modified_time(path: &str) -> Option> { +pub fn modified_time(path: &str) -> Option { + if is_remote(path) { + let response = reqwest::blocking::Client::new().head(path).send(); + + if let Ok(resp) = response { + if resp.status().is_success() { + if let Some(time) = time_from_header(resp.headers()) { + return Some(time.to_string()); + } + } + } + + return None; + } + if let Ok(time) = metadata(path).and_then(|metadata| metadata.modified()) { let date_time: DateTime = time.into(); - return Some(date_time); + return Some(date_time.to_string()); } None