Check caption file content

This commit is contained in:
Chocobozzz
2026-07-01 14:15:35 +02:00
parent c1a15ea23f
commit af9381a453
2 changed files with 11 additions and 13 deletions
@@ -1,4 +1,5 @@
import { HttpStatusCode, VideoCaptionGenerate, VideoChannelActivityAction } from '@peertube/peertube-models'
import { isVTTFileValid } from '@server/helpers/custom-validators/video-captions.js'
import { retryTransactionWrapper } from '@server/helpers/database-utils.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
import { createLocalCaption, createTranscriptionTaskIfNeeded, updateHLSMasterOnCaptionChangeIfNeeded } from '@server/lib/video-captions.js'
@@ -79,14 +80,19 @@ async function listVideoCaptions (req: express.Request, res: express.Response) {
async function createVideoCaption (req: express.Request, res: express.Response) {
const videoCaptionPhysicalFile: Express.Multer.File = req.files['captionfile'][0]
const captionPath = videoCaptionPhysicalFile.path
const video = res.locals.videoFull
if (!await isVTTFileValid(captionPath)) {
return res.fail({ status: HttpStatusCode.BAD_REQUEST_400, message: req.t('Invalid caption file') })
}
const captionLanguage = req.params.captionLanguage
const videoCaption = await createLocalCaption({
video,
language: captionLanguage,
path: videoCaptionPhysicalFile.path,
path: captionPath,
automaticallyGenerated: false
})
@@ -5,7 +5,7 @@ import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_TEXT_LANGUAGES } from '../../initi
import { logger } from '../logger.js'
import { exists, isFileValid } from './misc.js'
function isVideoCaptionLanguageValid (value: any) {
export function isVideoCaptionLanguageValid (value: any) {
return exists(value) && VIDEO_TEXT_LANGUAGES[value] !== undefined
}
@@ -14,7 +14,7 @@ const videoCaptionTypesRegex = [ ...Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYP
.map(m => `(${m})`)
.join('|')
function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
export function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
return isFileValid({
files,
mimeTypeRegex: videoCaptionTypesRegex,
@@ -23,7 +23,7 @@ function isVideoCaptionFile (files: UploadFilesForCheck, field: string) {
})
}
async function isVTTFileValid (filePath: string) {
export async function isVTTFileValid (filePath: string) {
const size = await getFileSize(filePath)
const content = await readFile(filePath, 'utf8')
@@ -31,13 +31,5 @@ async function isVTTFileValid (filePath: string) {
if (size > CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max) return false
return content?.startsWith('WEBVTT')
}
// ---------------------------------------------------------------------------
export {
isVideoCaptionFile,
isVTTFileValid,
isVideoCaptionLanguageValid
return !!content?.startsWith('WEBVTT')
}