Move utils::download_file into commands/download.rs
This commit is contained in:
parent
bb80105b4a
commit
9ca4dda9d9
|
@ -3,6 +3,8 @@ use crate::utils;
|
||||||
use crate::structs;
|
use crate::structs;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
@ -94,7 +96,7 @@ pub async fn run(args: env::Args) {
|
||||||
let mut tmp_file_path = file_path.clone();
|
let mut tmp_file_path = file_path.clone();
|
||||||
tmp_file_path.push_str(".tmp");
|
tmp_file_path.push_str(".tmp");
|
||||||
loop {
|
loop {
|
||||||
match utils::download_file(cloned_client.clone(), &url, &tmp_file_path).await {
|
match download_file(cloned_client.clone(), &url, &tmp_file_path).await {
|
||||||
Ok(success) => {
|
Ok(success) => {
|
||||||
if success {
|
if success {
|
||||||
break;
|
break;
|
||||||
|
@ -113,3 +115,19 @@ pub async fn run(args: env::Args) {
|
||||||
handle.await.unwrap();
|
handle.await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<bool, reqwest::Error> {
|
||||||
|
let resp = client.get(url)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
Ok(match resp.headers().get("Content-Type") {
|
||||||
|
Some(header) if header.to_str().unwrap_or_default().starts_with("image/") => {
|
||||||
|
let bytes = resp.bytes().await?;
|
||||||
|
let mut file = File::create(&file_name).unwrap();
|
||||||
|
file.write_all(&bytes).unwrap();
|
||||||
|
true
|
||||||
|
},
|
||||||
|
_ => false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
18
src/utils.rs
18
src/utils.rs
|
@ -1,22 +1,4 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Write;
|
|
||||||
extern crate reqwest;
|
|
||||||
|
|
||||||
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<bool, reqwest::Error> {
|
|
||||||
let resp = client.get(url)
|
|
||||||
.send()
|
|
||||||
.await?;
|
|
||||||
Ok(match resp.headers().get("Content-Type") {
|
|
||||||
Some(header) if header.to_str().unwrap_or_default().starts_with("image/") => {
|
|
||||||
let bytes = resp.bytes().await?;
|
|
||||||
let mut file = File::create(&file_name).unwrap();
|
|
||||||
file.write_all(&bytes).unwrap();
|
|
||||||
true
|
|
||||||
},
|
|
||||||
_ => false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_arg_sauces(args: env::Args) -> Result<Vec<i32>, String> {
|
pub fn get_arg_sauces(args: env::Args) -> Result<Vec<i32>, String> {
|
||||||
let mut sauces: Vec<i32> = Vec::new();
|
let mut sauces: Vec<i32> = Vec::new();
|
||||||
|
|
Loading…
Reference in New Issue