]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-captions.ts
Fix peertube subtitles import
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
1 import { UploadFilesForCheck } from 'express'
2 import { readFile } from 'fs-extra'
3 import { getFileSize } from '@shared/extra-utils'
4 import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
5 import { logger } from '../logger'
6 import { exists, isFileValid } from './misc'
7
8 function isVideoCaptionLanguageValid (value: any) {
9 return exists(value) && VIDEO_LANGUAGES[value] !== undefined
10 }
11
12 // MacOS sends application/octet-stream
13 const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ]
14 .map(m => `(${m})`)
15 .join('|')
16
17 function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
18 return isFileValid({
19 files,
20 mimeTypeRegex: videoCaptionTypesRegex,
21 field,
22 maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
23 })
24 }
25
26 async function isVTTFileValid (filePath: string) {
27 const size = await getFileSize(filePath)
28 const content = await readFile(filePath, 'utf8')
29
30 logger.debug('Checking VTT file %s', filePath, { size, content })
31
32 if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
33
34 return content?.startsWith('WEBVTT')
35 }
36
37 // ---------------------------------------------------------------------------
38
39 export {
40 isVideoCaptionFile,
41 isVTTFileValid,
42 isVideoCaptionLanguageValid
43 }