2022-02-11 03:51:33 -06:00
|
|
|
import { UploadFilesForCheck } from 'express'
|
2022-02-09 01:58:40 -06:00
|
|
|
import { readFile } from 'fs-extra'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { getFileSize } from '@shared/extra-utils'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
|
2018-07-12 12:02:00 -05:00
|
|
|
import { exists, isFileValid } from './misc'
|
|
|
|
|
|
|
|
function isVideoCaptionLanguageValid (value: any) {
|
2020-01-31 09:56:52 -06:00
|
|
|
return exists(value) && VIDEO_LANGUAGES[value] !== undefined
|
2018-07-12 12:02:00 -05:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:40:52 -05:00
|
|
|
const videoCaptionTypesRegex = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
|
|
|
|
.concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
|
2018-08-06 04:45:24 -05:00
|
|
|
.map(m => `(${m})`)
|
2020-04-07 03:40:52 -05:00
|
|
|
.join('|')
|
2022-02-11 03:51:33 -06: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 12:02:00 -05:00
|
|
|
}
|
|
|
|
|
2022-02-09 01:58:40 -06: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 12:02:00 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isVideoCaptionFile,
|
2022-02-09 01:58:40 -06:00
|
|
|
isVTTFileValid,
|
2019-07-23 03:40:39 -05:00
|
|
|
isVideoCaptionLanguageValid
|
2018-07-12 12:02:00 -05:00
|
|
|
}
|