]>
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 | ||
60b880ac C |
11 | // MacOS sends application/octet-stream |
12 | const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ] | |
13 | .map(m => `(${m})`) | |
14 | .join('|') | |
15 | ||
c729caf6 C |
16 | function isVideoCaptionFile (files: UploadFilesForCheck, field: string) { |
17 | return isFileValid({ | |
18 | files, | |
19 | mimeTypeRegex: videoCaptionTypesRegex, | |
20 | field, | |
21 | maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
22 | }) | |
40e87e9e C |
23 | } |
24 | ||
474542d7 C |
25 | async function isVTTFileValid (filePath: string) { |
26 | const size = await getFileSize(filePath) | |
27 | ||
28 | if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false | |
29 | ||
30 | const content = await readFile(filePath, 'utf8') | |
31 | ||
32 | return content?.startsWith('WEBVTT\n') | |
33 | } | |
34 | ||
40e87e9e C |
35 | // --------------------------------------------------------------------------- |
36 | ||
37 | export { | |
38 | isVideoCaptionFile, | |
474542d7 | 39 | isVTTFileValid, |
3e753302 | 40 | isVideoCaptionLanguageValid |
40e87e9e | 41 | } |