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