]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const validator = require('express-validator').validator | |
4 | ||
5 | const constants = require('../../initializers/constants') | |
6 | const usersValidators = require('./users') | |
7 | const miscValidators = require('./misc') | |
8 | const VIDEOS_CONSTRAINTS_FIELDS = constants.CONSTRAINTS_FIELDS.VIDEOS | |
9 | ||
10 | const videosValidators = { | |
11 | isEachRemoteVideosValid, | |
12 | isVideoAuthorValid, | |
13 | isVideoDateValid, | |
14 | isVideoDescriptionValid, | |
15 | isVideoDurationValid, | |
16 | isVideoInfoHashValid, | |
17 | isVideoNameValid, | |
18 | isVideoTagsValid, | |
19 | isVideoThumbnailValid, | |
20 | isVideoThumbnail64Valid | |
21 | } | |
22 | ||
23 | function isEachRemoteVideosValid (requests) { | |
24 | return miscValidators.isArray(requests) && | |
25 | requests.every(function (request) { | |
26 | const video = request.data | |
27 | return ( | |
28 | isRequestTypeAddValid(request.type) && | |
29 | isVideoAuthorValid(video.author) && | |
30 | isVideoDateValid(video.createdAt) && | |
31 | isVideoDescriptionValid(video.description) && | |
32 | isVideoDurationValid(video.duration) && | |
33 | isVideoInfoHashValid(video.infoHash) && | |
34 | isVideoNameValid(video.name) && | |
35 | isVideoTagsValid(video.tags) && | |
36 | isVideoThumbnail64Valid(video.thumbnailBase64) && | |
37 | isVideoRemoteIdValid(video.remoteId) && | |
38 | isVideoExtnameValid(video.extname) | |
39 | ) || | |
40 | ( | |
41 | isRequestTypeRemoveValid(request.type) && | |
42 | isVideoNameValid(video.name) && | |
43 | isVideoRemoteIdValid(video.remoteId) | |
44 | ) | |
45 | }) | |
46 | } | |
47 | ||
48 | function isVideoAuthorValid (value) { | |
49 | return usersValidators.isUserUsernameValid(value) | |
50 | } | |
51 | ||
52 | function isVideoDateValid (value) { | |
53 | return validator.isDate(value) | |
54 | } | |
55 | ||
56 | function isVideoDescriptionValid (value) { | |
57 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) | |
58 | } | |
59 | ||
60 | function isVideoDurationValid (value) { | |
61 | return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | |
62 | } | |
63 | ||
64 | function isVideoExtnameValid (value) { | |
65 | return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 | |
66 | } | |
67 | ||
68 | function isVideoInfoHashValid (value) { | |
69 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | |
70 | } | |
71 | ||
72 | function isVideoNameValid (value) { | |
73 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | |
74 | } | |
75 | ||
76 | function isVideoTagsValid (tags) { | |
77 | return miscValidators.isArray(tags) && | |
78 | validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && | |
79 | tags.every(function (tag) { | |
80 | return validator.isAlphanumeric(tag) && | |
81 | validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | |
82 | }) | |
83 | } | |
84 | ||
85 | function isVideoThumbnailValid (value) { | |
86 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) | |
87 | } | |
88 | ||
89 | function isVideoThumbnail64Valid (value) { | |
90 | return validator.isBase64(value) && | |
91 | validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64) | |
92 | } | |
93 | ||
94 | function isVideoRemoteIdValid (value) { | |
95 | return validator.isUUID(value, 4) | |
96 | } | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | module.exports = videosValidators | |
101 | ||
102 | // --------------------------------------------------------------------------- | |
103 | ||
104 | function isRequestTypeAddValid (value) { | |
105 | return value === 'add' | |
106 | } | |
107 | ||
108 | function isRequestTypeRemoveValid (value) { | |
109 | return value === 'remove' | |
110 | } |