]>
Commit | Line | Data |
---|---|---|
8d468a16 | 1 | import { Response } from 'express' |
fdbda9e3 | 2 | import 'express-validator' |
79d5caf9 | 3 | import { values } from 'lodash' |
1840c2f7 | 4 | import 'multer' |
79d5caf9 C |
5 | import * as validator from 'validator' |
6 | import { VideoRateType } from '../../../shared' | |
3fd3ab2d C |
7 | import { |
8 | CONSTRAINTS_FIELDS, | |
9 | VIDEO_CATEGORIES, | |
10 | VIDEO_LANGUAGES, | |
11 | VIDEO_LICENCES, | |
12 | VIDEO_PRIVACIES, | |
13 | VIDEO_RATE_TYPES | |
14 | } from '../../initializers' | |
15 | import { VideoModel } from '../../models/video/video' | |
79d5caf9 | 16 | import { exists, isArray } from './misc' |
65fcc311 C |
17 | |
18 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | |
19 | const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES | |
0b697522 | 20 | |
69818c93 | 21 | function isVideoCategoryValid (value: number) { |
f595d394 | 22 | return value === null || VIDEO_CATEGORIES[value] !== undefined |
6e07c3de C |
23 | } |
24 | ||
69818c93 | 25 | function isVideoLicenceValid (value: number) { |
f595d394 | 26 | return value === null || VIDEO_LICENCES[value] !== undefined |
6f0c39e2 C |
27 | } |
28 | ||
69818c93 | 29 | function isVideoLanguageValid (value: number) { |
65fcc311 | 30 | return value === null || VIDEO_LANGUAGES[value] !== undefined |
3092476e C |
31 | } |
32 | ||
69818c93 C |
33 | function isVideoNSFWValid (value: any) { |
34 | return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value)) | |
31b59b47 C |
35 | } |
36 | ||
8e10cf1a C |
37 | function isVideoDurationValid (value: string) { |
38 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | |
39 | } | |
40 | ||
9567011b C |
41 | function isVideoTruncatedDescriptionValid (value: string) { |
42 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION) | |
43 | } | |
44 | ||
69818c93 | 45 | function isVideoDescriptionValid (value: string) { |
f595d394 | 46 | return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)) |
be587647 C |
47 | } |
48 | ||
69818c93 C |
49 | function isVideoNameValid (value: string) { |
50 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | |
be587647 C |
51 | } |
52 | ||
0d0e8dd0 C |
53 | function isVideoTagValid (tag: string) { |
54 | return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) | |
55 | } | |
56 | ||
69818c93 | 57 | function isVideoTagsValid (tags: string[]) { |
65fcc311 | 58 | return isArray(tags) && |
69818c93 | 59 | validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) && |
0d0e8dd0 | 60 | tags.every(tag => isVideoTagValid(tag)) |
be587647 C |
61 | } |
62 | ||
69818c93 C |
63 | function isVideoAbuseReasonValid (value: string) { |
64 | return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) | |
e4c55619 C |
65 | } |
66 | ||
69818c93 C |
67 | function isVideoViewsValid (value: string) { |
68 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) | |
9e167724 C |
69 | } |
70 | ||
69818c93 | 71 | function isVideoRatingTypeValid (value: string) { |
ee9e7b61 | 72 | return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 |
d38b8281 C |
73 | } |
74 | ||
b60e5f38 | 75 | function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { |
f6f7dfee C |
76 | // Should have files |
77 | if (!files) return false | |
b60e5f38 | 78 | if (isArray(files)) return false |
f6f7dfee C |
79 | |
80 | // Should have videofile file | |
b60e5f38 | 81 | const videofile = files['videofile'] |
f6f7dfee C |
82 | if (!videofile || videofile.length === 0) return false |
83 | ||
84 | // The file should exist | |
85 | const file = videofile[0] | |
86 | if (!file || !file.originalname) return false | |
87 | ||
88 | return new RegExp('^video/(webm|mp4|ogg)$', 'i').test(file.mimetype) | |
89 | } | |
90 | ||
d4f1e94c | 91 | function isVideoPrivacyValid (value: string) { |
f595d394 | 92 | return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined |
d4f1e94c C |
93 | } |
94 | ||
93e1258c C |
95 | function isVideoFileInfoHashValid (value: string) { |
96 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | |
97 | } | |
98 | ||
d4f1e94c C |
99 | function isVideoFileResolutionValid (value: string) { |
100 | return exists(value) && validator.isInt(value + '') | |
101 | } | |
102 | ||
103 | function isVideoFileSizeValid (value: string) { | |
104 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | |
8d468a16 C |
105 | } |
106 | ||
a2431b7d | 107 | async function isVideoExist (id: string, res: Response) { |
3fd3ab2d | 108 | let video: VideoModel |
4e50b6a1 C |
109 | |
110 | if (validator.isInt(id)) { | |
3fd3ab2d | 111 | video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id) |
4e50b6a1 | 112 | } else { // UUID |
3fd3ab2d | 113 | video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id) |
4e50b6a1 C |
114 | } |
115 | ||
116 | if (!video) { | |
117 | res.status(404) | |
118 | .json({ error: 'Video not found' }) | |
119 | .end() | |
120 | ||
121 | return false | |
122 | } | |
123 | ||
124 | res.locals.video = video | |
125 | return true | |
126 | } | |
127 | ||
55fa55a9 C |
128 | // --------------------------------------------------------------------------- |
129 | ||
65fcc311 | 130 | export { |
65fcc311 C |
131 | isVideoCategoryValid, |
132 | isVideoLicenceValid, | |
133 | isVideoLanguageValid, | |
134 | isVideoNSFWValid, | |
9567011b | 135 | isVideoTruncatedDescriptionValid, |
65fcc311 | 136 | isVideoDescriptionValid, |
93e1258c | 137 | isVideoFileInfoHashValid, |
65fcc311 C |
138 | isVideoNameValid, |
139 | isVideoTagsValid, | |
65fcc311 | 140 | isVideoAbuseReasonValid, |
65fcc311 C |
141 | isVideoFile, |
142 | isVideoViewsValid, | |
65fcc311 | 143 | isVideoRatingTypeValid, |
8e10cf1a | 144 | isVideoDurationValid, |
0d0e8dd0 | 145 | isVideoTagValid, |
8d468a16 | 146 | isVideoPrivacyValid, |
d4f1e94c C |
147 | isVideoFileResolutionValid, |
148 | isVideoFileSizeValid, | |
a2431b7d | 149 | isVideoExist |
65fcc311 | 150 | } |