diff --git a/src/ilias.rs b/src/ilias.rs index 88264e0..0d4eb47 100644 --- a/src/ilias.rs +++ b/src/ilias.rs @@ -55,7 +55,12 @@ fn error_is_http2(error: &reqwest::Error) -> bool { impl ILIAS { // TODO: de-duplicate the logic below - pub async fn with_session(opt: Opt, session: Arc, ignore: Gitignore, course_names: HashMap) -> Result { + pub async fn with_session( + opt: Opt, + session: Arc, + ignore: Gitignore, + course_names: HashMap, + ) -> Result { let mut builder = Client::builder() .cookie_provider(Arc::clone(&session)) .user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))); @@ -76,7 +81,13 @@ impl ILIAS { }) } - pub async fn login(opt: Opt, user: &str, pass: &str, ignore: Gitignore, course_names: HashMap) -> Result { + pub async fn login( + opt: Opt, + user: &str, + pass: &str, + ignore: Gitignore, + course_names: HashMap, + ) -> Result { let cookie_store = CookieStore::default(); let cookie_store = reqwest_cookie_store::CookieStoreMutex::new(cookie_store); let cookie_store = std::sync::Arc::new(cookie_store); @@ -464,6 +475,12 @@ impl Object { _ => Generic { name, url }, }) } + + pub(crate) fn is_ignored_by_option(&self, opt: &Opt) -> bool { + (matches!(self, Object::Forum { .. }) && !opt.forum) + || (matches!(self, Object::Video { .. }) && opt.no_videos) + || (matches!(self, Object::File { .. }) && opt.skip_files) + } } #[allow(non_snake_case)] diff --git a/src/ilias/folder.rs b/src/ilias/folder.rs index 3f2e9f2..962192a 100644 --- a/src/ilias/folder.rs +++ b/src/ilias/folder.rs @@ -22,7 +22,9 @@ pub async fn download(path: &Path, ilias: Arc, url: &URL) -> Result<()> { } for item in content.0 { let item = item?; - let path = path.join(file_escape(ilias.course_names.get(item.name()).map(|x| &**x).unwrap_or(item.name()))); + let path = path.join(file_escape( + ilias.course_names.get(item.name()).map(|x| &**x).unwrap_or(item.name()), + )); let ilias = Arc::clone(&ilias); spawn(process_gracefully(ilias, path, item)); } diff --git a/src/ilias/video.rs b/src/ilias/video.rs index 12bd4b5..ca604a1 100644 --- a/src/ilias/video.rs +++ b/src/ilias/video.rs @@ -1,4 +1,8 @@ -use std::{path::{Path, PathBuf}, process::{ExitStatus, Stdio}, sync::Arc}; +use std::{ + path::{Path, PathBuf}, + process::{ExitStatus, Stdio}, + sync::Arc, +}; use anyhow::{Context, Result}; use once_cell::sync::Lazy; @@ -59,7 +63,7 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: } arguments.push("-c".into()); arguments.push("copy".into()); - for i in 0..(arguments.len() / 2)-1 { + for i in 0..(arguments.len() / 2) - 1 { arguments.push("-map".into()); arguments.push(format!("{}", i)); } @@ -70,7 +74,8 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: .stdout(Stdio::null()) .spawn() .context("failed to start ffmpeg")? - .wait().await + .wait() + .await .context("failed to wait for ffmpeg")?; if !status.success() { error!(format!("ffmpeg failed to merge video files into {}", path.display())); @@ -82,7 +87,12 @@ pub async fn download(path: &Path, relative_path: &Path, ilias: Arc, url: Ok(()) } -async fn download_all(path: &Path, streams: &Vec, ilias: Arc, relative_path: &Path) -> Result> { +async fn download_all( + path: &Path, + streams: &Vec, + ilias: Arc, + relative_path: &Path, +) -> Result> { let mut paths = Vec::new(); for (i, stream) in streams.into_iter().enumerate() { let url = stream @@ -91,7 +101,13 @@ async fn download_all(path: &Path, streams: &Vec, ilias: Arc< .as_str() .context("video src not string")?; let new_path = path.join(format!("Stream{}.mp4", i + 1)); - download_to_path(&ilias, &new_path, &relative_path.join(format!("Stream{}.mp4", i + 1)), url).await?; + download_to_path( + &ilias, + &new_path, + &relative_path.join(format!("Stream{}.mp4", i + 1)), + url, + ) + .await?; paths.push(new_path); } Ok(paths) diff --git a/src/main.rs b/src/main.rs index f79fa2b..262d57b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,12 +124,17 @@ async fn real_main(mut opt: Opt) -> Result<()> { let course_names_path = opt.output.join("course_names.toml"); let course_names = if fs::metadata(&course_names_path).await.is_ok() { // file exists, try to read it - toml::from_str(&fs::read_to_string(course_names_path).await.context("accessing course_names.toml")?).context("processing course_names.toml")? + toml::from_str( + &fs::read_to_string(course_names_path) + .await + .context("accessing course_names.toml")?, + ) + .context("processing course_names.toml")? } else { // If file doesn't exist, initialise course_names with empty HashMap HashMap::new() }; - + if let Some(err) = error { warning!(err); } @@ -230,6 +235,9 @@ async fn process(ilias: Arc, path: PathBuf, obj: Object) -> Result<()> { } log!(1, "Syncing {} {}", obj.kind(), relative_path.to_string_lossy()); log!(2, " URL: {}", obj.url().url); + if obj.is_ignored_by_option(&ilias.opt) { + return Ok(()); + } if obj.is_dir() { create_dir(&path).await?; }