]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/videos.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
CommitLineData
79d5caf9 1import { values } from 'lodash'
7cde3b9c 2import validator from 'validator'
c8861d5d 3import { VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared'
3fd3ab2d 4import {
8f0bc73d
C
5 CONSTRAINTS_FIELDS,
6 MIMETYPES,
3fd3ab2d 7 VIDEO_CATEGORIES,
28be8916 8 VIDEO_LICENCES,
3fd3ab2d 9 VIDEO_PRIVACIES,
2186386c 10 VIDEO_RATE_TYPES,
c6c0fa6c
C
11 VIDEO_STATES,
12 VIDEO_LIVE
74dc3bca 13} from '../../initializers/constants'
f2eb23cd 14import { exists, isArray, isDateValid, isFileMimeTypeValid, isFileValid } from './misc'
ce33919c 15import * as magnetUtil from 'magnet-uri'
65fcc311
C
16
17const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
0b697522 18
1cd3facc 19function isVideoFilterValid (filter: VideoFilter) {
0aa52e17 20 return filter === 'local' || filter === 'all-local' || filter === 'all'
1cd3facc
C
21}
22
9d3ef9fe 23function isVideoCategoryValid (value: any) {
a1587156 24 return value === null || VIDEO_CATEGORIES[value] !== undefined
2186386c
C
25}
26
27function isVideoStateValid (value: any) {
a1587156 28 return exists(value) && VIDEO_STATES[value] !== undefined
6e07c3de
C
29}
30
9d3ef9fe 31function isVideoLicenceValid (value: any) {
a1587156 32 return value === null || VIDEO_LICENCES[value] !== undefined
6f0c39e2
C
33}
34
9d3ef9fe
C
35function isVideoLanguageValid (value: any) {
36 return value === null ||
37 (typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
3092476e
C
38}
39
8e10cf1a
C
40function isVideoDurationValid (value: string) {
41 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
42}
43
9567011b
C
44function isVideoTruncatedDescriptionValid (value: string) {
45 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
46}
47
69818c93 48function isVideoDescriptionValid (value: string) {
f595d394 49 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
be587647
C
50}
51
2422c46b
C
52function isVideoSupportValid (value: string) {
53 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
54}
55
69818c93
C
56function isVideoNameValid (value: string) {
57 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
be587647
C
58}
59
0d0e8dd0
C
60function isVideoTagValid (tag: string) {
61 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
62}
63
69818c93 64function isVideoTagsValid (tags: string[]) {
2efd32f6
C
65 return tags === null || (
66 isArray(tags) &&
67 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
68 tags.every(tag => isVideoTagValid(tag))
69 )
be587647
C
70}
71
69818c93
C
72function isVideoViewsValid (value: string) {
73 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
9e167724
C
74}
75
69818c93 76function isVideoRatingTypeValid (value: string) {
bdd428a6 77 return value === 'none' || values(VIDEO_RATE_TYPES).includes(value as VideoRateType)
d38b8281
C
78}
79
14e2014a 80function isVideoFileExtnameValid (value: string) {
c6c0fa6c 81 return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined)
14e2014a 82}
2186386c 83
f2eb23cd
RK
84function isVideoFileMimeTypeValid (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
85 return isFileMimeTypeValid(files, MIMETYPES.VIDEO.MIMETYPES_REGEX, 'videofile')
ac81d1a0 86}
f6f7dfee 87
ac81d1a0 88const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
2186386c
C
89 .map(v => v.replace('.', ''))
90 .join('|')
ac81d1a0 91const videoImageTypesRegex = `image/(${videoImageTypes})`
2186386c 92
ac81d1a0 93function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
0c237b19 94 return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
f6f7dfee
C
95}
96
2baea0c7 97function isVideoPrivacyValid (value: number) {
a1587156 98 return VIDEO_PRIVACIES[value] !== undefined
d4f1e94c
C
99}
100
2baea0c7 101function isScheduleVideoUpdatePrivacyValid (value: number) {
22a73cb8 102 return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL
2baea0c7
C
103}
104
1e74f19a 105function isVideoOriginallyPublishedAtValid (value: string | null) {
106 return value === null || isDateValid(value)
107}
108
2cebd797 109function isVideoFileInfoHashValid (value: string | null | undefined) {
93e1258c
C
110 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
111}
112
d4f1e94c
C
113function isVideoFileResolutionValid (value: string) {
114 return exists(value) && validator.isInt(value + '')
115}
116
3a6f351b
C
117function isVideoFPSResolutionValid (value: string) {
118 return value === null || validator.isInt(value + '')
119}
120
d4f1e94c
C
121function isVideoFileSizeValid (value: string) {
122 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
8d468a16
C
123}
124
ce33919c
C
125function isVideoMagnetUriValid (value: string) {
126 if (!exists(value)) return false
127
128 const parsed = magnetUtil.decode(value)
129 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
130}
131
55fa55a9
C
132// ---------------------------------------------------------------------------
133
65fcc311 134export {
65fcc311
C
135 isVideoCategoryValid,
136 isVideoLicenceValid,
137 isVideoLanguageValid,
9567011b 138 isVideoTruncatedDescriptionValid,
65fcc311 139 isVideoDescriptionValid,
93e1258c 140 isVideoFileInfoHashValid,
65fcc311
C
141 isVideoNameValid,
142 isVideoTagsValid,
3a6f351b 143 isVideoFPSResolutionValid,
2baea0c7 144 isScheduleVideoUpdatePrivacyValid,
1e74f19a 145 isVideoOriginallyPublishedAtValid,
ce33919c 146 isVideoMagnetUriValid,
2186386c 147 isVideoStateValid,
65fcc311 148 isVideoViewsValid,
65fcc311 149 isVideoRatingTypeValid,
14e2014a 150 isVideoFileExtnameValid,
f2eb23cd 151 isVideoFileMimeTypeValid,
8e10cf1a 152 isVideoDurationValid,
0d0e8dd0 153 isVideoTagValid,
8d468a16 154 isVideoPrivacyValid,
d4f1e94c
C
155 isVideoFileResolutionValid,
156 isVideoFileSizeValid,
2422c46b 157 isVideoImage,
1cd3facc
C
158 isVideoSupportValid,
159 isVideoFilterValid
65fcc311 160}