summaryrefslogtreecommitdiff
path: root/src/net/http_packages.rs
blob: 1e3c6adce7452789eb9fb87b6339b20b091ce9a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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)
    }
}