From 48fee6897097ef9eb5a21271d55155388a05a13b Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sat, 19 Oct 2024 14:25:52 +0200 Subject: fix(yt/download): Create the download dir, if it does not exist --- yt/src/download/mod.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/yt/src/download/mod.rs b/yt/src/download/mod.rs index a056f80..5032b0f 100644 --- a/yt/src/download/mod.rs +++ b/yt/src/download/mod.rs @@ -8,7 +8,7 @@ // You should have received a copy of the License along with this program. // If not, see . -use std::{collections::HashMap, str::FromStr, sync::Arc, time::Duration}; +use std::{collections::HashMap, io, str::FromStr, sync::Arc, time::Duration}; use crate::{ app::App, @@ -234,7 +234,38 @@ impl Downloader { .boxed() } - dir_size(fs::read_dir(&app.config.paths.download_dir).await?).await + let read_dir_result = match fs::read_dir(&app.config.paths.download_dir).await { + Ok(ok) => ok, + Err(err) => match err.kind() { + io::ErrorKind::NotFound => { + fs::create_dir_all(&app.config.paths.download_dir) + .await + .with_context(|| { + format!( + "Failed to create download dir at: '{}'", + &app.config.paths.download_dir.display() + ) + })?; + + info!( + "Created empty download dir at '{}'", + &app.config.paths.download_dir.display(), + ); + + // The new dir should not contain anything (otherwise we would not have had to + // create it) + return Ok(0); + } + err => Err(io::Error::from(err)).with_context(|| { + format!( + "Failed to get dir size of download dir at: '{}'", + &app.config.paths.download_dir.display() + ) + })?, + }, + }; + + dir_size(read_dir_result).await } async fn get_approx_video_size(&mut self, app: &App, video: &Video) -> Result { -- cgit 1.4.1