]>
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' |
c56dd280 | 5 | import { logger } from '../logger' |
40e87e9e | 6 | import { exists, isFileValid } from './misc' |
40e87e9e C |
7 | |
8 | function isVideoCaptionLanguageValid (value: any) { | |
a1587156 | 9 | return exists(value) && VIDEO_LANGUAGES[value] !== undefined |
40e87e9e C |
10 | } |
11 | ||
60b880ac C |
12 | // MacOS sends application/octet-stream |
13 | const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ] | |
14 | .map(m => `(${m})`) | |
15 | .join('|') | |
16 | ||
c729caf6 C |
17 | function isVideoCaptionFile (files: UploadFilesForCheck, field: string) { |
18 | return isFileValid({ | |
19 | files, | |
20 | mimeTypeRegex: videoCaptionTypesRegex, | |
21 | field, | |
22 | maxSize: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
23 | }) | |
40e87e9e C |
24 | } |
25 | ||
474542d7 C |
26 | async function isVTTFileValid (filePath: string) { |
27 | const size = await getFileSize(filePath) | |
c56dd280 | 28 | const content = await readFile(filePath, 'utf8') |
474542d7 | 29 | |
c56dd280 | 30 | logger.debug('Checking VTT file %s', filePath, { size, content }) |
474542d7 | 31 | |
c56dd280 | 32 | if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false |
474542d7 | 33 | |
c56dd280 | 34 | return content?.startsWith('WEBVTT') |
474542d7 C |
35 | } |
36 | ||
40e87e9e C |
37 | // --------------------------------------------------------------------------- |
38 | ||
39 | export { | |
40 | isVideoCaptionFile, | |
474542d7 | 41 | isVTTFileValid, |
3e753302 | 42 | isVideoCaptionLanguageValid |
40e87e9e | 43 | } |