From 474542d7ac60f7860daf9ea34d1c31968f43ab29 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Feb 2022 08:58:40 +0100 Subject: Add additional checks when importing a video --- server/helpers/custom-validators/video-captions.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts index 528edf60c..4cc7dcaf4 100644 --- a/server/helpers/custom-validators/video-captions.ts +++ b/server/helpers/custom-validators/video-captions.ts @@ -1,3 +1,5 @@ +import { getFileSize } from '@shared/extra-utils' +import { readFile } from 'fs-extra' import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' import { exists, isFileValid } from './misc' @@ -13,9 +15,20 @@ function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) } +async function isVTTFileValid (filePath: string) { + const size = await getFileSize(filePath) + + if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false + + const content = await readFile(filePath, 'utf8') + + return content?.startsWith('WEBVTT\n') +} + // --------------------------------------------------------------------------- export { isVideoCaptionFile, + isVTTFileValid, isVideoCaptionLanguageValid } -- cgit v1.2.3