libsubconverter/utils/
file.rs

1use crate::settings::Settings;
2use crate::utils::http::{parse_proxy, web_get_async};
3
4// Import platform-specific implementations
5#[cfg(not(target_arch = "wasm32"))]
6mod platform {
7    pub use crate::utils::file_std::{copy_file, file_exists, file_get_async, read_file_async};
8}
9
10#[cfg(target_arch = "wasm32")]
11mod platform {
12    pub use crate::utils::file_wasm::{copy_file, file_exists, file_get_async, read_file_async};
13}
14
15// Re-export platform-specific implementations
16pub use platform::*;
17
18// These functions are re-exported from platform-specific implementations
19
20/// Async version of load_content
21///
22/// # Arguments
23/// * `path` - Path to the file or URL to load
24///
25/// # Returns
26/// * `Ok(String)` - The content
27/// * `Err(String)` - Error message if loading failed
28pub async fn load_content_async(path: &str) -> Result<String, String> {
29    if path.starts_with("http://") || path.starts_with("https://") {
30        // It's a URL, use HTTP client
31        match web_get_async(path, &parse_proxy(&Settings::current().proxy_config), None).await {
32            Ok(response) => Ok(response.body),
33            Err(e) => Err(format!("Failed to read file from URL: {}", e)),
34        }
35    } else if file_exists(path).await {
36        // It's a file, read it asynchronously
37        match read_file_async(path).await {
38            Ok(data) => Ok(data),
39            Err(e) => Err(format!("Failed to read file: {}", e)),
40        }
41    } else {
42        Err(format!("Path not found: {}", path))
43    }
44}