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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use {
    crate::{
        html5::Picture,
        path,
        utils,
        webp::processor::BatchParameter,
        webp::processor::Parameter as ProcessorParameter,
        webp::WebpParameter,
    },
    clap::{crate_authors, crate_version, Parser},
    fs_extra::dir::{
        copy_with_progress,
        move_dir_with_progress,
        CopyOptions,
        TransitProcess,
    },
    indicatif::MultiProgress,
    log::error,
    queue::Queue,
    std::{path::PathBuf, sync::Arc},
};

#[cfg(debug_assertions)]
use log::debug;

type Step = fn(&mut State);

/// Converts the images (currently png only) of the input folder to webp format.
/// It also has the ability to create multiple versions of the input images
/// having different sizes. See -s for further details.
/// Additionally it automatically generates HTML5 <picture> tag files for you
/// to be able to integrate them in a webpage easily.
///
/// Depends on cwebp, so make sure webp is installed on your pc!
///
/// Example:
/// html5-picture ./assets 3;
/// Input image dimensions: 6000x962;
/// Scaled images count: 3;
/// Resulting converted images:
///     original_filename        6000x962;
///     original_filename-w4500  4500x751;
///     original_filename-w3000  3000x501;
///     original_filename-w1500  1500x250;
#[derive(Parser, Debug, Clone)]
#[clap(
    version = crate_version!(),
    author = crate_authors!(", "),
)]
pub struct Config {
    /// The directory containing all images that should be processed.
    pub input_dir: PathBuf,
    /// The source image width is divided by this option (value + 1). Afterwards
    /// the source image is scaled (keeping the aspect ratio) to these widths
    /// before convertion.
    /// Useful if you want to have multiple sizes of the image on the webpage
    /// for different breakpoints.
    pub scaled_images_count: u8,
    /// Installs the converted and sized pictures into the given folder.
    #[clap(short)]
    pub install_images_into: Option<PathBuf>,
    /// The destination folder of HTML5 picture tag files.
    #[clap(short)]
    pub picture_tags_output_folder: Option<PathBuf>,
    /// Can be used in combination with -p, sets the mountpoint for links in
    /// the HTML tags.
    #[clap(short)]
    pub mountpoint: Option<PathBuf>,
    /// If true, existing files are overwritten if install-images-into is set.
    #[clap(short, long)]
    pub force_overwrite: bool,
    /// Defines the quality of cwebp conversion.
    #[clap(short)]
    pub quality_webp: Option<u8>,
    /// If set, the processing is done single threaded.
    #[clap(short)]
    pub single_threaded: bool,
}

/// Contains the application state and config.
pub struct State {
    pub config: Config,
    pub file_names_to_convert: Vec<PathBuf>,
    pub current_step: usize,
    pub max_progress_steps: usize,
}

impl State {
    /// Creates a new instance of the application state.
    pub fn new(config: Config, max_progress_steps: usize) -> Self {
        Self {
            config,
            file_names_to_convert: vec![],
            current_step: 0,
            max_progress_steps,
        }
    }

    /// Small wrapper around the original dequeue function that automatically
    /// calculates the current application step.
    pub fn dequeue(&mut self, queue: &mut Queue<Step>) -> Option<Step> {
        self.current_step = self.max_progress_steps + 1 - queue.len();
        queue.dequeue()
    }

    /// Returns the prefix that is used in the ProgressBars.
    pub fn get_prefix(&self) -> String {
        format!("{}/{}", self.current_step, self.max_progress_steps)
    }
}

/// Collects all png files in the given input folder.
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(),
    ));
}

/// Recreates the folder structure of the input directory in the output directory.
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!");
}

/// Copies the input folder to the working directory.
pub fn copy_originals_to_output(state: &mut State) {
    let pb = utils::create_progressbar(0);
    let pb_clone = pb.clone();
    let force_overwrite = state.config.force_overwrite;
    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_prefix(&state.get_prefix());
    pb.set_message("Copying original files...");
    let mut copy_options = CopyOptions::new();
    copy_options.content_only = true;
    if let Err(msg) = copy_with_progress(
        &state.config.input_dir,
        path::get_output_working_dir(&state.config.input_dir).unwrap(),
        &copy_options,
        progress_handler,
    ) {
        error!("{}", msg.to_string());
    }
    pb.finish_with_message("Successfully copied original images!");
}

