1
0
Fork 0
peertube/server/helpers/custom-validators/video-captions.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-02-11 04:51:33 -05:00
import { UploadFilesForCheck } from 'express'
import { readFile } from 'fs-extra'
2022-02-11 04:51:33 -05:00
import { getFileSize } from '@shared/extra-utils'
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
2018-07-12 13:02:00 -04:00
import { exists, isFileValid } from './misc'
function isVideoCaptionLanguageValid (value: any) {
2020-01-31 10:56:52 -05:00
return exists(value) && VIDEO_LANGUAGES[value] !== undefined
2018-07-12 13:02:00 -04:00
}
2020-04-07 04:40:52 -04:00
const videoCaptionTypesRegex = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
.concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
2018-08-06 05:45:24 -04:00
.map(m => `(${m})`)
2020-04-07 04:40:52 -04:00
.join('|')
2022-02-11 04:51:33 -05:00
function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
return isFileValid({
files,
mimeTypeRegex: videoCaptionTypesRegex,
field,
maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
})
2018-07-12 13:02:00 -04:00
}
async function isVTTFileValid (filePath: string) {
const size = await getFileSize(filePath)
if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
const content = await readFile(filePath, 'utf8')
return content?.startsWith('WEBVTT\n')
}
2018-07-12 13:02:00 -04:00
// ---------------------------------------------------------------------------
export {
isVideoCaptionFile,
isVTTFileValid,
2019-07-23 04:40:39 -04:00
isVideoCaptionLanguageValid
2018-07-12 13:02:00 -04:00
}