<commit_before>'use strict';

const sharp = require('sharp');
const cdn = require('./cdn');
const rp = require('request');
const Promise = require('bluebird');
const exceptions = require('./error/exceptions');
const logger = require('./logging').getLogger(__filename);

const getResizeOptions = function (width, height, fit) {
    let resizeOptions = {
        background: {r: 255, g: 255, b: 255, alpha: 0},
        withoutEnlargement: true,
        fit: sharp.fit.cover
    };

    if (typeof width === 'number') {
        resizeOptions.width = width;
    }
    if (typeof height === 'number') {
        resizeOptions.height = height;
    }
    if (typeof fit === 'string') {
        resizeOptions.fit = fit;
    }
    return resizeOptions;
};

const uploadImage = async function (width, height, options) {
    let imageBuffer = await sharp(options.originalImagePath).resize(getResizeOptions(width, height, options.fit))
        .flatten().jpeg({quality: options.quality}).toBuffer();
    await cdn.uploadBuffer(imageBuffer, options.cdnPath, options.bucket);
};

const uploadingExternalImage = async function (width, height, uri, options) {
    try {
        return await new Promise(function (resolve, reject) {
            if (uri) {
                rp.get(uri, function (err) {
                    if (err) {
                        reject(err);
                    }
                }).pipe(sharp().resize(getResizeOptions(width, height)).flatten().jpeg({quality: options.quality})
                    .toBuffer(async function (err, buffer) {
                        if (err) {
                            reject(err);
                        }
                        await cdn.uploadBuffer(buffer, options.cdnPath, options.bucket);
                        resolve(buffer);
                    }));
            } else {
                resolve();
            }
        });
    } catch (error) {
        logger.error(`Failed to get image ${uri}`, error);
    }
};

const ERROR_CODE_IMAGE_TO_SMALL = 1;
<commit_msg>Bugfix: Setting background color to white for transparent png.<commit_after>'use strict';

const sharp = require('sharp');
const cdn = require('./cdn');
const rp = require('request');
const Promise = require('bluebird');
const exceptions = require('./error/exceptions');
const logger = require('./logging').getLogger(__filename);

const getResizeOptions = function (width, height, fit) {
    let resizeOptions = {
        background: {r: 255, g: 255, b: 255, alpha: 0},
        withoutEnlargement: true,
        fit: sharp.fit.cover
    };

    if (typeof width === 'number') {
        resizeOptions.width = width;
    }
    if (typeof height === 'number') {
        resizeOptions.height = height;
    }
    if (typeof fit === 'string') {
        resizeOptions.fit = fit;
    }
    return resizeOptions;
};

const uploadImage = async function (width, height, options) {
    let imageBuffer = await sharp(options.originalImagePath)
        .flatten({background: {r: 255, g: 255, b: 255}})
        .resize(getResizeOptions(width, height, options.fit))
        .jpeg({quality: options.quality}).toBuffer();
    await cdn.uploadBuffer(imageBuffer, options.cdnPath, options.bucket);
};

const uploadingExternalImage = async function (width, height, uri, options) {
    try {
        return await new Promise(function (resolve, reject) {
            if (uri) {
                rp.get(uri, function (err) {
                    if (err) {
                        reject(err);
                    }
                }).pipe(sharp().flatten({background: {r: 255, g: 255, b: 255}})
                    .resize(getResizeOptions(width, height)).jpeg({quality: options.quality})
                    .toBuffer(async function (err, buffer) {
                        if (err) {
                            reject(err);
                        }
                        await cdn.uploadBuffer(buffer, options.cdnPath, options.bucket);
                        resolve(buffer);
                    }));
            } else {
                resolve();
            }
        });
    } catch (error) {
        logger.error(`Failed to get image ${uri}`, error);
    }
};

const ERROR_CODE_IMAGE_TO_SMALL = 1;

{'fraction_non_alphanumeric': 0.131779257849667, 'fraction_numerical': 0.009514747859181731, 'mean_word_length': 2.0100214745884037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2341029', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1101, 'n_words': 285}