-
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.
Models are still not being published, so this fails downloading. Will work in the future hopufully.
- Loading branch information
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
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,48 @@ | ||
use std::process::exit; | ||
|
||
use log::{info, debug, error}; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::runtime::Runtime; | ||
use futures_util::StreamExt; | ||
use reqwest; | ||
|
||
|
||
// Run a tokio task that listens for ctrl+c | ||
async fn run_cancel_handler() { | ||
tokio::spawn(async move { | ||
match tokio::signal::ctrl_c().await { | ||
Ok(_) => { | ||
info!("Received Ctrl+C, terminating immediately."); | ||
exit(1); | ||
} | ||
Err(e) => error!("Error listening for SIGINT: {}", e), | ||
}; | ||
}); | ||
} | ||
|
||
// Download a file to a path | ||
pub fn download_file(url: &str, filepath: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
let runtime = Runtime::new()?; | ||
runtime.block_on(async { | ||
info!("Downloading file from '{url}'"); | ||
run_cancel_handler().await; | ||
// Create a download stream | ||
let response = reqwest::get(url).await?; | ||
let status = response.status(); | ||
debug!("Response status: {}", status); | ||
if !status.is_success() { | ||
error!("Could not download file, HTTP status code: {status}"); | ||
exit(1); | ||
} | ||
|
||
let mut response_stream = response.bytes_stream(); | ||
let mut outfile = tokio::fs::File::create(filepath).await?; | ||
|
||
debug!("Writing file to '{filepath}'"); | ||
// asyncronously write to the file every piece of bytes that come from the stream | ||
while let Some(bytes) = response_stream.next().await { | ||
outfile.write_all(&bytes?).await?; | ||
} | ||
Ok(()) | ||
}) | ||
} |