]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-captions.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
CommitLineData
c729caf6 1import { UploadFilesForCheck } from 'express'
474542d7 2import { readFile } from 'fs-extra'
c729caf6 3import { getFileSize } from '@shared/extra-utils'
74dc3bca 4import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
c56dd280 5import { logger } from '../logger'
40e87e9e 6import { exists, isFileValid } from './misc'
40e87e9e
C
7
8function isVideoCaptionLanguageValid (value: any) {
a1587156 9 return exists(value) && VIDEO_LANGUAGES[value] !== undefined
40e87e9e
C
10}
11
60b880ac
C
12// MacOS sends application/octet-stream
13const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ]
14 .map(m => `(${m})`)
15 .join('|')
16
c729caf6
C
17function 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 })
40e87e9e
C
24}
25
474542d7
C
26async function isVTTFileValid (filePath: string) {
27 const size = await getFileSize(filePath)
c56dd280 28 const content = await readFile(filePath, 'utf8')
474542d7 29
c56dd280 30 logger.debug('Checking VTT file %s', filePath, { size, content })
474542d7 31
c56dd280 32 if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
474542d7 33
c56dd280 34 return content?.startsWith('WEBVTT')
474542d7
C
35}
36
40e87e9e
C
37// ---------------------------------------------------------------------------
38
39export {
40 isVideoCaptionFile,
474542d7 41 isVTTFileValid,
3e753302 42 isVideoCaptionLanguageValid
40e87e9e 43}