-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
876f10b
commit 2d74a14
Showing
6 changed files
with
240 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::cli::FetchArgs; | ||
use anyhow::Context; | ||
|
||
pub async fn fetch(fetch_args: FetchArgs) -> anyhow::Result<()> { | ||
let fetch_url = url::Url::parse(&fetch_args.url).context("Invalid URL")?; | ||
let tmp_file = tempfile::Builder::new() | ||
.prefix("checked-") | ||
.suffix(".unverified") | ||
.tempfile() | ||
.context("Could not create temporary file")?; | ||
|
||
let client = reqwest::Client::new(); | ||
|
||
let resp = client | ||
.head(fetch_url.clone()) | ||
.send() | ||
.await? | ||
.error_for_status()?; | ||
|
||
let content_length = resp | ||
.headers() | ||
.get("content-length") | ||
.and_then(|l| l.to_str().ok()) | ||
.and_then(|l| l.parse::<usize>().ok()) | ||
.unwrap_or(0); | ||
|
||
println!("Content length: {}", content_length); | ||
|
||
let mut res = reqwest::get(fetch_args.url).await?; | ||
|
||
while let Some(c) = res.chunk()? { | ||
tmp_file.as_file_mut().write_all(&c)?; | ||
} | ||
|
||
println!("Did initial request"); | ||
|
||
let content = res.bytes().await?; | ||
|
||
println!("Reading body"); | ||
|
||
std::io::copy(&mut content.as_ref(), &mut tmp_file.as_file())?; | ||
|
||
println!("Downloaded to {:?}", tmp_file.path()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters