From 94c656ad40a7aae570e5a5fb61ad32632acc6d46 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 23 Aug 2024 13:11:09 +0200 Subject: feat(treewide): Use a configuration file This allows use to avoid duplication of default values in the codebase and obviously also facilitates changing these without having to re-compile. --- src/download/mod.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/download/mod.rs') diff --git a/src/download/mod.rs b/src/download/mod.rs index c3d79b7..707f281 100644 --- a/src/download/mod.rs +++ b/src/download/mod.rs @@ -12,7 +12,6 @@ use std::{collections::HashMap, sync::Arc, time::Duration}; use crate::{ app::App, - constants::download_dir, download::download_options::download_opts, storage::video_database::{ downloader::{get_next_uncached_video, set_video_cache_path}, @@ -72,8 +71,8 @@ impl Downloader { /// This will run, until the database doesn't contain any watchable videos pub async fn consume(&mut self, app: Arc, max_cache_size: u64) -> Result<()> { while let Some(next_video) = get_next_uncached_video(&app).await? { - if Self::get_current_cache_allocation().await? - + self.get_approx_video_size(&next_video).await? + if Self::get_current_cache_allocation(&app).await? + + self.get_approx_video_size(&app, &next_video).await? >= max_cache_size { warn!( @@ -134,7 +133,7 @@ impl Downloader { Ok(()) } - async fn get_current_cache_allocation() -> Result { + async fn get_current_cache_allocation(app: &App) -> Result { fn dir_size(mut dir: fs::ReadDir) -> BoxFuture<'static, Result> { async move { let mut acc = 0; @@ -155,14 +154,14 @@ impl Downloader { .boxed() } - let val = dir_size(fs::read_dir(download_dir(true)?).await?).await; + let val = dir_size(fs::read_dir(&app.config.paths.download_dir).await?).await; if let Ok(val) = val.as_ref() { info!("Cache dir has a size of '{}'", val); } val } - async fn get_approx_video_size(&mut self, video: &Video) -> Result { + async fn get_approx_video_size(&mut self, app: &App, video: &Video) -> Result { if let Some(value) = self.video_size_cache.get(&video.extractor_hash) { Ok(*value) } else { @@ -170,7 +169,7 @@ impl Downloader { let add_opts = YtDlpOptions { subtitle_langs: "".to_owned(), }; - let opts = &download_opts(add_opts); + let opts = &download_opts(&app, add_opts); let result = yt_dlp::extract_info(&opts, &video.url, false, true) .await @@ -201,7 +200,7 @@ impl Downloader { let addional_opts = get_video_yt_dlp_opts(&app, &video.extractor_hash).await?; - let result = yt_dlp::download(&[video.url.clone()], &download_opts(addional_opts)) + let result = yt_dlp::download(&[video.url.clone()], &download_opts(&app, addional_opts)) .await .with_context(|| format!("Failed to download video: '{}'", video.title))?; -- cgit 1.4.1