Skip to content

Commit 077f9d3

Browse files
authored
fix(oma-fetch)!: local file path do not decode url two times (#364)
1 parent 7902084 commit 077f9d3

File tree

5 files changed

+40
-47
lines changed

5 files changed

+40
-47
lines changed

Cargo.lock

Lines changed: 4 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oma-fetch/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ faster-hex = "0.10"
1616
sha2 = "0.10"
1717
futures = "0.3"
1818
async-compression = { version = "0.4", features = ["gzip", "xz", "futures-io", "bzip2", "zstd"] }
19-
oma-utils = { version = "^0.11", features = ["url-no-escape"] }
2019
tracing = "0.1"
2120
tokio-util = { version = "0.7", features = ["compat"] }
2221
md-5 = "0.10.6"

oma-fetch/src/download.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::{
1111
use async_compression::futures::bufread::{BzDecoder, GzipDecoder, XzDecoder, ZstdDecoder};
1212
use bon::Builder;
1313
use futures::{AsyncRead, TryStreamExt, io::BufReader};
14-
use oma_utils::url_no_escape::url_no_escape;
1514
use reqwest::{
1615
Client, Method, RequestBuilder,
1716
header::{ACCEPT_RANGES, CONTENT_LENGTH, HeaderValue, RANGE},
@@ -219,33 +218,33 @@ impl SingleDownloader<'_> {
219218
F: Fn(Event) -> Fut,
220219
Fut: Future<Output = ()>,
221220
{
222-
let file = self.entry.dir.join(&*self.entry.filename);
223-
let file_exist = file.exists();
224-
let mut file_size = file.metadata().ok().map(|x| x.len()).unwrap_or(0);
221+
let path = self.entry.dir.join(&*self.entry.filename);
222+
let file_exist = path.exists();
223+
let mut file_size = path.metadata().ok().map(|x| x.len()).unwrap_or(0);
225224

226-
debug!("{} Exist file size is: {file_size}", file.display());
227-
debug!("{} download url is: {}", file.display(), source.url);
225+
debug!("{} Exist file size is: {file_size}", path.display());
226+
debug!("{} download url is: {}", path.display(), source.url);
228227
let mut dest = None;
229228
let mut validator = None;
230229

231-
if file.is_symlink() {
232-
tokio::fs::remove_file(&file).await.context(RemoveSnafu)?;
230+
if path.is_symlink() {
231+
tokio::fs::remove_file(&path).await.context(RemoveSnafu)?;
233232
}
234233

235234
// 如果要下载的文件已经存在,则验证 Checksum 是否正确,若正确则添加总进度条的进度,并返回
236235
// 如果不存在,则继续往下走
237-
if file_exist && !file.is_symlink() {
236+
if file_exist && !path.is_symlink() {
238237
debug!("File: {} exists", self.entry.filename);
239238

240-
self.set_permission_with_path(&file).await?;
239+
self.set_permission_with_path(&path).await?;
241240

242241
if let Some(hash) = &self.entry.hash {
243242
debug!("Hash exist! It is: {}", hash);
244243

245244
let mut f = tokio::fs::OpenOptions::new()
246245
.write(true)
247246
.read(true)
248-
.open(&file)
247+
.open(&path)
249248
.await
250249
.context(OpenAsWriteModeSnafu)?;
251250

@@ -399,7 +398,7 @@ impl SingleDownloader<'_> {
399398
self.entry.filename
400399
);
401400

402-
let f = match File::create(&file).await {
401+
let f = match File::create(&path).await {
403402
Ok(f) => f,
404403
Err(e) => {
405404
callback(Event::ProgressDone(self.download_list_index)).await;
@@ -429,7 +428,7 @@ impl SingleDownloader<'_> {
429428
self.entry.filename
430429
);
431430

432-
let f = match File::create(&file).await {
431+
let f = match File::create(&path).await {
433432
Ok(f) => f,
434433
Err(e) => {
435434
callback(Event::ProgressDone(self.download_list_index)).await;
@@ -599,12 +598,7 @@ impl SingleDownloader<'_> {
599598
debug!("{:?}", self.entry);
600599
let msg = self.progress_msg();
601600

602-
let url = &source.url;
603-
604-
// 传入的参数不对,应该 panic
605-
let url_path = url_no_escape(url.strip_prefix("file:").unwrap());
606-
607-
let url_path = Path::new(&url_path);
601+
let url_path = Path::new(&source.url);
608602

609603
let total_size = tokio::fs::metadata(url_path)
610604
.await

oma-pm/src/download.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use oma_fetch::{
66
checksum::Checksum, reqwest::Client,
77
};
88
use oma_pm_operation_type::InstallEntry;
9+
use oma_utils::url_no_escape::url_no_escape_times;
910
use tracing::debug;
1011

1112
use crate::apt::{DownloadConfig, OmaAptError, OmaAptResult};
@@ -48,20 +49,23 @@ where
4849
let sources = uris
4950
.iter()
5051
.map(|x| {
51-
let source_type = if x.index_url.starts_with("file:") {
52-
DownloadSourceType::Local(!download_only)
52+
let (source_type, url) = if let Some(url) = x.download_url.strip_prefix("file:") {
53+
(
54+
DownloadSourceType::Local(!download_only),
55+
url_no_escape_times(url, 1),
56+
)
5357
} else {
5458
let auth = auth.and_then(|auth| auth.find(&x.index_url));
5559

56-
DownloadSourceType::Http {
57-
auth: auth.map(|x| (x.login.to_owned(), x.password.to_owned())),
58-
}
60+
(
61+
DownloadSourceType::Http {
62+
auth: auth.map(|x| (x.login.to_owned(), x.password.to_owned())),
63+
},
64+
x.index_url.to_string(),
65+
)
5966
};
6067

61-
DownloadSource {
62-
url: x.download_url.clone(),
63-
source_type,
64-
}
68+
DownloadSource { url, source_type }
6569
})
6670
.collect::<Vec<_>>();
6771

oma-utils/src/url_no_escape/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ pub fn url_no_escape(s: &str) -> String {
1717
c += 1;
1818
}
1919
}
20+
21+
/// Get escape url for decode times
22+
pub fn url_no_escape_times(s: &str, times: usize) -> String {
23+
let mut tmp = s.to_string();
24+
for _ in 1..=times {
25+
tmp = url_escape::decode(&tmp).to_string();
26+
}
27+
28+
tmp
29+
}

0 commit comments

Comments
 (0)