diff --git a/i18n/zh-CN/oma.ftl b/i18n/zh-CN/oma.ftl index 1a5bdfad..626cacd9 100644 --- a/i18n/zh-CN/oma.ftl +++ b/i18n/zh-CN/oma.ftl @@ -233,7 +233,7 @@ tum-2 = 根据您指定的操作,oma 还需要对系统组件执行若干变 security = 安全更新 verify-error = 在验证 { $p } 的签名时遇到错误。 sources-list-empty = 软件源配置为空。 -failed-refresh = 无法刷新软件源数据, +failed-refresh = 无法刷新软件源数据。 unsupported-sources-list = APT 软件源配置文件 { $p } 不受支持:仅支持单行 ({ $list }) 及 DEB822 ({ $sources }) 格式的配置文件。 set-permission = 设置权限失败。 open-file-as-write-mode = 无法打开文件为写模式。 diff --git a/oma-refresh/src/db.rs b/oma-refresh/src/db.rs index df33ea67..9fb8fc73 100644 --- a/oma-refresh/src/db.rs +++ b/oma-refresh/src/db.rs @@ -352,7 +352,7 @@ impl<'a> OmaRefresh<'a> { let results = mirror_sources .fetch_all_release( - &self.client, + self.client, replacer, &self.download_dir, self.threads, @@ -680,7 +680,7 @@ fn collect_flat_repo_no_release( tasks: &mut Vec, replacer: &DatabaseFilenameReplacer, ) -> Result<()> { - let msg = mirror_source.get_human_download_url(Some("Packages"))?; + let msg = mirror_source.get_human_download_message(Some("Packages"))?; let dist_url = mirror_source.dist_path(); @@ -727,7 +727,7 @@ fn collect_download_task( ) -> Result<()> { let file_type = &c.msg; - let msg = mirror_source.get_human_download_url(Some(file_type))?; + let msg = mirror_source.get_human_download_message(Some(file_type))?; let dist_url = &mirror_source.dist_path(); diff --git a/oma-refresh/src/sourceslist.rs b/oma-refresh/src/sourceslist.rs index 2cda0b1f..049a54f1 100644 --- a/oma-refresh/src/sourceslist.rs +++ b/oma-refresh/src/sourceslist.rs @@ -233,7 +233,7 @@ impl MirrorSource<'_, '_> { self.sources.first().unwrap().from() } - pub fn get_human_download_url( + pub fn get_human_download_message( &self, file_name: Option<&str>, ) -> Result { @@ -313,11 +313,7 @@ impl MirrorSource<'_, '_> { { let dist_path = self.dist_path(); - let mut r = None; - let mut u = None; - let mut is_release = false; - - let msg = self.get_human_download_url(None)?; + let msg = self.get_human_download_message(None)?; callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner { index, @@ -325,47 +321,33 @@ impl MirrorSource<'_, '_> { })) .await; - for (index, file_name) in ["InRelease", "Release"].iter().enumerate() { - let url = format!("{}/{}", dist_path, file_name); - let request = build_request_with_basic_auth( - client, - Method::GET, - &self - .auth() - .map(|x| (x.login.to_string(), x.password.to_string())), - &url, - ); - - let resp = request - .send() - .await - .and_then(|resp| resp.error_for_status()); - - r = Some(resp); + let mut url = format!("{}/InRelease", dist_path); + let mut is_release = false; - if r.as_ref().unwrap().is_ok() { - u = Some(url); - if index == 1 { - is_release = true; + let resp = match self.send_request(client, &url, Method::GET).await { + Ok(resp) => resp, + Err(e) => { + debug!("{e}"); + url = format!("{}/Release", dist_path); + let resp = self.send_request(client, &url, Method::GET).await; + + if resp.is_err() && self.is_flat() { + // Flat repo no release + callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await; + return Ok(()); } - break; - } - } - let r = r.unwrap(); + is_release = true; - callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await; + callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await; - if r.is_err() && self.is_flat() { - // Flat repo no release - return Ok(()); - } + resp.map_err(|e| SingleDownloadError::ReqwestError { source: e }) + .map_err(|e| RefreshError::DownloadFailed(Some(e)))? + } + }; - let resp = r - .map_err(|e| SingleDownloadError::ReqwestError { source: e }) - .map_err(|e| RefreshError::DownloadFailed(Some(e)))?; + callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await; - let url = u.unwrap(); let file_name = replacer.replace(&url)?; self.download_file(&file_name, resp, index, total, download_dir, &callback) @@ -403,6 +385,27 @@ impl MirrorSource<'_, '_> { Ok(()) } + async fn send_request( + &self, + client: &Client, + url: &str, + method: Method, + ) -> Result { + let request = build_request_with_basic_auth( + client, + method, + &self + .auth() + .map(|x| (x.login.to_string(), x.password.to_string())), + url, + ); + + request + .send() + .await + .and_then(|resp| resp.error_for_status()) + } + async fn download_file( &self, file_name: &str, @@ -424,7 +427,7 @@ impl MirrorSource<'_, '_> { "({}/{}) {}", index, total, - self.get_human_download_url(Some(file_name)).unwrap(), + self.get_human_download_message(Some(file_name)).unwrap(), ), size: total_size, })); @@ -482,7 +485,7 @@ impl MirrorSource<'_, '_> { let mut name = None; - let msg = self.get_human_download_url(None)?; + let msg = self.get_human_download_message(None)?; callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner { index, diff --git a/src/error.rs b/src/error.rs index 1b944f1f..48ec148f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -427,10 +427,19 @@ impl From for OutputError { description: fl!("sources-list-empty"), source: None, }, - RefreshError::DownloadFailed(_) => Self { - description: fl!("failed-refresh"), - source: None, - }, + RefreshError::DownloadFailed(err) => { + if let Some(err) = err { + Self { + description: fl!("failed-refresh"), + source: Some(Box::new(OutputError::from(err))), + } + } else { + Self { + description: fl!("failed-refresh"), + source: None, + } + } + } RefreshError::OperateFile(path, error) => Self { description: fl!("failed-to-operate-path", p = path.display().to_string()), source: Some(Box::new(error)),