1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use {
crate::{
path,
utils,
webp::processor::BatchParameter,
webp::processor::Parameter as ProcessorParameter,
webp::WebpParameter,
},
clap::{crate_authors, crate_version, Clap},
fs_extra::dir::{move_dir_with_progress, CopyOptions, TransitProcess},
indicatif::MultiProgress,
log::error,
queue::Queue,
std::{path::PathBuf, sync::Arc},
};
type Step = fn(&mut State);
#[derive(Clap, Debug)]
#[clap(
version = crate_version!(),
author = crate_authors!(", ")
)]
pub struct Config {
pub input_dir: PathBuf,
#[clap(short)]
pub scaled_images_count: Option<u8>,
#[clap(short)]
pub install_images_into: Option<PathBuf>,
#[clap(short)]
pub force_overwrite: bool,
#[clap(short)]
pub quality_webp: Option<u8>,
}
pub struct State {
pub config: Config,
pub file_names_to_convert: Vec<PathBuf>,
pub current_step: usize,
pub max_progress_steps: usize,
}
impl State {
pub fn new(config: Config, max_progress_steps: usize) -> Self {
Self {
config,
file_names_to_convert: vec![],
current_step: 0,
max_progress_steps,
}
}
pub fn dequeue(&mut self, queue: &mut Queue<Step>) -> Option<Step> {
self.current_step = self.max_progress_steps + 1 - queue.len();
queue.dequeue()
}
pub fn get_prefix(&self) -> String {
format!("{}/{}", self.current_step, self.max_progress_steps)
}
}
pub fn collect_file_names(state: &mut State) {
let pb = utils::create_spinner();
pb.set_prefix(&state.get_prefix());
pb.set_message("Collecting files to convert...");
state.file_names_to_convert = crate::collect_png_file_names(
&state.config.input_dir,
Some(pb.clone()),
);
pb.finish_with_message(&format!(
"Collected {} files!",
&state.file_names_to_convert.len(),
));
}
pub fn create_all_output_directories(state: &mut State) {
let pb = utils::create_spinner();
pb.set_prefix(&state.get_prefix());
pb.set_message("Create all output directories...");
crate::fs::create_output_directories(
&state.config.input_dir,
&state.file_names_to_convert,
Some(pb.clone()),
);
pb.finish_with_message("Created all output directories!");
}
pub fn process_images(state: &mut State) {
let webp_params = WebpParameter::new(state.config.quality_webp);
let params = ProcessorParameter {
webp_parameter: webp_params,
input: state.config.input_dir.clone(),
output_dir: PathBuf::new(),
scaled_images_count: state.config.scaled_images_count,
};
let batch_params = BatchParameter {
single_params: params,
};
let mp = Arc::new(MultiProgress::new());
let batch_processor = crate::webp::processor::BatchProcessor::new(
batch_params,
Some(Arc::clone(&mp)),
);
let pb = utils::create_spinner();
pb.set_prefix(&state.get_prefix());
pb.set_message("Converting files...");
batch_processor.run(&state.file_names_to_convert);
pb.finish_with_message("Finished :-)");
}
pub fn install_images_into(state: &mut State) {
let pb = utils::create_progressbar(0);
match &state.config.install_images_into {
None => return,
Some(p) => {
if !p.is_dir() {
if let Err(msg) = std::fs::create_dir_all(p) {
pb.abandon_with_message(&format!(
"Could not create folder: {}",
msg.to_string()
));
}
}
}
}
pb.set_prefix(&state.get_prefix());
let install_path =
state.config.install_images_into.as_ref().unwrap().to_str();
let install_string = match install_path {
Some(s) => s,
None => {
pb.abandon_with_message("Invalid install_images_into parameter!");
return;
}
};
let force_overwrite = state.config.force_overwrite;
let pb_clone = pb.clone();
let progress_handler = move |process_info: TransitProcess| {
pb_clone.set_length(process_info.total_bytes);
pb_clone.set_position(process_info.copied_bytes);
if force_overwrite {
return fs_extra::dir::TransitProcessResult::Overwrite;
}
fs_extra::dir::TransitProcessResult::ContinueOrAbort
};
pb.set_message(&format!("Installing files to {}...", &install_string));
let mut copy_options = CopyOptions::new();
copy_options.content_only = true;
if let Err(msg) = move_dir_with_progress(
path::get_output_working_dir(&state.config.input_dir).unwrap(),
state.config.install_images_into.as_ref().unwrap(),
©_options,
progress_handler,
) {
error!("{}", msg.to_string());
}
pb.finish_with_message(&format!(
"Successfully installed images to {}!",
state
.config
.install_images_into
.as_ref()
.unwrap()
.to_str()
.unwrap()
));
}