]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-captions.ts
Handle subtitles in player
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
CommitLineData
40e87e9e
C
1import { CONSTRAINTS_FIELDS, VIDEO_LANGUAGES } from '../../initializers'
2import { exists, isFileValid } from './misc'
3import { Response } from 'express'
4import { VideoModel } from '../../models/video/video'
5import { VideoCaptionModel } from '../../models/video/video-caption'
6
7function isVideoCaptionLanguageValid (value: any) {
8 return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined
9}
10
11const videoCaptionTypes = CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
12 .map(v => v.replace('.', ''))
13 .join('|')
14const videoCaptionsTypesRegex = `text/(${videoCaptionTypes})`
15
16function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
17 return isFileValid(files, videoCaptionsTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
18}
19
20async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) {
21 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
22
23 if (!videoCaption) {
24 res.status(404)
25 .json({ error: 'Video caption not found' })
26 .end()
27
28 return false
29 }
30
31 res.locals.videoCaption = videoCaption
32 return true
33}
34
35// ---------------------------------------------------------------------------
36
37export {
38 isVideoCaptionFile,
39 isVideoCaptionLanguageValid,
40 isVideoCaptionExist
41}