]>
Commit | Line | Data |
---|---|---|
474542d7 C |
1 | import { getFileSize } from '@shared/extra-utils' |
2 | import { readFile } from 'fs-extra' | |
74dc3bca | 3 | import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants' |
40e87e9e | 4 | import { exists, isFileValid } from './misc' |
40e87e9e C |
5 | |
6 | function isVideoCaptionLanguageValid (value: any) { | |
a1587156 | 7 | return exists(value) && VIDEO_LANGUAGES[value] !== undefined |
40e87e9e C |
8 | } |
9 | ||
edaf5b86 C |
10 | const 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 | 14 | function 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 |
18 | async 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 | ||
30 | export { | |
31 | isVideoCaptionFile, | |
474542d7 | 32 | isVTTFileValid, |
3e753302 | 33 | isVideoCaptionLanguageValid |
40e87e9e | 34 | } |