diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-11-29 20:26:35 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-11-29 20:26:35 +0300 |
| commit | d10ac4cc08d679e7971296b79c6eafadcdbc78de (patch) | |
| tree | d12c170704d0632fcdfde809879ec9c477ad8574 /src/net/http_packages.rs | |
| parent | 2536cfdb527cd252ed926e3c05313430858a4ca6 (diff) | |
Big refactors and additions, implemented http package getter and refactored i2p fn
Diffstat (limited to 'src/net/http_packages.rs')
| -rw-r--r-- | src/net/http_packages.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/net/http_packages.rs b/src/net/http_packages.rs new file mode 100644 index 0000000..1e3c6ad --- /dev/null +++ b/src/net/http_packages.rs @@ -0,0 +1,92 @@ +use reqwest; +use std::fs::File; +use std::io::Write; +use std::path::Path; +use crate::cfg::config::Config; + +pub struct HTTPPackage { + config: Config, +} + +impl HTTPPackage { + /// Creates a new Downloader object with the given configuration. + /// + /// # Arguments + /// + /// * `config` - The full mesk + /// + /// # Returns + /// + /// A new Downloader object with the given configuration. + pub fn new(config: Config) -> Self { + Self { config } + } + + /// Parse the mesk configuration file and return the Config object. + /// + /// This function reads the file at `config_path`, parses it and returns the Config object. + /// + /// # Arguments + /// + /// * `config_path` - A string representing the path to the mesk configuration file. + /// + /// # Returns + /// + /// A new Config object with the parsed configuration. If there's an error while reading or parsing the file, returns an Err containing a Box<dyn std::error::Error>. + pub fn parse_config(config_path: &str) -> Result<Config, Box<dyn std::error::Error>> { + let config = Config::parse()?; + Ok(config) + } + + + /// Downloads the INDEX.tar.gz file from the configured repository + /// and stores it in the configured cache directory. + /// + /// # Errors + /// + /// Returns an error if the request fails, if the response status is not successful, or if there's an issue while reading or writing the file. + /// + pub async fn fetch_index_http(&mut self) -> Result<bool, std::io::Error> { + let repo_url_str = &self.config.repo.repo_url; + let cache_dir = &self.config.paths.cache_dir; + + let index_url = if repo_url_str.ends_with(".tar.gz") { + repo_url_str.clone() + } else { + format!("{}/INDEX.tar.gz", repo_url_str.trim_end_matches('/')) + }; + + let client = reqwest::Client::new(); + + let response = client + .get(&index_url) + .send() + .await + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Request failed: {}", e)))?; + + if !response.status().is_success() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("HTTP Error: {}", response.status()), + )); + } + + let index_data = response + .bytes() + .await + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to read response body: {}", e)))? + .to_vec(); + + let file_path = Path::new(cache_dir).join("INDEX.tar.gz"); + + let mut file = File::create(&file_path) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to create file: {}", e)))?; + file.write_all(&index_data) + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to write file: {}", e)))?; + file.flush() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("Failed to flush file: {}", e)))?; + + Ok(true) + } +} + |
