From 5761deb2986af2aacfcacec874f31c708ccdf4c4 Mon Sep 17 00:00:00 2001 From: Valmo Date: Tue, 30 Jun 2026 02:16:23 -0300 Subject: [PATCH] ultima chance --- src/config.rs | 90 +++++++++++++++++++++++++++++++++++++++-------- src/redis_rtsp.rs | 24 ++++++++++--- 2 files changed, 96 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5fb7c67..af113dc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -367,25 +367,54 @@ fn load_csv_targets(file: Option<&Path>, inline: Option<&str>) -> Result { + let targets = parse_csv_targets(&content); + if !targets.is_empty() { + return Ok(targets); + } + return Err(anyhow!("arquivo CSV vazio: {}", file.display())); + } + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + let targets = parse_csv_targets(include_str!("../camfinder-open-rtsp.csv")); + if !targets.is_empty() { + return Ok(targets); + } + return Err(anyhow!( + "arquivo CSV padrão embutido vazio: ../camfinder-open-rtsp.csv" + )); + } + Err(err) => { + return Err(anyhow!(err)) + .with_context(|| format!("failed to read CSV file {}", file.display())); + } } - return Err(anyhow!("arquivo CSV vazio: {}", file.display())); } if let Ok(path) = env::var("RTSP_TARGETS_FILE") { let path = path.trim(); if !path.is_empty() { - let content = fs::read_to_string(path) - .with_context(|| format!("failed to read CSV file {path}"))?; - let targets = parse_csv_targets(&content); - if !targets.is_empty() { - return Ok(targets); + match fs::read_to_string(path) { + Ok(content) => { + let targets = parse_csv_targets(&content); + if !targets.is_empty() { + return Ok(targets); + } + return Err(anyhow!("arquivo CSV vazio: {path}")); + } + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + let targets = parse_csv_targets(include_str!("../camfinder-open-rtsp.csv")); + if !targets.is_empty() { + return Ok(targets); + } + return Err(anyhow!( + "arquivo CSV padrão embutido vazio: ../camfinder-open-rtsp.csv" + )); + } + Err(err) => { + return Err(err).with_context(|| format!("failed to read CSV file {path}")); + } } - return Err(anyhow!("arquivo CSV vazio: {path}")); } } @@ -449,7 +478,20 @@ fn load_rtsp_paths(file: Option<&Path>, inline: Option<&str>) -> Result Ok(paths), + Err(err) if is_not_found(&err) => { + let paths = parse_rtsp_paths(include_str!("../rtsp_paths.txt")); + if !paths.is_empty() { + Ok(paths) + } else { + Err(anyhow!( + "arquivo de paths RTSP padrão embutido vazio: ../rtsp_paths.txt" + )) + } + } + Err(err) => Err(err), + }; } } @@ -463,7 +505,20 @@ fn load_rtsp_paths(file: Option<&Path>, inline: Option<&str>) -> Result Ok(paths), + Err(err) if is_not_found(&err) => { + let paths = parse_rtsp_paths(include_str!("../rtsp_paths.txt")); + if !paths.is_empty() { + Ok(paths) + } else { + Err(anyhow!( + "arquivo de paths RTSP padrão embutido vazio: ../rtsp_paths.txt" + )) + } + } + Err(err) => Err(err), + } } fn load_rtsp_paths_from_file(file: &Path) -> Result> { @@ -476,6 +531,13 @@ fn load_rtsp_paths_from_file(file: &Path) -> Result> { Err(anyhow!("arquivo de paths RTSP vazio: {}", file.display())) } +fn is_not_found(err: &anyhow::Error) -> bool { + err.chain() + .find_map(|cause| cause.downcast_ref::()) + .map(|err| err.kind() == std::io::ErrorKind::NotFound) + .unwrap_or(false) +} + fn parse_rtsp_paths(input: &str) -> Vec { let mut seen = std::collections::HashSet::new(); input diff --git a/src/redis_rtsp.rs b/src/redis_rtsp.rs index 7fb1103..143d6b0 100644 --- a/src/redis_rtsp.rs +++ b/src/redis_rtsp.rs @@ -548,8 +548,15 @@ async fn sqlite_exec(sqlite_path: &Path, sql: &str) -> Result<()> { } fn load_ip_list(input: &str) -> Result> { - let content = - fs::read_to_string(input).with_context(|| format!("failed to read input CSV {input}"))?; + let content = match fs::read_to_string(input) { + Ok(content) => content, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + include_str!("../camfinder-open-rtsp.csv").to_string() + } + Err(err) => { + return Err(anyhow!(err)).with_context(|| format!("failed to read input CSV {input}")); + } + }; let mut ips = Vec::new(); for line in content.lines() { @@ -682,8 +689,17 @@ impl RtspCredential { } fn load_rtsp_credentials(path: &Path) -> Result> { - let content = fs::read_to_string(path) - .with_context(|| format!("failed to read RTSP credential file {}", path.display()))?; + let content = match fs::read_to_string(path) { + Ok(content) => content, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + include_str!("../main_ids.json").to_string() + } + Err(err) => { + return Err(anyhow!(err)).with_context(|| { + format!("failed to read RTSP credential file {}", path.display()) + }); + } + }; let usernames = parse_json_string_array(&content, "usernames")?; let passwords = parse_json_string_array(&content, "passwords")?; Ok(build_rtsp_credentials(&usernames, &passwords))