Move utils::download_file into commands/download.rs

This commit is contained in:
blankie 2023-10-28 11:09:48 +11:00
parent bb80105b4a
commit 9ca4dda9d9
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 19 additions and 19 deletions

View File

@ -3,6 +3,8 @@ use crate::utils;
use crate::structs;
use std::env;
use std::fs::File;
use std::io::Write;
use std::sync::Arc;
use std::path::Path;
use std::process::exit;
@ -94,7 +96,7 @@ pub async fn run(args: env::Args) {
let mut tmp_file_path = file_path.clone();
tmp_file_path.push_str(".tmp");
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) => {
if success {
break;
@ -113,3 +115,19 @@ pub async fn run(args: env::Args) {
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
})
}

View File

@ -1,22 +1,4 @@
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> {
let mut sauces: Vec<i32> = Vec::new();