<commit_before>"use strict";

const ImageArchiver = require("./ImageArchiver");
const ImageResizer  = require("./ImageResizer");
const ImageReducer  = require("./ImageReducer");

class ImageProcessor {

    /**
     * Image processor
     * management resize/reduce image list by configration,
     * and pipe AWS Lambda's event/context
     *
     * @constructor
     * @param Object fileSystem
     * @param Object s3Object
     */
    constructor(fileSystem, s3Object) {
        this.fileSystem = fileSystem;
        this.s3Object = s3Object;
    }

    /**
     * Run the process
     *
     * @public
     * @param Config config
     */
    run(config) {
        if ( ! config.get("bucket") ) {
            config.set("bucket", this.s3Object.bucket.name);
        }

        return this.fileSystem.getObject(
            this.s3Object.bucket.name,
            decodeURIComponent(this.s3Object.object.key.replace(/\+/g, ' '))
        )
        .then((imageData) => this.processImage(imageData, config));
    }

    /**
     * Processing image
     *
     * @public
     * @param ImageData imageData
     * @param Config config
     * @return Promise
     */
    processImage(imageData, config) {
        const acl = config.get("acl");
        const bucket = config.get("bucket");
        const jpegOptimizer = config.get("jpegOptimizer", "mozjpeg");
        const removeAfterSuccess = ( config.get("backup", {}).move === true );

        let promise = Promise.resolve();
        let processedImages = 0;

        if ( config.exists("backup") ) {
            const backup = config.get("backup");
            backup.acl = backup.acl || acl;
            backup.bucket = backup.bucket || bucket;

            promise = promise
              .then(() => this.execBackupImage(backup, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => {
                console.log("backup success");
                ++processedImages;
                return Promise.resolve();
              })
        }

        if ( config.exists("reduce") ) {
            const reduce = config.get("reduce");
            reduce.acl = reduce.acl || acl;
            reduce.bucket = reduce.bucket || bucket;
            reduce.jpegOptimizer = reduce.jpegOptimizer || jpegOptimizer;

            promise = promise
              .then(() => this.execReduceImage(reduce, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => Promise.resolve(++processedImages));
        }

        config.get("resizes", []).filter((resize) => {
            return resize.size &&
                imageData.fileName.indexOf(resize.directory) !== 0; // don't process images in the output folder
        }).forEach((resize) => {
            resize.acl = resize.acl || acl;
            resize.bucket = resize.bucket || bucket;
            resize.jpegOptimizer = resize.jpegOptimizer || jpegOptimizer;

            promise = promise
              .then(() => this.execResizeImage(resize, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => Promise.resolve(++processedImages));
        });

        if ( removeAfterSuccess ) {
          promise = promise
            .then(() => this.fileSystem.deleteObject(imageData))
            .then(() => Promise.resolve(processedImages));
        }

        return promise;
    }
<commit_msg>Remove debug code, move works backup section<commit_after>"use strict";

const ImageArchiver = require("./ImageArchiver");
const ImageResizer  = require("./ImageResizer");
const ImageReducer  = require("./ImageReducer");

class ImageProcessor {

    /**
     * Image processor
     * management resize/reduce image list by configration,
     * and pipe AWS Lambda's event/context
     *
     * @constructor
     * @param Object fileSystem
     * @param Object s3Object
     */
    constructor(fileSystem, s3Object) {
        this.fileSystem = fileSystem;
        this.s3Object = s3Object;
    }

    /**
     * Run the process
     *
     * @public
     * @param Config config
     */
    run(config) {
        if ( ! config.get("bucket") ) {
            config.set("bucket", this.s3Object.bucket.name);
        }

        return this.fileSystem.getObject(
            this.s3Object.bucket.name,
            decodeURIComponent(this.s3Object.object.key.replace(/\+/g, ' '))
        )
        .then((imageData) => this.processImage(imageData, config));
    }

    /**
     * Processing image
     *
     * @public
     * @param ImageData imageData
     * @param Config config
     * @return Promise
     */
    processImage(imageData, config) {
        const acl = config.get("acl");
        const bucket = config.get("bucket");
        const jpegOptimizer = config.get("jpegOptimizer", "mozjpeg");

        let promise = Promise.resolve();
        let processedImages = 0;

        if ( config.exists("backup") ) {
            const backup = config.get("backup");
            backup.acl = backup.acl || acl;
            backup.bucket = backup.bucket || bucket;

              })

            promise = promise
              .then(() => this.execBackupImage(backup, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => ( backup.move === true ) ? this.fileSystem.deleteObject(imageData) : Promise.resolve())
              .then(() => Promise.resolve(++processedImages));
        }

        if ( config.exists("reduce") ) {
            const reduce = config.get("reduce");
            reduce.acl = reduce.acl || acl;
            reduce.bucket = reduce.bucket || bucket;
            reduce.jpegOptimizer = reduce.jpegOptimizer || jpegOptimizer;

            promise = promise
              .then(() => this.execReduceImage(reduce, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => Promise.resolve(++processedImages));
        }

        config.get("resizes", []).filter((resize) => {
            return resize.size &&
                imageData.fileName.indexOf(resize.directory) !== 0; // don't process images in the output folder
        }).forEach((resize) => {
            resize.acl = resize.acl || acl;
            resize.bucket = resize.bucket || bucket;
            resize.jpegOptimizer = resize.jpegOptimizer || jpegOptimizer;

            promise = promise
              .then(() => this.execResizeImage(resize, imageData))
              .then((image) => this.fileSystem.putObject(image))
              .then(() => Promise.resolve(++processedImages));
        });

        return promise;
    }

{'fraction_non_alphanumeric': 0.13630188679245284, 'fraction_numerical': 0.002716981132075472, 'mean_word_length': 1.97931654676259, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3788264', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1774, 'n_words': 356}