]>
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, | |
ac81d1a0 | 11 | VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, |
3fd3ab2d C |
12 | VIDEO_PRIVACIES, |
13 | VIDEO_RATE_TYPES | |
14 | } from '../../initializers' | |
15 | import { VideoModel } from '../../models/video/video' | |
ac81d1a0 | 16 | import { exists, isArray, isFileValid } 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 | ||
8e10cf1a C |
33 | function isVideoDurationValid (value: string) { |
34 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) | |
35 | } | |
36 | ||
9567011b C |
37 | function isVideoTruncatedDescriptionValid (value: string) { |
38 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION) | |
39 | } | |
40 | ||
69818c93 | 41 | function isVideoDescriptionValid (value: string) { |
f595d394 | 42 | return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)) |
be587647 C |
43 | } |
44 | ||
2422c46b C |
45 | function isVideoSupportValid (value: string) { |
46 | return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT)) | |
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) { |
57a49263 | 72 | return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1 |
d38b8281 C |
73 | } |
74 | ||
ac81d1a0 C |
75 | const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`) |
76 | const videoFileTypesRegex = videoFileTypes.join('|') | |
b60e5f38 | 77 | function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { |
ac81d1a0 C |
78 | return isFileValid(files, videoFileTypesRegex, 'videofile') |
79 | } | |
f6f7dfee | 80 | |
ac81d1a0 C |
81 | const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME |
82 | .map(v => v.replace('.', '')) | |
83 | .join('|') | |
84 | const videoImageTypesRegex = `image/(${videoImageTypes})` | |
85 | function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { | |
86 | return isFileValid(files, videoImageTypesRegex, field, true) | |
f6f7dfee C |
87 | } |
88 | ||
d4f1e94c | 89 | function isVideoPrivacyValid (value: string) { |
f595d394 | 90 | return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined |
d4f1e94c C |
91 | } |
92 | ||
93e1258c C |
93 | function isVideoFileInfoHashValid (value: string) { |
94 | return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) | |
95 | } | |
96 | ||
d4f1e94c C |
97 | function isVideoFileResolutionValid (value: string) { |
98 | return exists(value) && validator.isInt(value + '') | |
99 | } | |
100 | ||
101 | function isVideoFileSizeValid (value: string) { | |
102 | return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE) | |
8d468a16 C |
103 | } |
104 | ||
a2431b7d | 105 | async function isVideoExist (id: string, res: Response) { |
3fd3ab2d | 106 | let video: VideoModel |
4e50b6a1 C |
107 | |
108 | if (validator.isInt(id)) { | |
3fd3ab2d | 109 | video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id) |
4e50b6a1 | 110 | } else { // UUID |
3fd3ab2d | 111 | video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id) |
4e50b6a1 C |
112 | } |
113 | ||
114 | if (!video) { | |
115 | res.status(404) | |
116 | .json({ error: 'Video not found' }) | |
117 | .end() | |
118 | ||
119 | return false | |
120 | } | |
121 | ||
122 | res.locals.video = video | |
123 | return true | |
124 | } | |
125 | ||
55fa55a9 C |
126 | // --------------------------------------------------------------------------- |
127 | ||
65fcc311 | 128 | export { |
65fcc311 C |
129 | isVideoCategoryValid, |
130 | isVideoLicenceValid, | |
131 | isVideoLanguageValid, | |
9567011b | 132 | isVideoTruncatedDescriptionValid, |
65fcc311 | 133 | isVideoDescriptionValid, |
93e1258c | 134 | isVideoFileInfoHashValid, |
65fcc311 C |
135 | isVideoNameValid, |
136 | isVideoTagsValid, | |
65fcc311 | 137 | isVideoAbuseReasonValid, |
65fcc311 C |
138 | isVideoFile, |
139 | isVideoViewsValid, | |
65fcc311 | 140 | isVideoRatingTypeValid, |
8e10cf1a | 141 | isVideoDurationValid, |
0d0e8dd0 | 142 | isVideoTagValid, |
8d468a16 | 143 | isVideoPrivacyValid, |
d4f1e94c C |
144 | isVideoFileResolutionValid, |
145 | isVideoFileSizeValid, | |
ac81d1a0 | 146 | isVideoExist, |
2422c46b C |
147 | isVideoImage, |
148 | isVideoSupportValid | |
65fcc311 | 149 | } |