about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-10-19 14:25:52 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-10-19 14:25:52 +0200
commit48fee6897097ef9eb5a21271d55155388a05a13b (patch)
tree37c48daa254d5a91c303c534e7f264bb43af2406
parentchore(version): v1.3.0 (diff)
downloadyt-48fee6897097ef9eb5a21271d55155388a05a13b.tar.gz
yt-48fee6897097ef9eb5a21271d55155388a05a13b.zip
fix(yt/download): Create the download dir, if it does not exist
-rw-r--r--yt/src/download/mod.rs35
1 files 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 <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-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<u64> {