2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_LANGUAGES } from '../../initializers/constants'
|
2018-07-12 12:02:00 -05:00
|
|
|
import { exists, isFileValid } from './misc'
|
|
|
|
import { Response } from 'express'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoCaptionModel } from '../../models/video/video-caption'
|
|
|
|
|
|
|
|
function isVideoCaptionLanguageValid (value: any) {
|
|
|
|
return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined
|
|
|
|
}
|
|
|
|
|
2018-12-11 07:52:50 -06:00
|
|
|
const videoCaptionTypes = Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT)
|
2018-08-06 04:45:24 -05:00
|
|
|
.concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream ><
|
|
|
|
.map(m => `(${m})`)
|
2018-07-16 07:22:16 -05:00
|
|
|
const videoCaptionTypesRegex = videoCaptionTypes.join('|')
|
2018-07-12 12:02:00 -05:00
|
|
|
function isVideoCaptionFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
|
2018-07-16 07:22:16 -05:00
|
|
|
return isFileValid(files, videoCaptionTypesRegex, field, CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max)
|
2018-07-12 12:02:00 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:26:50 -05:00
|
|
|
async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) {
|
2018-07-12 12:02:00 -05:00
|
|
|
const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
|
|
|
|
|
|
|
|
if (!videoCaption) {
|
|
|
|
res.status(404)
|
|
|
|
.json({ error: 'Video caption not found' })
|
|
|
|
.end()
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoCaption = videoCaption
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isVideoCaptionFile,
|
|
|
|
isVideoCaptionLanguageValid,
|
2019-03-19 03:26:50 -05:00
|
|
|
doesVideoCaptionExist
|
2018-07-12 12:02:00 -05:00
|
|
|
}
|