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