PeerTube/server/core/helpers/custom-validators/video-captions.ts

44 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-02-11 10:51:33 +01:00
import { UploadFilesForCheck } from 'express'
import { readFile } from 'fs/promises'
import { getFileSize } from '@peertube/peertube-node-utils'
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants.js'
import { logger } from '../logger.js'
import { exists, isFileValid } from './misc.js'
2018-07-12 19:02:00 +02:00
function isVideoCaptionLanguageValid (value: any) {
2020-01-31 16:56:52 +01:00
return exists(value) && VIDEO_LANGUAGES[value] !== undefined
2018-07-12 19:02:00 +02:00
}
2022-12-30 10:12:20 +01:00
// MacOS sends application/octet-stream
const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT), 'application/octet-stream' ]
.map(m => `(${m})`)
.join('|')
2022-02-11 10:51:33 +01: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 19:02:00 +02:00
}
async function isVTTFileValid (filePath: string) {
const size = await getFileSize(filePath)
2023-05-26 16:00:19 +02:00
const content = await readFile(filePath, 'utf8')
2023-05-26 16:00:19 +02:00
logger.debug('Checking VTT file %s', filePath, { size, content })
2023-05-26 16:00:19 +02:00
if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
2023-05-26 16:00:19 +02:00
return content?.startsWith('WEBVTT')
}
2018-07-12 19:02:00 +02:00
// ---------------------------------------------------------------------------
export {
isVideoCaptionFile,
isVTTFileValid,
2019-07-23 10:40:39 +02:00
isVideoCaptionLanguageValid
2018-07-12 19:02:00 +02:00
}