"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoFilesDeleteHLSFileValidator = exports.videoFilesDeleteHLSValidator = exports.videoFilesDeleteWebTorrentFileValidator = exports.videoFilesDeleteWebTorrentValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const misc_1 = require("../../../helpers/custom-validators/misc");
const shared_1 = require("../shared");
const videoFilesDeleteWebTorrentValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoExist)(req.params.id, res)))
            return;
        const video = res.locals.videoAll;
        if (!checkLocalVideo(video, res))
            return;
        if (!video.hasWebTorrentFiles()) {
            return res.fail({
                status: 400,
                message: 'This video does not have WebTorrent files'
            });
        }
        if (!video.getHLSPlaylist()) {
            return res.fail({
                status: 400,
                message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
            });
        }
        return next();
    })
];
exports.videoFilesDeleteWebTorrentValidator = videoFilesDeleteWebTorrentValidator;
const videoFilesDeleteWebTorrentFileValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (0, express_validator_1.param)('videoFileId')
        .custom(misc_1.isIdValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoExist)(req.params.id, res)))
            return;
        const video = res.locals.videoAll;
        if (!checkLocalVideo(video, res))
            return;
        const files = video.VideoFiles;
        if (!files.find(f => f.id === +req.params.videoFileId)) {
            return res.fail({
                status: 404,
                message: 'This video does not have this WebTorrent file id'
            });
        }
        if (files.length === 1 && !video.getHLSPlaylist()) {
            return res.fail({
                status: 400,
                message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
            });
        }
        return next();
    })
];
exports.videoFilesDeleteWebTorrentFileValidator = videoFilesDeleteWebTorrentFileValidator;
const videoFilesDeleteHLSValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoExist)(req.params.id, res)))
            return;
        const video = res.locals.videoAll;
        if (!checkLocalVideo(video, res))
            return;
        if (!video.getHLSPlaylist()) {
            return res.fail({
                status: 400,
                message: 'This video does not have HLS files'
            });
        }
        if (!video.hasWebTorrentFiles()) {
            return res.fail({
                status: 400,
                message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
            });
        }
        return next();
    })
];
exports.videoFilesDeleteHLSValidator = videoFilesDeleteHLSValidator;
const videoFilesDeleteHLSFileValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (0, express_validator_1.param)('videoFileId')
        .custom(misc_1.isIdValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoExist)(req.params.id, res)))
            return;
        const video = res.locals.videoAll;
        if (!checkLocalVideo(video, res))
            return;
        if (!video.getHLSPlaylist()) {
            return res.fail({
                status: 400,
                message: 'This video does not have HLS files'
            });
        }
        const hlsFiles = video.getHLSPlaylist().VideoFiles;
        if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) {
            return res.fail({
                status: 404,
                message: 'This HLS playlist does not have this file id'
            });
        }
        if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) {
            return res.fail({
                status: 400,
                message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files'
            });
        }
        return next();
    })
];
exports.videoFilesDeleteHLSFileValidator = videoFilesDeleteHLSFileValidator;
function checkLocalVideo(video, res) {
    if (video.remote) {
        res.fail({
            status: 400,
            message: 'Cannot delete files of remote video'
        });
        return false;
    }
    return true;
}
//# sourceMappingURL=video-files.js.map