diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/downloader.rs | 40 | ||||
| -rw-r--r-- | src/error.rs | 4 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/downloader.rs b/src/downloader.rs index 0141424..e40d5d1 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -20,6 +20,43 @@ impl Downloader { Ok(appimages_dir()?.join(filename)) } + pub fn validate_response(&self, resp: &reqwest::Response) -> Result<()> { + if !resp.status().is_success() { + return Err(Error::Download { + url: resp.url().to_string(), + source: resp.error_for_status_ref().unwrap_err(), + }); + } + + if let Some(len) = resp.content_length() { + if len < 1024 { + return Err(Error::InvalidAppImage); + } + } + + let content_type = resp + .headers() + .get("content-type") + .and_then(|ct| ct.to_str().ok()) + .unwrap_or("") + .to_lowercase(); + + let is_binary = matches!( + content_type.as_str(), + "application/octet-stream" + | "application/vnd.appimage" + | "application/x-executable" + | "application/x-elf" + | "binary/octet-stream" + | "application/binary", + ); + + if !is_binary { + return Err(Error::InvalidAppImage); + } + + Ok(()) + } pub async fn download_with_progress(&self, url: &str, path: &PathBuf) -> Result<()> { fs::create_dir_all(&appimages_dir()?).await?; @@ -29,6 +66,9 @@ impl Downloader { url: url.to_string(), source, })?; + + self.validate_response(&resp)?; + let total_size = resp.content_length().unwrap_or(0); let bar = make_progress_bar(total_size)?; diff --git a/src/error.rs b/src/error.rs index b473469..25fc685 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ pub enum Error { url: String, source: reqwest::Error, }, + InvalidAppImage, #[from] Io(std::io::Error), @@ -56,6 +57,9 @@ impl core::fmt::Display for Error { write!(fmt, "Failed to download from {url}: {source}") } } + Error::InvalidAppImage => { + write!(fmt, "Invalid AppImage") + } } } } |
