"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadImage = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const image_utils_1 = require("../../../helpers/image-utils");
const requests_1 = require("../../../helpers/requests");
const config_1 = require("../../../initializers/config");
function downloadImage(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { url, destDir, destName, size } = options;
        const tmpPath = (0, path_1.join)(config_1.CONFIG.STORAGE.TMP_DIR, 'pending-' + destName);
        yield (0, requests_1.doRequestAndSaveToFile)(url, tmpPath);
        const destPath = (0, path_1.join)(destDir, destName);
        try {
            yield (0, image_utils_1.processImage)({ path: tmpPath, destination: destPath, newSize: size });
        }
        catch (err) {
            yield (0, fs_extra_1.remove)(tmpPath);
            throw err;
        }
    });
}
exports.downloadImage = downloadImage;
module.exports = downloadImage;
//# sourceMappingURL=image-downloader.js.map