From d10ac4cc08d679e7971296b79c6eafadcdbc78de Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Sat, 29 Nov 2025 20:26:35 +0300 Subject: Big refactors and additions, implemented http package getter and refactored i2p fn --- src/net/http_packages.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/net/http_packages.rs (limited to 'src/net/http_packages.rs') 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. + pub fn parse_config(config_path: &str) -> Result> { + 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 { + 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) + } +} + -- cgit v1.2.3