"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoStudioAddEditionValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const misc_1 = require("../../../helpers/custom-validators/misc");
const video_studio_1 = require("../../../helpers/custom-validators/video-studio");
const express_utils_1 = require("../../../helpers/express-utils");
const config_1 = require("../../../initializers/config");
const video_studio_2 = require("../../../lib/video-studio");
const extra_utils_1 = require("../../../../shared/extra-utils");
const shared_1 = require("../shared");
const videoStudioAddEditionValidator = [
    (0, express_validator_1.param)('videoId')
        .custom(misc_1.isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'),
    (0, express_validator_1.body)('tasks')
        .custom(video_studio_1.isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if (config_1.CONFIG.VIDEO_STUDIO.ENABLED !== true) {
            res.fail({
                status: 400,
                message: 'Video studio is disabled on this instance'
            });
            return (0, express_utils_1.cleanUpReqFiles)(req);
        }
        if ((0, shared_1.areValidationErrors)(req, res))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        const body = req.body;
        const files = req.files;
        for (let i = 0; i < body.tasks.length; i++) {
            const task = body.tasks[i];
            if (!checkTask(req, task, i)) {
                res.fail({
                    status: 400,
                    message: `Task ${task.name} is invalid`
                });
                return (0, express_utils_1.cleanUpReqFiles)(req);
            }
            if (task.name === 'add-intro' || task.name === 'add-outro') {
                const filePath = (0, video_studio_2.getTaskFile)(files, i).path;
                if (yield (0, extra_utils_1.isAudioFile)(filePath)) {
                    res.fail({
                        status: 400,
                        message: `Task ${task.name} is invalid: file does not contain a video stream`
                    });
                    return (0, express_utils_1.cleanUpReqFiles)(req);
                }
            }
        }
        if (!(yield (0, shared_1.doesVideoExist)(req.params.videoId, res)))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        const video = res.locals.videoAll;
        if (video.state === 2 || video.state === 9) {
            res.fail({
                status: 409,
                message: 'Cannot edit video that is already waiting for transcoding/edition'
            });
            return (0, express_utils_1.cleanUpReqFiles)(req);
        }
        const user = res.locals.oauth.token.User;
        if (!(0, shared_1.checkUserCanManageVideo)(user, video, 17, res))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        const additionalBytes = yield (0, video_studio_2.approximateIntroOutroAdditionalSize)(video, body.tasks, i => (0, video_studio_2.getTaskFile)(files, i).path);
        if ((yield (0, shared_1.checkUserQuota)(user, additionalBytes, res)) === false)
            return (0, express_utils_1.cleanUpReqFiles)(req);
        return next();
    })
];
exports.videoStudioAddEditionValidator = videoStudioAddEditionValidator;
const taskCheckers = {
    'cut': video_studio_1.isStudioCutTaskValid,
    'add-intro': video_studio_1.isStudioTaskAddIntroOutroValid,
    'add-outro': video_studio_1.isStudioTaskAddIntroOutroValid,
    'add-watermark': video_studio_1.isStudioTaskAddWatermarkValid
};
function checkTask(req, task, indice) {
    const checker = taskCheckers[task.name];
    if (!checker)
        return false;
    return checker(task, indice, req.files);
}
//# sourceMappingURL=video-studio.js.map