summaryrefslogtreecommitdiff
path: root/src/net/http_packages.rs
blob: fe1833d87f2deaacdf084decc61a047e2cc21f59 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::cfg::config::Config;
use reqwest;
use std::fs::File;
use std::io::Write;
use std::path::Path;

#[allow(dead_code)]
pub struct HTTPPackage {
    config: Config,
}

#[allow(dead_code)]
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() -> 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)
    }
}