This commit is contained in:
JMARyA 2024-07-10 09:17:09 +02:00
parent fd581bf9d9
commit 704530ee41
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 357 additions and 261 deletions

594
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -127,15 +127,13 @@ impl Mirror {
// check if cache should be used
Self::create_cache_dir(p.parent().unwrap());
if !self.no_cache.is_match(path) {
if !self.is_cache_invalid(&p) {
if !self.no_cache.is_match(path) && !self.is_cache_invalid(&p) {
// use cache if present
if let Some(resp) = self.fetch_cache(&p, req).await {
log::info!("Returning {path} from cache");
return Some(resp);
}
}
}
// fetch from network, if no response (offline) then use cache
if let Some(resp) = self.fetch_network(path, &p).await {
@ -166,6 +164,7 @@ impl Mirror {
.into_response(req),
);
}
return Some(
actix_files::NamedFile::open_async(&p)
.await
@ -173,6 +172,7 @@ impl Mirror {
.into_response(req),
);
}
None
}
@ -193,13 +193,13 @@ impl Mirror {
log::info!("Fetching {path} from mirrors");
for mirror in mirrors {
let url = format!("{mirror}{path}");
let response = self.get_url(&url, local).await;
if let Some(res) = response {
if res.status().is_success() {
return Some(res);
if let Some(response) = self.get_url(&url, local).await {
if response.status().is_success() {
return Some(response);
}
}
}
None
}
@ -220,7 +220,7 @@ impl Mirror {
/// or `None` if there was an error during the request or response retrieval.
pub async fn get_url(&self, path: &str, save: &PathBuf) -> Option<HttpResponse> {
log::info!("Fetching {path}");
let response = reqwest::get(path).await.unwrap();
let response = reqwest::get(path).await.ok()?;
let status_code = response.status();
let body_bytes = response.bytes().await.ok()?;
if status_code.is_success() {