From 7ee4a2fa5d81cdf1a80cf45594272ac5c4111a08 Mon Sep 17 00:00:00 2001 From: euzu Date: Tue, 26 Mar 2024 12:34:57 +0100 Subject: [PATCH] code reactoring --- src/api/v1_api.rs | 19 +++++++++---------- src/filter.pest | 8 ++++---- src/filter.rs | 20 +++++++++++++++++++- src/main.rs | 15 ++++++--------- src/processing/playlist_processor.rs | 7 ++----- src/repository/xtream_repository.rs | 8 ++++---- src/test.rs | 20 +++++++++++++++++++- src/{ => utils}/config_reader.rs | 5 ++--- src/utils/mod.rs | 4 +++- src/{ => utils}/multi_file_reader.rs | 0 10 files changed, 68 insertions(+), 38 deletions(-) rename src/{ => utils}/config_reader.rs (97%) rename src/{ => utils}/multi_file_reader.rs (100%) diff --git a/src/api/v1_api.rs b/src/api/v1_api.rs index 120774750..1c22fafe4 100644 --- a/src/api/v1_api.rs +++ b/src/api/v1_api.rs @@ -4,15 +4,14 @@ use serde_json::{json}; use crate::api::api_model::{AppState, PlaylistRequest, ServerConfig, ServerInputConfig, ServerSourceConfig, ServerTargetConfig}; use crate::model::config::{Config, ConfigDto, ConfigInput, ConfigInputOptions, ConfigSource, ConfigTarget, InputType, validate_targets}; use log::{error}; -use crate::api::download_api::{download_file_info, queue_download_file}; -use crate::config_reader::{read_config, save_api_proxy, save_main_config}; +use crate::api::download_api; use crate::m3u_filter_error::M3uFilterError; use crate::model::api_proxy::{ApiProxyConfig, ApiProxyServerInfo, TargetUser}; -use crate::processing::playlist_processor::exec_processing; -use crate::utils::download; +use crate::processing::playlist_processor; +use crate::utils::{config_reader, download}; fn _save_config_api_proxy(backup_dir: &str, api_proxy: &mut ApiProxyConfig) -> Option { - match save_api_proxy(api_proxy._file_path.as_str(), backup_dir, api_proxy) { + match config_reader::save_api_proxy(api_proxy._file_path.as_str(), backup_dir, api_proxy) { Ok(_) => {} Err(err) => { error!("Failed to save api_proxy.yml {}", err.to_string()); @@ -23,7 +22,7 @@ fn _save_config_api_proxy(backup_dir: &str, api_proxy: &mut ApiProxyConfig) -> O } fn _save_config_main(file_path: &str, backup_dir: &str, cfg: &ConfigDto) -> Option { - match save_main_config(file_path, backup_dir, cfg) { + match config_reader::save_main_config(file_path, backup_dir, cfg) { Ok(_) => {} Err(err) => { error!("Failed to save config.yml {}", err.to_string()); @@ -95,7 +94,7 @@ pub(crate) async fn playlist_update( let process_targets = validate_targets(&user_targets, &_app_state.config.sources); match process_targets { Ok(valid_targets) => { - actix_rt::spawn(exec_processing(Arc::clone(&_app_state.config), Arc::new(valid_targets))); + actix_rt::spawn(playlist_processor::exec_processing(Arc::clone(&_app_state.config), Arc::new(valid_targets))); HttpResponse::Ok().finish() } Err(err) => { @@ -200,7 +199,7 @@ pub(crate) async fn config( api_proxy: config._api_proxy.read().unwrap().clone(), }; - let mut result = match read_config(_app_state.config._config_path.as_str(), + let mut result = match config_reader::read_config(_app_state.config._config_path.as_str(), _app_state.config._config_file_path.as_str(), _app_state.config._sources_file_path.as_str()) { Ok(mut cfg) => { @@ -226,6 +225,6 @@ pub(crate) fn v1_api_register() -> Scope { .route("/config/apiproxy", web::post().to(save_config_api_proxy_config)) .route("/playlist", web::post().to(playlist)) .route("/playlist/update", web::post().to(playlist_update)) - .route("/file/download", web::post().to(queue_download_file)) - .route("/file/download/info", web::get().to(download_file_info)) + .route("/file/download", web::post().to(download_api::queue_download_file)) + .route("/file/download/info", web::get().to(download_api::download_file_info)) } diff --git a/src/filter.pest b/src/filter.pest index 704dc0854..9936f8587 100644 --- a/src/filter.pest +++ b/src/filter.pest @@ -1,8 +1,8 @@ WHITESPACE = _{ " " | "\t" } -field = { "Group" | "Title" | "Name" | "Url" } -and = {"AND" | "and"} -or = {"OR" | "or"} -not = { "NOT" | "not" } +field = { ^"group" | ^"title" | ^"name" | ^"url" } +and = { ^"and" } +or = { ^"or" } +not = { ^"not" } regexp = @{ "\"" ~ ( "\\\"" | (!"\"" ~ ANY) )* ~ "\"" } comparison_value = _{ regexp } comparison = { field ~ "~" ~ comparison_value } diff --git a/src/filter.rs b/src/filter.rs index aa00576c1..7446eb074 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -73,7 +73,25 @@ pub(crate) struct RegexWithCaptures { #[derive(Parser)] //#[grammar = "filter.pest"] -#[grammar_inline = "WHITESPACE = _{ \" \" | \"\\t\" }\nfield = { \"Group\" | \"Title\" | \"Name\" | \"Url\" }\nand = {\"AND\" | \"and\"}\nor = {\"OR\" | \"or\"}\nnot = { \"NOT\" | \"not\" }\nregexp = @{ \"\\\"\" ~ ( \"\\\\\\\"\" | (!\"\\\"\" ~ ANY) )* ~ \"\\\"\" }\ncomparison_value = _{ regexp }\ncomparison = { field ~ \"~\" ~ comparison_value }\nbool_op = { and | or}\nexpr_group = { \"(\" ~ expr ~ \")\" }\nexpr = {comparison ~ (bool_op ~ expr)* | expr_group ~ (bool_op ~ expr)* | not ~ expr ~ (bool_op ~ expr)* }\nstmt = { expr ~ (bool_op ~ expr)* }\nmain = _{ SOI ~ stmt ~ EOI }"] +#[grammar_inline = r#" +WHITESPACE = _{ " " | "\t" } +field = { ^"group" | ^"title" | ^"name" | ^"url" } +and = { ^"and" } +or = { ^"or" } +not = { ^"not" } +regexp = @{ "\"" ~ ( "\\\"" | (!"\"" ~ ANY) )* ~ "\"" } +comparison_value = _{ regexp } +comparison = { field ~ "~" ~ comparison_value } +bool_op = { and | or} +expr_group = { "(" ~ expr ~ ")" } +expr = { + comparison ~ (bool_op ~ expr)* + | expr_group ~ (bool_op ~ expr)* + | not ~ expr ~ (bool_op ~ expr)* +} +stmt = { expr ~ (bool_op ~ expr)* } +main = _{ SOI ~ stmt ~ EOI } +"#] struct FilterParser; #[derive(Debug, Clone)] diff --git a/src/main.rs b/src/main.rs index 4c363690d..1454dcaf4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,13 +10,11 @@ use clap::Parser; use env_logger::Builder; use log::{error, info, LevelFilter}; -use crate::config_reader::{read_api_proxy_config, read_config, read_mappings}; use crate::model::config::{Config, ProcessTargets, validate_targets}; -use crate::processing::playlist_processor::exec_processing; -use crate::utils::file_utils; +use crate::processing::playlist_processor; +use crate::utils::{config_reader, file_utils}; mod m3u_filter_error; -mod config_reader; mod model; mod filter; mod repository; @@ -24,7 +22,6 @@ mod messaging; mod test; mod api; mod processing; -mod multi_file_reader; mod utils; #[derive(Parser)] @@ -79,7 +76,7 @@ fn main() { let sources_file: String = args.source_file.unwrap_or(file_utils::get_default_sources_file_path(&config_path)); - let mut cfg = read_config(config_path.as_str(), config_file.as_str(), sources_file.as_str()).unwrap_or_else(|err| exit!("{}", err)); + let mut cfg = config_reader::read_config(config_path.as_str(), config_file.as_str(), sources_file.as_str()).unwrap_or_else(|err| exit!("{}", err)); if args.log_level.is_none() { if let Some(log_level) = &cfg.log_level { @@ -97,12 +94,12 @@ fn main() { info!("Config file: {}", &config_file); info!("Source file: {}", &sources_file); - if let Err(err) = read_mappings(args.mapping_file, &mut cfg) { + if let Err(err) = config_reader::read_mappings(args.mapping_file, &mut cfg) { exit!("{}", err); } if args.server { - read_api_proxy_config(args.api_proxy, &mut cfg); + config_reader::read_api_proxy_config(args.api_proxy, &mut cfg); start_in_server_mode(Arc::new(cfg), Arc::new(targets)); } else { start_in_cli_mode(Arc::new(cfg), Arc::new(targets)) @@ -110,7 +107,7 @@ fn main() { } fn start_in_cli_mode(cfg: Arc, targets: Arc) { - System::new().block_on(async { exec_processing(cfg, targets).await }); + System::new().block_on(async { playlist_processor::exec_processing(cfg, targets).await }); } fn start_in_server_mode(cfg: Arc, targets: Arc) { diff --git a/src/processing/playlist_processor.rs b/src/processing/playlist_processor.rs index 50d49475d..a0d2b5fc3 100644 --- a/src/processing/playlist_processor.rs +++ b/src/processing/playlist_processor.rs @@ -187,11 +187,8 @@ fn rename_playlist(playlist: &mut [PlaylistGroup], target: &ConfigTarget) -> Opt macro_rules! apply_pattern { ($pattern:expr, $provider:expr, $processor:expr) => {{ - match $pattern { - Some(ptrn) => { - ptrn.filter($provider, $processor); - }, - _ => {} + if let Some(ptrn) = $pattern { + ptrn.filter($provider, $processor); }; }}; } diff --git a/src/repository/xtream_repository.rs b/src/repository/xtream_repository.rs index c79c06ceb..ce4b44637 100644 --- a/src/repository/xtream_repository.rs +++ b/src/repository/xtream_repository.rs @@ -334,12 +334,12 @@ fn write_index(path: &PathBuf, index: &IndexTree) -> std::io::Result<()> { fn seek_read( reader: &mut (impl Read + Seek), - offset: u32, + offset: u64, amount_to_read: u16, ) -> Result, Error> { // A buffer filled with as many zeros as we'll read with read_exact - let mut buf = vec![0; amount_to_read as usize]; - reader.seek(SeekFrom::Start(offset as u64))?; + let mut buf = vec![0u8; amount_to_read as usize]; + reader.seek(SeekFrom::Start(offset))?; reader.read_exact(&mut buf)?; Ok(buf) } @@ -360,7 +360,7 @@ pub(crate) async fn xtream_get_stored_stream_info( if let Some(idx_map) = &index_tree { if let Some((offset, size)) = idx_map.get(&stream_id) { let mut reader = BufReader::new(File::open(&col_path).unwrap()); - if let Ok(bytes) = seek_read(&mut reader, *offset, *size) { + if let Ok(bytes) = seek_read(&mut reader, *offset as u64, *size) { let mut decomp: Vec = Vec::new(); let _ = lzma_rs::lzma_decompress(&mut bytes.as_slice(), &mut decomp); drop(shared_lock); diff --git a/src/test.rs b/src/test.rs index d805020f8..ad0ccbfd2 100644 --- a/src/test.rs +++ b/src/test.rs @@ -9,7 +9,25 @@ mod tests { Ok(filter) => { assert_eq!(format!("{}", filter), flt1); }, - Err(e) => {} + Err(_e) => {} } } + + + // #[test] + // fn test_xtream_id_mapping() { + // let mappings = vec![ + // MultiXtreamMapping { stream_id: 2, input_id: 3 }, + // MultiXtreamMapping { stream_id: 4, input_id: 5 }, + // MultiXtreamMapping { stream_id: 8, input_id: 6 }, + // ]; + // + // write_xtream_mapping(&mappings).unwrap(); + // for i in 1..=mappings.len() { + // let mapping = read_xtream_mapping(i as u32).unwrap().unwrap(); + // let test_mapping = mappings.get(i-1).unwrap(); + // assert_eq!(mapping.stream_id, test_mapping.stream_id); + // assert_eq!(mapping.input_id, test_mapping.input_id); + // } + // } } \ No newline at end of file diff --git a/src/config_reader.rs b/src/utils/config_reader.rs similarity index 97% rename from src/config_reader.rs rename to src/utils/config_reader.rs index 89628ddcc..a3afa45de 100644 --- a/src/config_reader.rs +++ b/src/utils/config_reader.rs @@ -8,8 +8,7 @@ use crate::model::config::{Config, ConfigDto}; use crate::model::mapping::Mappings; use crate::{create_m3u_filter_error_result, handle_m3u_filter_error_result}; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; -use crate::multi_file_reader::MultiFileReader; -use crate::utils::file_utils; +use crate::utils::{file_utils, multi_file_reader}; pub(crate) fn read_mappings(args_mapping: Option, cfg: &mut Config) -> Result<(), M3uFilterError> { let mappings_file: String = args_mapping.unwrap_or(file_utils::get_default_mappings_path(cfg._config_path.as_str())); @@ -42,7 +41,7 @@ pub(crate) fn read_api_proxy_config(args_api_proxy_config: Option, cfg: pub(crate) fn read_config(config_path: &str, config_file: &str, sources_file: &str) -> Result { let files = vec![std::path::PathBuf::from(config_file), std::path::PathBuf::from(sources_file)]; - match MultiFileReader::new(&files) { + match multi_file_reader::MultiFileReader::new(&files) { Ok(file) => { match serde_yaml::from_reader::<_, Config>(file) { Ok(mut result) => { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 872668fd4..53fe4fb75 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,4 +2,6 @@ pub (crate) mod file_utils; pub (crate) mod request_utils; pub (crate) mod download; pub (crate) mod string_utils; -pub (crate) mod json_utils; \ No newline at end of file +pub (crate) mod json_utils; +pub (crate) mod config_reader; +pub (crate) mod multi_file_reader; \ No newline at end of file diff --git a/src/multi_file_reader.rs b/src/utils/multi_file_reader.rs similarity index 100% rename from src/multi_file_reader.rs rename to src/utils/multi_file_reader.rs