about summary refs log tree commit diff stats
path: root/src/constants.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/constants.rs14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/constants.rs b/src/constants.rs
index f4eef3d..00919ce 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -8,9 +8,9 @@
 // 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::{env::temp_dir, path::PathBuf};
+use std::{env::temp_dir, fs, path::PathBuf};
 
-use anyhow::Context;
+use anyhow::{Context, Result};
 
 pub const HELP_STR: &str = include_str!("./select/selection_file/help.str");
 pub const LOCAL_COMMENTS_LENGTH: usize = 1000;
@@ -21,9 +21,15 @@ pub const DEFAULT_SUBTITLE_LANGS: &str = "en";
 
 pub const CONCURRENT_DOWNLOADS: u32 = 5;
 // We download to the temp dir to avoid taxing the disk
-pub fn download_dir() -> PathBuf {
+pub fn download_dir(create: bool) -> Result<PathBuf> {
     const DOWNLOAD_DIR: &str = "/tmp/yt";
-    PathBuf::from(DOWNLOAD_DIR)
+    let dir = PathBuf::from(DOWNLOAD_DIR);
+
+    if !dir.exists() && create {
+        fs::create_dir_all(&dir).context("Failed to create the download directory")?
+    }
+
+    Ok(dir)
 }
 
 const PREFIX: &str = "yt";