soar_dl/
common.rs

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use std::{env, sync::Arc};

use regex::Regex;
use reqwest::{
    header::{HeaderMap, AUTHORIZATION, USER_AGENT},
    StatusCode,
};
use serde::Deserialize;

use crate::{
    downloader::{DownloadOptions, DownloadState, Downloader},
    error::{DownloadError, PlatformError},
};

pub enum ApiType {
    PkgForge,
    Primary,
}

pub fn should_fallback(status: StatusCode) -> bool {
    status == StatusCode::TOO_MANY_REQUESTS
        || status == StatusCode::UNAUTHORIZED
        || status == StatusCode::FORBIDDEN
        || status.is_server_error()
}

pub trait DownloadableAsset {
    fn name(&self) -> &str;
    fn size(&self) -> u64;
    fn download_url(&self) -> &str;
}

pub trait ReleasePlatform {
    const API_BASE_PRIMARY: &'static str;
    const API_BASE_PKGFORGE: &'static str;
    const TOKEN_ENV_VAR: &'static str;

    fn format_project_path(project: &str) -> Result<(String, String), PlatformError>;
    fn format_api_path(project: &str) -> Result<String, PlatformError>;
}

pub trait ReleaseAsset {
    fn name(&self) -> &str;
    fn size(&self) -> Option<u64>;
    fn download_url(&self) -> &str;
}

pub trait Release<A: ReleaseAsset> {
    fn name(&self) -> &str;
    fn tag_name(&self) -> &str;
    fn is_prerelease(&self) -> bool;
    fn published_at(&self) -> &str;
    fn assets(&self) -> Vec<A>;
}

#[derive(Clone)]
pub struct PlatformDownloadOptions {
    pub output_path: Option<String>,
    pub progress_callback: Option<Arc<dyn Fn(DownloadState) + Send + Sync + 'static>>,
    pub tag: Option<String>,
    pub regex_patterns: Vec<Regex>,
    pub match_keywords: Vec<String>,
    pub exclude_keywords: Vec<String>,
    pub exact_case: bool,
}

#[derive(Default)]
pub struct ReleaseHandler<P: ReleasePlatform> {
    downloader: Downloader,
    client: reqwest::Client,
    _platform: std::marker::PhantomData<P>,
}

impl<P: ReleasePlatform> ReleaseHandler<P> {
    pub fn new() -> Self {
        Self {
            downloader: Downloader::default(),
            client: reqwest::Client::new(),
            _platform: std::marker::PhantomData,
        }
    }

    async fn call_api(
        &self,
        api_type: &ApiType,
        project: &str,
    ) -> Result<reqwest::Response, PlatformError> {
        let base_url = match api_type {
            ApiType::PkgForge => P::API_BASE_PKGFORGE,
            ApiType::Primary => P::API_BASE_PRIMARY,
        };

        let api_path = P::format_api_path(project)?;
        let url = format!("{}{}", base_url, api_path);

        let mut headers = HeaderMap::new();
        headers.insert(USER_AGENT, "pkgforge/soar".parse().unwrap());

        if matches!(api_type, ApiType::Primary) {
            if let Ok(token) = env::var(P::TOKEN_ENV_VAR) {
                headers.insert(AUTHORIZATION, format!("Bearer {}", token).parse().unwrap());
            }
        }

        Ok(self
            .client
            .get(&url)
            .headers(headers)
            .send()
            .await
            .map_err(|err| DownloadError::NetworkError { source: err })?)
    }

    pub async fn fetch_releases<R>(&self, project: &str) -> Result<Vec<R>, PlatformError>
    where
        R: for<'de> Deserialize<'de>,
    {
        let response = match self.call_api(&ApiType::PkgForge, project).await {
            Ok(resp) => {
                let status = resp.status();
                if should_fallback(status) {
                    self.call_api(&ApiType::Primary, project).await?
                } else {
                    resp
                }
            }
            Err(err) => return Err(err),
        };

        if !response.status().is_success() {
            return Err(DownloadError::ResourceError {
                url: response.url().to_string(),
                status: response.status(),
            }
            .into());
        }

        response
            .json()
            .await
            .map_err(|_| PlatformError::InvalidResponse)
    }

    pub async fn filter_releases<R, A>(
        &self,
        releases: &[R],
        options: &PlatformDownloadOptions,
    ) -> Result<Vec<A>, PlatformError>
    where
        R: Release<A>,
        A: ReleaseAsset + Clone,
    {
        let release = if let Some(ref tag_name) = options.tag {
            releases
                .iter()
                .find(|release| release.tag_name().starts_with(tag_name))
        } else {
            releases
                .iter()
                .find(|release| !release.is_prerelease())
                .map_or_else(|| releases.first(), Some)
        };

        let Some(release) = release else {
            return Err(PlatformError::NoRelease {
                tag: options.tag.clone(),
            });
        };

        let assets: Vec<A> = release
            .assets()
            .into_iter()
            .filter(|asset| {
                let name = asset.name();
                options
                    .regex_patterns
                    .iter()
                    .all(|regex| regex.is_match(name))
                    && options.match_keywords.iter().all(|keyword| {
                        keyword
                            .split(',')
                            .map(str::trim)
                            .filter(|s| !s.is_empty())
                            .all(|part| {
                                let (asset_name, part) = if options.exact_case {
                                    (name.to_string(), part.to_string())
                                } else {
                                    (name.to_lowercase(), part.to_lowercase())
                                };
                                asset_name.contains(&part)
                            })
                    })
                    && options.exclude_keywords.iter().all(|keyword| {
                        keyword
                            .split(',')
                            .map(str::trim)
                            .filter(|s| !s.is_empty())
                            .all(|part| {
                                let (asset_name, part) = if options.exact_case {
                                    (name.to_string(), part.to_string())
                                } else {
                                    (name.to_lowercase(), part.to_lowercase())
                                };
                                !asset_name.contains(&part)
                            })
                    })
            })
            .collect();

        if assets.is_empty() {
            return Err(PlatformError::NoMatchingAssets {
                available_assets: release
                    .assets()
                    .into_iter()
                    .map(|a| a.name().to_string())
                    .collect(),
            });
        }

        Ok(assets)
    }

    pub async fn download<A: ReleaseAsset>(
        &self,
        asset: &A,
        options: PlatformDownloadOptions,
    ) -> Result<String, PlatformError> {
        Ok(self
            .downloader
            .download(DownloadOptions {
                url: asset.download_url().to_string(),
                output_path: options.output_path,
                progress_callback: options.progress_callback,
            })
            .await?)
    }
}