Same folder as input option #74

This commit is contained in:
Matteo Paonessa 2024-10-11 20:52:47 +02:00
parent 5d0b0b4906
commit e82e7296c7
5 changed files with 31 additions and 24 deletions

7
Cargo.lock generated
View File

@ -151,7 +151,7 @@ checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3"
[[package]]
name = "caesiumclt"
version = "0.21.0"
version = "0.22.0"
dependencies = [
"filetime",
"human_bytes",
@ -598,8 +598,9 @@ checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "libcaesium"
version = "0.16.5"
source = "git+https://github.com/Lymphatus/libcaesium?rev=0.16.5#5499d2ff9d051da54d085952180a2763168062c9"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76975eb2d706a0759e61fdabbbfce62ba97ae9785997e451e29a080fc67a098"
dependencies = [
"bytes",
"gifsicle",

View File

@ -1,6 +1,6 @@
[package]
name = "caesiumclt"
version = "0.21.0"
version = "0.22.0"
authors = ["Matteo Paonessa <matteo.paonessa@gmail.com>"]
edition = "2021"
@ -16,4 +16,4 @@ rand = "0.8"
human_bytes = { version = "0.4", default-features = false }
filetime = "0.2"
imagesize = "0.13"
libcaesium = { git = "https://github.com/Lymphatus/libcaesium", rev = "0.16.5" }
libcaesium = "0.17.0"

View File

@ -24,6 +24,7 @@ FLAGS:
-S, --keep-structure keep the folder structure, can be used only with -R
-Q, --quiet suppress all output
-R, --recursive if input is a folder, scan subfolders too
--same-folder-as-input sets the output folder to be the same as the input folder. Overwrites original files
-V, --version Prints version information
--zopfli use zopfli when optimizing PNG files (it may take a very long time to complete)

View File

@ -1,7 +1,7 @@
use std::fs;
use std::path::Path;
use std::sync::{Arc, Mutex};
use caesium::parameters::CSParameters;
use caesium::SupportedFileTypes;
use filetime::{FileTime, set_file_times};
use human_bytes::human_bytes;
@ -39,7 +39,6 @@ fn main() {
let mut verbose = opt.verbose;
let args = opt.files;
let dry_run = opt.dry_run;
let output_dir = opt.output;
let output_format = map_output_format(opt.output_format);
let convert = output_format.file_type != SupportedFileTypes::Unkn;
let keep_dates = opt.keep_dates;
@ -56,6 +55,14 @@ fn main() {
num_cpus::get()
};
rayon::ThreadPoolBuilder::new().num_threads(cpus).build_global().unwrap_or_default();
let (base_path, files) = scanfiles::scanfiles(args, opt.recursive);
let same_folder_as_input = opt.same_folder_as_input;
let output_dir = if same_folder_as_input {
base_path.clone()
} else {
opt.output.unwrap()
};
if dry_run {
log("Running in dry run mode", 0, Notice, verbose);
@ -66,9 +73,7 @@ fn main() {
}
}
let (base_path, files) = scanfiles::scanfiles(args, opt.recursive);
let mut compression_parameters = caesium::initialize_parameters();
let mut compression_parameters = CSParameters::new();
if opt.quality.is_some() {
let quality = opt.quality.unwrap_or(80);
@ -209,19 +214,15 @@ fn main() {
match result {
Ok(_) => {
compression_result.result = true;
let output_metadata = fs::metadata(output_full_path.clone());
let output_size = if let Ok(..) = output_metadata {
output_metadata.unwrap().len()
} else {
0
let output_size = match fs::metadata(output_full_path.clone()) {
Ok(s) => s.len(),
Err(_) => 0
};
let mut final_output_size = output_size;
if matches!(overwrite_policy, OverwritePolicy::Bigger) && final_output_full_path.exists() {
let existing_file_metadata = fs::metadata(final_output_full_path.clone());
let existing_file_size = if let Ok(..) = existing_file_metadata {
existing_file_metadata.unwrap().len()
} else {
0
let existing_file_size = match fs::metadata(final_output_full_path.clone()) {
Ok(s) => s.len(),
Err(_) => 0
};
if output_size >= existing_file_size {
match fs::remove_file(output_full_path) {

View File

@ -31,11 +31,11 @@ pub struct Opt {
pub exif: bool,
/// width of the output image, if height is not set will preserve aspect ratio
#[structopt(long, conflicts_with_all(&["height", "long-edge", "short-edge"]))]
#[structopt(long, conflicts_with_all(&["long-edge", "short-edge"]))]
pub width: Option<u32>,
/// height of the output image, if width is not set will preserve aspect ratio
#[structopt(long, conflicts_with_all(&["width", "long-edge", "short-edge"]))]
#[structopt(long, conflicts_with_all(&["long-edge", "short-edge"]))]
pub height: Option<u32>,
/// sets the size of the longest edge of the image
@ -47,8 +47,8 @@ pub struct Opt {
pub short_edge: Option<u32>,
/// output folder
#[structopt(short = "o", long, parse(from_os_str))]
pub output: PathBuf,
#[structopt(short = "o", long, conflicts_with="same-folder-as-input", parse(from_os_str))]
pub output: Option<PathBuf>,
/// if input is a folder, scan subfolders too
#[structopt(short = "R", long)]
@ -94,6 +94,10 @@ pub struct Opt {
#[structopt(long = "png-opt-level", default_value="3")]
pub png_opt_level: u8,
/// sets the output folder to be the same as the input folder. Overwrites original files
#[structopt(long = "same-folder-as-input", conflicts_with="output")]
pub same_folder_as_input: bool,
/// Files to process
#[structopt(name = "FILE", parse(from_os_str))]
pub files: Vec<PathBuf>,