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 | |
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')
-rw-r--r-- | server/helpers/custom-validators/activitypub/videos.ts | 13 | ||||
-rw-r--r-- | server/helpers/custom-validators/video-captions.ts | 41 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 24 |
3 files changed, 78 insertions, 0 deletions
diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 37c90a0c8..d97bbd2a9 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts | |||
@@ -51,6 +51,7 @@ function sanitizeAndCheckVideoTorrentObject (video: any) { | |||
51 | if (!setValidRemoteVideoUrls(video)) return false | 51 | if (!setValidRemoteVideoUrls(video)) return false |
52 | if (!setRemoteVideoTruncatedContent(video)) return false | 52 | if (!setRemoteVideoTruncatedContent(video)) return false |
53 | if (!setValidAttributedTo(video)) return false | 53 | if (!setValidAttributedTo(video)) return false |
54 | if (!setValidRemoteCaptions(video)) return false | ||
54 | 55 | ||
55 | // Default attributes | 56 | // Default attributes |
56 | if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED | 57 | if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED |
@@ -98,6 +99,18 @@ function setValidRemoteTags (video: any) { | |||
98 | return true | 99 | return true |
99 | } | 100 | } |
100 | 101 | ||
102 | function setValidRemoteCaptions (video: any) { | ||
103 | if (!video.subtitleLanguage) video.subtitleLanguage = [] | ||
104 | |||
105 | if (Array.isArray(video.subtitleLanguage) === false) return false | ||
106 | |||
107 | video.subtitleLanguage = video.subtitleLanguage.filter(caption => { | ||
108 | return isRemoteStringIdentifierValid(caption) | ||
109 | }) | ||
110 | |||
111 | return true | ||
112 | } | ||
113 | |||
101 | function isRemoteNumberIdentifierValid (data: any) { | 114 | function isRemoteNumberIdentifierValid (data: any) { |
102 | return validator.isInt(data.identifier, { min: 0 }) | 115 | return validator.isInt(data.identifier, { min: 0 }) |
103 | } | 116 | } |
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 | } | ||
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 672f06dc0..b5cb126d9 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -126,6 +126,29 @@ function isVideoFileSizeValid (value: string) { | |||
126 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | 126 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) |
127 | } | 127 | } |
128 | 128 | ||
129 | function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { | ||
130 | // Retrieve the user who did the request | ||
131 | if (video.isOwned() === false) { | ||
132 | res.status(403) | ||
133 | .json({ error: 'Cannot manage a video of another server.' }) | ||
134 | .end() | ||
135 | return false | ||
136 | } | ||
137 | |||
138 | // Check if the user can delete the video | ||
139 | // The user can delete it if he has the right | ||
140 | // Or if s/he is the video's account | ||
141 | const account = video.VideoChannel.Account | ||
142 | if (user.hasRight(right) === false && account.userId !== user.id) { | ||
143 | res.status(403) | ||
144 | .json({ error: 'Cannot manage a video of another user.' }) | ||
145 | .end() | ||
146 | return false | ||
147 | } | ||
148 | |||
149 | return true | ||
150 | } | ||
151 | |||
129 | async function isVideoExist (id: string, res: Response) { | 152 | async function isVideoExist (id: string, res: Response) { |
130 | let video: VideoModel | 153 | let video: VideoModel |
131 | 154 | ||
@@ -179,6 +202,7 @@ async function isVideoChannelOfAccountExist (channelId: number, user: UserModel, | |||
179 | 202 | ||
180 | export { | 203 | export { |
181 | isVideoCategoryValid, | 204 | isVideoCategoryValid, |
205 | checkUserCanManageVideo, | ||
182 | isVideoLicenceValid, | 206 | isVideoLicenceValid, |
183 | isVideoLanguageValid, | 207 | isVideoLanguageValid, |
184 | isVideoTruncatedDescriptionValid, | 208 | isVideoTruncatedDescriptionValid, |