"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImageBase64 = void 0;
const express_validator_1 = require("express-validator");
const axios_1 = __importDefault(require("axios"));
const getImageBase64 = async (req, res, next) => {
    try {
        const { urls } = req.body;
        const errors = (0, express_validator_1.validationResult)(req);
        if (!errors.isEmpty()) {
            const error = new Error(errors.array()[0].msg);
            error.statusCode = 422;
            throw error;
        }
        if (!Array.isArray(urls)) {
            const error = new Error('O campo urls espera um array!');
            error.statusCode = 422;
            throw error;
        }
        let images = [];
        for (const url of urls) {
            const response = await axios_1.default.get(url, {
                responseType: 'arraybuffer', // importante!
            });
            const base64 = Buffer.from(response.data, 'binary').toString('base64');
            const contentType = response.headers['content-type'] || 'application/octet-stream';
            // Retorna no formato padrão base64 de arquivos:
            const imageBase64 = `data:${contentType};base64,${base64}`;
            images.push(imageBase64);
        }
        res.status(200).json({ msg: 'Sucesso', images });
    }
    catch (err) {
        next(err);
    }
};
exports.getImageBase64 = getImageBase64;
