From b6a238c67aa626aa7547b68b79a6e4d1b3879aac Mon Sep 17 00:00:00 2001 From: LuckyTurtleDev Date: Mon, 25 Sep 2023 22:28:22 +0200 Subject: [PATCH] skip non png files, if `--recursive` is used (#548) Fix #547 --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 79568f01..a83e4464 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use oxipng::RowFilter; use oxipng::StripChunks; use oxipng::{InFile, OutFile}; use rayon::prelude::*; +use std::ffi::OsString; use std::fs::DirBuilder; use std::io::Write; #[cfg(feature = "zopfli")] @@ -64,7 +65,7 @@ fn main() { ) .arg( Arg::new("recursive") - .help("Recurse into subdirectories") + .help("Recurse into subdirectories and optimize all *.png/*.apng files") .short('r') .long("recursive") .action(ArgAction::SetTrue), @@ -353,10 +354,10 @@ fn collect_files( out_dir: &Option, out_file: &OutFile, recursive: bool, - allow_stdin: bool, + top_level: bool, //explicitly specify files ) -> Vec<(InFile, OutFile)> { let mut in_out_pairs = Vec::new(); - let allow_stdin = allow_stdin && files.len() == 1; + let allow_stdin = top_level && files.len() == 1; for input in files { let using_stdin = allow_stdin && input.to_str().map_or(false, |p| p == "-"); if !using_stdin && input.is_dir() { @@ -389,6 +390,14 @@ fn collect_files( let in_file = if using_stdin { InFile::StdIn } else { + // Skip non png files if not given on top level + if !top_level && { + let extension = input.extension().map(|f| f.to_ascii_lowercase()); + extension != Some(OsString::from("png")) + && extension != Some(OsString::from("apng")) + } { + continue; + } InFile::Path(input) }; in_out_pairs.push((in_file, out_file));