diff --git a/src/sites.rs b/src/sites.rs index 88e8804..17e5a23 100644 --- a/src/sites.rs +++ b/src/sites.rs @@ -3,6 +3,7 @@ use async_std::io::{BufReader, BufWriter, WriteExt}; use async_std::net::TcpStream; use async_std::stream::StreamExt; use async_std::sync::RwLock; +use async_std::task::JoinHandle; use async_std::{fs, task}; use camino::{Utf8Path, Utf8PathBuf}; use color_eyre::Result; @@ -12,7 +13,6 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; -use async_std::task::JoinHandle; #[derive(Serialize, Deserialize)] pub struct SiteState { @@ -152,9 +152,12 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont // Download let archive_file_path = site.path.join(format!("{}.zip", current_version.id)); - let mut response = task::spawn_blocking({ + let response = task::spawn_blocking({ let url = current_version.download_url.clone(); - move || ureq_agent.get(url).call() }).await?; + move || ureq_agent.get(url).call() + }) + .await?; + let status = response.status(); if !status.is_success() { return Err(eyre!("Download request failed with status code {status}")); @@ -162,20 +165,23 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont let archive_file = task::spawn_blocking({ let archive_file_path = archive_file_path.to_owned().into_std_path_buf(); - - move || -> std::io::Result { - let mut writer = std::io::BufWriter::new(std::fs::OpenOptions::new() - .truncate(true) - .create(true) - .write(true) - .read(true) - .open(archive_file_path)?); - std::io::copy(&mut response.body_mut().as_reader(), &mut writer)?; + move || -> std::io::Result { + let mut writer = std::io::BufWriter::new( + std::fs::OpenOptions::new() + .truncate(true) + .create(true) + .write(true) + .read(true) + .open(archive_file_path)?, + ); + + std::io::copy(&mut response.into_body().into_reader(), &mut writer)?; Ok(writer.into_inner()?) } - }).await?; + }) + .await?; let extraction_directory_path = site.path.join(¤t_version.id); let _ = fs::remove_dir_all(extraction_directory_path.as_std_path()).await; @@ -207,7 +213,7 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont // Cleanup fs::remove_file(archive_file_path.as_std_path()).await?; if let Some(old_active_version) = old_active_version { - fs::remove_dir_all(site.path.join(old_active_version.id).into_std_path_buf()).await?; + let _ = fs::remove_dir_all(site.path.join(old_active_version.id).into_std_path_buf()).await; } info!("Cleanup finished for {domain}"); @@ -215,13 +221,18 @@ async fn handle_download(ureq_agent: Arc, sites: &Sites, caddy_cont Ok(()) } -async fn sites_worker(mut download_tasks_receiver: async_std::channel::Receiver, sites: Arc, mut caddy_controller: CaddyController) -> Result { - let mut ureq_agent = Arc::new( - ureq::Agent::new_with_config(ureq::Agent::config_builder() +async fn sites_worker( + mut download_tasks_receiver: async_std::channel::Receiver, + sites: Arc, + mut caddy_controller: CaddyController, +) -> Result { + let ureq_agent = Arc::new(ureq::Agent::new_with_config( + ureq::Agent::config_builder() + .timeout_resolve(Some(Duration::from_secs(10))) .timeout_connect(Some(Duration::from_secs(30))) .timeout_global(Some(Duration::from_mins(15))) - .build()) - ); + .build(), + )); loop { let domain = download_tasks_receiver.recv().await.unwrap();