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) } }