"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isVideoFilterValid = exports.isVideoSupportValid = exports.isVideoImageValid = exports.isVideoFileSizeValid = exports.isVideoFileResolutionValid = exports.isVideoPrivacyValid = exports.isVideoTagValid = exports.isVideoDurationValid = exports.isVideoFileMimeTypeValid = exports.isVideoFileExtnameValid = exports.isVideoRatingTypeValid = exports.isVideoViewsValid = exports.isVideoIncludeValid = exports.isVideoStateValid = exports.isVideoMagnetUriValid = exports.isVideoOriginallyPublishedAtValid = exports.isScheduleVideoUpdatePrivacyValid = exports.isVideoFPSResolutionValid = exports.areVideoTagsValid = exports.isVideoNameValid = exports.isVideoFileInfoHashValid = exports.isVideoDescriptionValid = exports.isVideoLanguageValid = exports.isVideoLicenceValid = exports.isVideoCategoryValid = void 0;
const tslib_1 = require("tslib");
const magnet_uri_1 = tslib_1.__importDefault(require("magnet-uri"));
const validator_1 = tslib_1.__importDefault(require("validator"));
const constants_1 = require("../../initializers/constants");
const misc_1 = require("./misc");
const VIDEOS_CONSTRAINTS_FIELDS = constants_1.CONSTRAINTS_FIELDS.VIDEOS;
function isVideoFilterValid(filter) {
    return filter === 'local' || filter === 'all-local' || filter === 'all';
}
exports.isVideoFilterValid = isVideoFilterValid;
function isVideoIncludeValid(include) {
    return (0, misc_1.exists)(include) && validator_1.default.isInt('' + include);
}
exports.isVideoIncludeValid = isVideoIncludeValid;
function isVideoCategoryValid(value) {
    return value === null || constants_1.VIDEO_CATEGORIES[value] !== undefined;
}
exports.isVideoCategoryValid = isVideoCategoryValid;
function isVideoStateValid(value) {
    return (0, misc_1.exists)(value) && constants_1.VIDEO_STATES[value] !== undefined;
}
exports.isVideoStateValid = isVideoStateValid;
function isVideoLicenceValid(value) {
    return value === null || constants_1.VIDEO_LICENCES[value] !== undefined;
}
exports.isVideoLicenceValid = isVideoLicenceValid;
function isVideoLanguageValid(value) {
    return value === null ||
        (typeof value === 'string' && validator_1.default.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE));
}
exports.isVideoLanguageValid = isVideoLanguageValid;
function isVideoDurationValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION);
}
exports.isVideoDurationValid = isVideoDurationValid;
function isVideoDescriptionValid(value) {
    return value === null || ((0, misc_1.exists)(value) && validator_1.default.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION));
}
exports.isVideoDescriptionValid = isVideoDescriptionValid;
function isVideoSupportValid(value) {
    return value === null || ((0, misc_1.exists)(value) && validator_1.default.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT));
}
exports.isVideoSupportValid = isVideoSupportValid;
function isVideoNameValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME);
}
exports.isVideoNameValid = isVideoNameValid;
function isVideoTagValid(tag) {
    return (0, misc_1.exists)(tag) && validator_1.default.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG);
}
exports.isVideoTagValid = isVideoTagValid;
function areVideoTagsValid(tags) {
    return tags === null || ((0, misc_1.isArray)(tags) &&
        validator_1.default.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
        tags.every(tag => isVideoTagValid(tag)));
}
exports.areVideoTagsValid = areVideoTagsValid;
function isVideoViewsValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS);
}
exports.isVideoViewsValid = isVideoViewsValid;
const ratingTypes = new Set(Object.values(constants_1.VIDEO_RATE_TYPES));
function isVideoRatingTypeValid(value) {
    return value === 'none' || ratingTypes.has(value);
}
exports.isVideoRatingTypeValid = isVideoRatingTypeValid;
function isVideoFileExtnameValid(value) {
    return (0, misc_1.exists)(value) && (value === constants_1.VIDEO_LIVE.EXTENSION || constants_1.MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined);
}
exports.isVideoFileExtnameValid = isVideoFileExtnameValid;
function isVideoFileMimeTypeValid(files, field = 'videofile') {
    return (0, misc_1.isFileValid)({
        files,
        mimeTypeRegex: constants_1.MIMETYPES.VIDEO.MIMETYPES_REGEX,
        field,
        maxSize: null
    });
}
exports.isVideoFileMimeTypeValid = isVideoFileMimeTypeValid;
const videoImageTypes = constants_1.CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
    .map(v => v.replace('.', ''))
    .join('|');
const videoImageTypesRegex = `image/(${videoImageTypes})`;
function isVideoImageValid(files, field, optional = true) {
    return (0, misc_1.isFileValid)({
        files,
        mimeTypeRegex: videoImageTypesRegex,
        field,
        maxSize: constants_1.CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max,
        optional
    });
}
exports.isVideoImageValid = isVideoImageValid;
function isVideoPrivacyValid(value) {
    return constants_1.VIDEO_PRIVACIES[value] !== undefined;
}
exports.isVideoPrivacyValid = isVideoPrivacyValid;
function isScheduleVideoUpdatePrivacyValid(value) {
    return value === 2 || value === 1 || value === 4;
}
exports.isScheduleVideoUpdatePrivacyValid = isScheduleVideoUpdatePrivacyValid;
function isVideoOriginallyPublishedAtValid(value) {
    return value === null || (0, misc_1.isDateValid)(value);
}
exports.isVideoOriginallyPublishedAtValid = isVideoOriginallyPublishedAtValid;
function isVideoFileInfoHashValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH);
}
exports.isVideoFileInfoHashValid = isVideoFileInfoHashValid;
function isVideoFileResolutionValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isInt(value + '');
}
exports.isVideoFileResolutionValid = isVideoFileResolutionValid;
function isVideoFPSResolutionValid(value) {
    return value === null || validator_1.default.isInt(value + '');
}
exports.isVideoFPSResolutionValid = isVideoFPSResolutionValid;
function isVideoFileSizeValid(value) {
    return (0, misc_1.exists)(value) && validator_1.default.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE);
}
exports.isVideoFileSizeValid = isVideoFileSizeValid;
function isVideoMagnetUriValid(value) {
    if (!(0, misc_1.exists)(value))
        return false;
    const parsed = magnet_uri_1.default.decode(value);
    return parsed && isVideoFileInfoHashValid(parsed.infoHash);
}
exports.isVideoMagnetUriValid = isVideoMagnetUriValid;
//# sourceMappingURL=videos.js.map