Skip to content

Commit

Permalink
Allow conversion and keeping dates #70
Browse files Browse the repository at this point in the history
  • Loading branch information
Lymphatus committed Jul 20, 2024
1 parent 444faa3 commit f76e8d8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "caesiumclt"
version = "0.19.3"
version = "0.20.0"
authors = ["Matteo Paonessa <[email protected]>"]
edition = "2021"

Expand All @@ -10,8 +10,9 @@ structopt = "0.3"
indicatif = "0.17"
walkdir = "2.5"
num_cpus = "1.16"
infer = "0.15"
infer = "0.16"
rayon = "1.10"
rand = "0.8"
human_bytes = { version = "0.4", default-features = false }
libcaesium = { git = "https://github.com/Lymphatus/libcaesium", tag = "0.15.4" }
filetime = "0.2"
libcaesium = { git = "https://github.com/Lymphatus/libcaesium", rev = "0.16.1" }
72 changes: 64 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fs;
use std::path::Path;
use std::sync::{Arc, Mutex};

use caesium::SupportedFileTypes;
use filetime::{FileTime, set_file_times};
use human_bytes::human_bytes;
use indicatif::ProgressBar;
use indicatif::ProgressDrawTarget;
Expand All @@ -27,12 +28,21 @@ struct CompressionResult {
pub result: bool,
}

struct OutputFormat {
pub file_type: SupportedFileTypes,
pub extension: String
}

fn main() {
let opt = options::get_opts();
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;

if opt.quiet {
verbose = 0;
}
Expand Down Expand Up @@ -86,12 +96,13 @@ fn main() {

let results = Arc::new(Mutex::new(Vec::new()));
files.par_iter().for_each(|input_file| {
let input_size = match fs::metadata(input_file) {
Ok(s) => s.len(),
let input_file_metadata = fs::metadata(input_file);
let (input_size, input_mtime, input_atime) = match input_file_metadata {
Ok(s) => (s.len(), FileTime::from_last_modification_time(&s), FileTime::from_last_access_time(&s)),
Err(e) => {
let error_message = format!("Cannot get file size for {}, Error: {}", input_file.display(), e);
log(error_message.as_str(), 202, Warning, verbose);
0
(0, FileTime::now(), FileTime::now())
}
};

Expand Down Expand Up @@ -130,7 +141,11 @@ fn main() {
.map(char::from)
.collect();
let random_suffixed_name = format!("{}.{}", filename_str, random_suffix);
let final_output_full_path = output_dir.clone().join(filename);
let mut final_output_full_path = output_dir.clone().join(filename);
if convert {
final_output_full_path.set_extension(output_format.extension.clone());
}

let output_full_path = output_dir.clone().join(random_suffixed_name);
let output_full_dir = output_full_path.parent().unwrap_or_else(|| Path::new("/"));
compression_result.output_path = final_output_full_path.display().to_string();
Expand All @@ -156,7 +171,13 @@ fn main() {
Some(ofp) => ofp
};
if !dry_run {
match caesium::compress(input_full_path.to_string(), output_full_path_str.to_string(), &compression_parameters) {
let result = if convert {
caesium::convert(input_full_path.to_string(), output_full_path_str.to_string(), &compression_parameters, output_format.file_type)
} else {
caesium::compress(input_full_path.to_string(), output_full_path_str.to_string(), &compression_parameters)
};

match result {
Ok(_) => {
compression_result.result = true;
let output_metadata = fs::metadata(output_full_path.clone());
Expand All @@ -183,7 +204,7 @@ fn main() {
};
final_output_size = existing_file_size;
} else {
match fs::rename(output_full_path, final_output_full_path) {
match fs::rename(output_full_path, final_output_full_path.clone()) {
Ok(_) => {}
Err(e) => {
compression_result.error = format!("Cannot rename existing file. Error: {}.", e);
Expand All @@ -192,7 +213,7 @@ fn main() {
};
}
} else {
match fs::rename(output_full_path, final_output_full_path) {
match fs::rename(output_full_path, final_output_full_path.clone()) {
Ok(_) => {}
Err(e) => {
compression_result.error = format!("Cannot rename existing file. Error: {}.", e);
Expand All @@ -201,6 +222,16 @@ fn main() {
};
}
compression_result.compressed_size = final_output_size;
if compression_result.result && keep_dates {


match set_file_times(final_output_full_path, input_atime, input_mtime) {
Ok(_) => {}
Err(_) => {
compression_result.error = "Cannot set original file dates.".into();
}
}
}
results.lock().unwrap().push(compression_result);
}
Err(e) => {
Expand Down Expand Up @@ -273,3 +304,28 @@ fn setup_progress_bar(len: u64, verbose: u8) -> ProgressBar {

progress_bar
}

fn map_output_format(format: String) -> OutputFormat {
match format.to_lowercase().as_str() {
"jpg|jpeg" => OutputFormat {
file_type: SupportedFileTypes::Jpeg,
extension: format
},
"png" => OutputFormat {
file_type: SupportedFileTypes::Png,
extension: format
},
"webp" => OutputFormat {
file_type: SupportedFileTypes::WebP,
extension: format
},
"tiff|tif" => OutputFormat {
file_type: SupportedFileTypes::Tiff,
extension: format
},
_ =>OutputFormat {
file_type: SupportedFileTypes::Unkn,
extension: "".to_string()
},
}
}
13 changes: 10 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;

use structopt::clap::arg_enum;
use structopt::StructOpt;

Expand All @@ -16,7 +15,7 @@ arg_enum! {
}


#[derive(StructOpt, Debug)]
#[derive(StructOpt)]
#[structopt(name = "", about = "CaesiumCLT - Command Line Tools for image compression")]
pub struct Opt {
/// sets output file quality between [0-100], 0 for optimization
Expand Down Expand Up @@ -52,7 +51,7 @@ pub struct Opt {
pub overwrite: OverwritePolicy,

/// do not compress files but just show output paths
#[structopt(short = "d", long)]
#[structopt(long = "dry-run", short = "d", long)]
pub dry_run: bool,

/// suppress all output
Expand All @@ -71,6 +70,14 @@ pub struct Opt {
#[structopt(long, default_value = "1")]
pub verbose: u8,

/// convert the image to the selected format (jpg, png, webp, tiff)
#[structopt(long = "output-format", default_value = "none")]
pub output_format: String,

/// keep original file date information
#[structopt(long = "keep-dates")]
pub keep_dates: bool,

/// Files to process
#[structopt(name = "FILE", parse(from_os_str))]
pub files: Vec<PathBuf>,
Expand Down

0 comments on commit f76e8d8

Please sign in to comment.