X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideo-captions.ts;h=0e24655a081a793864a6890c5375bd0cdecd6fed;hb=c56dd2807fe5d129907b9bf8c42656a8314d754b;hp=b33d90e1856f5bc3a77683f2c8d321ec4b763dfa;hpb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts index b33d90e18..0e24655a0 100644 --- a/server/helpers/custom-validators/video-captions.ts +++ b/server/helpers/custom-validators/video-captions.ts @@ -1,40 +1,43 @@ -import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers' +import { UploadFilesForCheck } from 'express' +import { readFile } from 'fs-extra' +import { getFileSize } from '@shared/extra-utils' +import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' +import { logger } from '../logger' import { exists, isFileValid } from './misc' -import { Response } from 'express' -import { VideoModel } from '../../models/video/video' -import { VideoCaptionModel } from '../../models/video/video-caption' function isVideoCaptionLanguageValid (value: any) { - return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined + return exists(value) && VIDEO_LANGUAGES[value] !== undefined } -const videoCaptionTypes = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT) - .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream >< - .map(m => `(${m})`) -const videoCaptionTypesRegex = videoCaptionTypes.join('|') -function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { - return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) +// MacOS sends application/octet-stream +const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ] + .map(m => `(${m})`) + .join('|') + +function isVideoCaptionFile (files: UploadFilesForCheck, field: string) { + return isFileValid({ + files, + mimeTypeRegex: videoCaptionTypesRegex, + field, + maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max + }) } -async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) { - const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) +async function isVTTFileValid (filePath: string) { + const size = await getFileSize(filePath) + const content = await readFile(filePath, 'utf8') - if (!videoCaption) { - res.status(404) - .json({ error: 'Video caption not found' }) - .end() + logger.debug('Checking VTT file %s', filePath, { size, content }) - return false - } + if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false - res.locals.videoCaption = videoCaption - return true + return content?.startsWith('WEBVTT') } // --------------------------------------------------------------------------- export { isVideoCaptionFile, - isVideoCaptionLanguageValid, - isVideoCaptionExist + isVTTFileValid, + isVideoCaptionLanguageValid }