diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-12 19:02:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 11:50:08 +0200 |
commit | 40e87e9ecc54e3513fb586928330a7855eb192c6 (patch) | |
tree | af1111ecba85f9cd8286811ff332a67cf21be2f6 /server/helpers/custom-validators/video-captions.ts | |
parent | d4557fd3ecc8d4ed4fb0e5c868929bc36c959ed2 (diff) | |
download | PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.gz PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.zst PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.zip |
Implement captions/subtitles
Diffstat (limited to 'server/helpers/custom-validators/video-captions.ts')
-rw-r--r-- | server/helpers/custom-validators/video-captions.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/video-captions.ts b/server/helpers/custom-validators/video-captions.ts new file mode 100644 index 000000000..fd4dc740b --- /dev/null +++ b/server/helpers/custom-validators/video-captions.ts | |||
@@ -0,0 +1,41 @@ | |||
1 | import { CONSTRAINTS_FIELDS, VIDEO_LANGUAGES } from '../../initializers' | ||
2 | import { exists, isFileValid } from './misc' | ||
3 | import { Response } from 'express' | ||
4 | import { VideoModel } from '../../models/video/video' | ||
5 | import { VideoCaptionModel } from '../../models/video/video-caption' | ||
6 | |||
7 | function isVideoCaptionLanguageValid (value: any) { | ||
8 | return exists(value) && VIDEO_LANGUAGES[ value ] !== undefined | ||
9 | } | ||
10 | |||
11 | const videoCaptionTypes = CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | ||
12 | .map(v => v.replace('.', '')) | ||
13 | .join('|') | ||
14 | const videoCaptionsTypesRegex = `text/(${videoCaptionTypes})` | ||
15 | |||
16 | function 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 | |||
20 | async 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 | |||
37 | export { | ||
38 | isVideoCaptionFile, | ||
39 | isVideoCaptionLanguageValid, | ||
40 | isVideoCaptionExist | ||
41 | } | ||