/// Resizes and converts all input images.
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,
        single_threaded: state.config.single_threaded,
    };
    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 :-)");
}

/// Installs all images that have been converted to the given install folder.
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(),
        &copy_options,
        progress_handler,
    ) {
        error!("{}", msg.to_string());
    }
    pb.finish_with_message(&format!(
        "Successfully installed images to {}!",
        state.config.install_images_into.as_ref().unwrap().display()
    ));
}

/// Saves the html `<picture>` tags to the folder given by the options.
pub fn save_html_picture_tags(state: &mut State) {
    let pb =
        utils::create_progressbar(state.file_names_to_convert.len() as u64);
    pb.set_prefix(&state.get_prefix());
    pb.set_message("Writing HTML picture tag files...");

    if let None = &state.config.picture_tags_output_folder {
        pb.abandon_with_message(
            "Parameter picture_tags_output_folder not set!",
        );
        return;
    }

    for file_name in &state.file_names_to_convert {
        use std::io::prelude::*;
        let mut output_name = file_name.clone();
        output_name.set_extension("html");
        let output_tag_file_name =
            match crate::path::create_output_file_name_with_output_dir(
                &state.config.picture_tags_output_folder.as_ref().unwrap(),
                &state.config.input_dir,
                &output_name,
            ) {
                Ok(name) => name,
                Err(msg) => {
                    pb.abandon_with_message(&format!("{}", msg.to_string()));
                    return;
                }
            };

        #[cfg(debug_assertions)]
        debug!("{:#?}", output_tag_file_name);

        if std::path::Path::new(&output_tag_file_name).exists()
            && !state.config.force_overwrite
        {
            #[cfg(debug_assertions)]
            debug!("Skipping file {:#?}", output_tag_file_name);
            continue;
        }

        let parent_folder = match output_tag_file_name.parent() {
            Some(p) => p,
            None => {
                pb.abandon_with_message(&format!(
                    "No parent folder available for {}",
                    output_tag_file_name.display()
                ));
                return;
            }
        };
        let is_folder = match std::fs::metadata(parent_folder) {
            Ok(v) => v.is_dir(),
            Err(_) => false,
        };
        if !is_folder {
            if let Err(msg) = std::fs::create_dir_all(parent_folder) {
                error!(
                    "Parent folder could not be created: {}",
                    msg.to_string()
                );
                return;
            }
        }

        let mut pic =
            Picture::from(&file_name, state.config.scaled_images_count)
                .unwrap();

        if let Some(mountpoint) = &state.config.mountpoint {
            for source in &mut pic.sources {
                source.srcset =
                    match crate::path::create_output_file_name_with_output_dir(
                        &mountpoint,
                        &state.config.input_dir,
                        &PathBuf::from(&source.srcset),
                    ) {
                        Ok(name) => String::from(name.to_str().unwrap()),
                        Err(msg) => {
                            pb.abandon_with_message(&format!(
                                "{}",
                                msg.to_string()
                            ));
                            return;
                        }
                    };
            }
            pic.fallback_uri =
                match crate::path::create_output_file_name_with_output_dir(
                    &mountpoint,
                    &state.config.input_dir,
                    &PathBuf::from(&pic.fallback_uri),
                ) {
                    Ok(name) => String::from(name.to_str().unwrap()),
                    Err(msg) => {
                        pb.abandon_with_message(&format!(
                            "{}",
                            msg.to_string()
                        ));
                        return;
                    }
                };
        }

        let mut html_file = match std::fs::File::create(output_tag_file_name) {
            Ok(f) => f,
            Err(msg) => {
                error!("{}", msg.to_string());
                return;
            }
        };
        if let Err(msg) =
            html_file.write_all(pic.to_html_string(None, "").as_bytes())
        {
            error!("{}", msg.to_string());
        };
        pb.inc(1);
    }
    pb.finish_with_message(&format!(
        "Successfully wrote HTML picture tag files to: {}",
        &state
            .config
            .picture_tags_output_folder
            .as_ref()
            .unwrap()
            .display()
    ));
}