From 9ca4dda9d9a5db0e282981e8351a860fb2aedf6d Mon Sep 17 00:00:00 2001 From: blankie Date: Sat, 28 Oct 2023 11:09:48 +1100 Subject: [PATCH] Move utils::download_file into commands/download.rs --- src/commands/download.rs | 20 +++++++++++++++++++- src/utils.rs | 18 ------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/commands/download.rs b/src/commands/download.rs index 9f93c50..556f458 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -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 { + 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 + }) +} + diff --git a/src/utils.rs b/src/utils.rs index 91defeb..e6971b5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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 { - 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, String> { let mut sauces: Vec = Vec::new();