]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-captions.ts
Try to fix ARM docker builds
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
CommitLineData
474542d7
C
1import { getFileSize } from '@shared/extra-utils'
2import { readFile } from 'fs-extra'
74dc3bca 3import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
40e87e9e 4import { exists, isFileValid } from './misc'
40e87e9e
C
5
6function isVideoCaptionLanguageValid (value: any) {
a1587156 7 return exists(value) && VIDEO_LANGUAGES[value] !== undefined
40e87e9e
C
8}
9
edaf5b86
C
10const videoCaptionTypesRegex = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
11 .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
2769e297 12 .map(m => `(${m})`)
edaf5b86 13 .join('|')
40e87e9e 14function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
f4001cf4 15 return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
40e87e9e
C
16}
17
474542d7
C
18async function isVTTFileValid (filePath: string) {
19 const size = await getFileSize(filePath)
20
21 if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
22
23 const content = await readFile(filePath, 'utf8')
24
25 return content?.startsWith('WEBVTT\n')
26}
27
40e87e9e
C
28// ---------------------------------------------------------------------------
29
30export {
31 isVideoCaptionFile,
474542d7 32 isVTTFileValid,
3e753302 33 isVideoCaptionLanguageValid
40e87e9e 34}