Skip to content

Commit

Permalink
feat: support application/x-msdownload download
Browse files Browse the repository at this point in the history
  • Loading branch information
luhc228 committed Jun 5, 2024
1 parent 7715f23 commit c9f4bb7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
78 changes: 51 additions & 27 deletions src/utils/download_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,57 @@ pub async fn download_file(url: &str, set_process_message: impl Fn(&str)) -> Res
}

fn get_file_name_from_response(response: &Response) -> Result<String> {
let content_disposition = response
.headers()
.get("content-disposition")
.ok_or(anyhow::anyhow!("Failed to get content-disposition header"))?
.to_str()
.map_err(|err| anyhow::anyhow!("Failed to convert content-disposition header to string. Error: {}", err))?;
if content_disposition == "attachment" {
let url = response.url();
let filename = Url::parse(response.url().as_str())
.map_err(|err| anyhow::anyhow!("Failed to parse url '{}'. Error: {}", url, err))
.map(|url| url.path_segments().unwrap().last().unwrap().to_string())?;
Ok(filename)
} else {
let re =
Regex::new(r"filename=([^;]+)").map_err(|err| anyhow::anyhow!("Failed to create regex. Error: {}", err))?;
let file_name = re
.captures(content_disposition)
.ok_or(anyhow::anyhow!(
"Failed to get file name from content-disposition header"
))?
.get(1)
.ok_or(anyhow::anyhow!(
"Failed to get file name from content-disposition header"
))?
.as_str()
.replace('"', "");
let headers = response.headers();
if let Some(content_disposition) = headers.get("content-disposition") {
let content_disposition = content_disposition
.to_str()
.map_err(|err| anyhow::anyhow!("Failed to convert content-disposition header to string. Error: {}", err))?;
if content_disposition == "attachment" {
get_last_segment_from_url(response.url().as_str())
} else {
let re = Regex::new(r"filename=([^;]+)").map_err(|err| anyhow::anyhow!("Failed to create regex. Error: {}", err))?;
let file_name = re
.captures(content_disposition)
.ok_or(anyhow::anyhow!("failed to match filename field from content-disposition header"))?
.get(1)
.ok_or(anyhow::anyhow!("failed to get filename field from content-disposition header"))?
.as_str().replace('"', "");
Ok(file_name)
}
} else if let Some(content_type) = headers.get("content-type") {
let content_type = content_type
.to_str()
.map_err(|err| anyhow::anyhow!("Failed to convert content-type header to string. Error: {}", err))?;
// for Windows exe file
if content_type == "application/x-msdownload" || content_type == "application/vnd.microsoft.portable-executable" {
get_last_segment_from_url(response.url().as_str())
} else {
get_last_segment_from_url(response.url().as_str())
}
}else {
get_last_segment_from_url(response.url().as_str())
}
}

fn get_last_segment_from_url(url: &str) -> Result<String> {
let url = Url::parse(url)
.map_err(|err| anyhow::anyhow!("Failed to parse url '{}'. Error: {}", url, err))?;
Ok(url.path_segments().unwrap().last().unwrap().to_string())
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_download_file() -> Result<()> {
// Windows exe file
let download_result = download_file(
"https://releases.arc.net/windows/ArcInstaller.exe",
|_| {}
).await?;
assert!(download_result.exists());

Ok(file_name)
Ok(())
}
}
2 changes: 0 additions & 2 deletions src/utils/extract_zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ pub fn extract_zip<T: AsRef<Path>>(zip_path: T, extract_path: &str) -> Result<()
#[cfg(test)]
mod tests {
use std::fs;

use crate::download_file;

pub use super::*;

#[cfg(target_os = "macos")]
Expand Down

0 comments on commit c9f4bb7

Please sign in to comment.