]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-captions.ts
External auth can set more user fields
[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'
40e87e9e 5import { exists, isFileValid } from './misc'
40e87e9e
C
6
7function isVideoCaptionLanguageValid (value: any) {
a1587156 8 return exists(value) && VIDEO_LANGUAGES[value] !== undefined
40e87e9e
C
9}
10
edaf5b86
C
11const videoCaptionTypesRegex = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
12 .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
2769e297 13 .map(m => `(${m})`)
edaf5b86 14 .join('|')
c729caf6
C
15function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
16 return isFileValid({
17 files,
18 mimeTypeRegex: videoCaptionTypesRegex,
19 field,
20 maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
21 })
40e87e9e
C
22}
23
474542d7
C
24async function isVTTFileValid (filePath: string) {
25 const size = await getFileSize(filePath)
26
27 if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
28
29 const content = await readFile(filePath, 'utf8')
30
31 return content?.startsWith('WEBVTT\n')
32}
33
40e87e9e
C
34// ---------------------------------------------------------------------------
35
36export {
37 isVideoCaptionFile,
474542d7 38 isVTTFileValid,
3e753302 39 isVideoCaptionLanguageValid
40e87e9e 40}