"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isVideoCaptionLanguageValid = exports.isVTTFileValid = exports.isVideoCaptionFile = void 0;
const tslib_1 = require("tslib");
const extra_utils_1 = require("../../../shared/extra-utils");
const fs_extra_1 = require("fs-extra");
const constants_1 = require("../../initializers/constants");
const misc_1 = require("./misc");
function isVideoCaptionLanguageValid(value) {
    return (0, misc_1.exists)(value) && constants_1.VIDEO_LANGUAGES[value] !== undefined;
}
exports.isVideoCaptionLanguageValid = isVideoCaptionLanguageValid;
const videoCaptionTypesRegex = Object.keys(constants_1.MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
    .concat(['application/octet-stream'])
    .map(m => `(${m})`)
    .join('|');
function isVideoCaptionFile(files, field) {
    return (0, misc_1.isFileValid)(files, videoCaptionTypesRegex, field, constants_1.CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max);
}
exports.isVideoCaptionFile = isVideoCaptionFile;
function isVTTFileValid(filePath) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const size = yield (0, extra_utils_1.getFileSize)(filePath);
        if (size > constants_1.CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
            return false;
        const content = yield (0, fs_extra_1.readFile)(filePath, 'utf8');
        return content === null || content === void 0 ? void 0 : content.startsWith('WEBVTT\n');
    });
}
exports.isVTTFileValid = isVTTFileValid;
//# sourceMappingURL=video-captions.js.map