]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-captions.ts
Handle .srt subtitles
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-captions.ts
CommitLineData
f4001cf4 1import { CONSTRAINTS_FIELDS, VIDEO_CAPTIONS_MIMETYPE_EXT, VIDEO_LANGUAGES, VIDEO_MIMETYPE_EXT } from '../../initializers'
40e87e9e
C
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
f4001cf4
C
11const videoCaptionTypes = Object.keys(VIDEO_CAPTIONS_MIMETYPE_EXT).map(m => `(${m})`)
12const videoCaptionTypesRegex = videoCaptionTypes.join('|')
40e87e9e 13function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
f4001cf4 14 return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
40e87e9e
C
15}
16
17async function isVideoCaptionExist (video: VideoModel, language: string, res: Response) {
18 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
19
20 if (!videoCaption) {
21 res.status(404)
22 .json({ error: 'Video caption not found' })
23 .end()
24
25 return false
26 }
27
28 res.locals.videoCaption = videoCaption
29 return true
30}
31
32// ---------------------------------------------------------------------------
33
34export {
35 isVideoCaptionFile,
36 isVideoCaptionLanguageValid,
37 isVideoCaptionExist
38}