]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/videos.ts
Update videos response api
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
CommitLineData
8d468a16 1import { Response } from 'express'
fdbda9e3 2import 'express-validator'
79d5caf9 3import { values } from 'lodash'
1840c2f7 4import 'multer'
79d5caf9
C
5import * as validator from 'validator'
6import { VideoRateType } from '../../../shared'
3fd3ab2d
C
7import {
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'
15import { VideoModel } from '../../models/video/video'
ac81d1a0 16import { exists, isArray, isFileValid } from './misc'
65fcc311
C
17
18const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
19const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
0b697522 20
69818c93 21function isVideoCategoryValid (value: number) {
f595d394 22 return value === null || VIDEO_CATEGORIES[value] !== undefined
6e07c3de
C
23}
24
69818c93 25function isVideoLicenceValid (value: number) {
f595d394 26 return value === null || VIDEO_LICENCES[value] !== undefined
6f0c39e2
C
27}
28
ae5a3dd6
C
29function areVideoLanguagesValid (value: number[]) {
30 return value === null || (isArray(value) && value.every(v => isVideoLanguageValid(v)))
31}
32
69818c93 33function isVideoLanguageValid (value: number) {
ae5a3dd6 34 return VIDEO_LANGUAGES[value] !== undefined
3092476e
C
35}
36
8e10cf1a
C
37function isVideoDurationValid (value: string) {
38 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
39}
40
9567011b
C
41function isVideoTruncatedDescriptionValid (value: string) {
42 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
43}
44
69818c93 45function isVideoDescriptionValid (value: string) {
f595d394 46 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
be587647
C
47}
48
2422c46b
C
49function isVideoSupportValid (value: string) {
50 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
51}
52
69818c93
C
53function isVideoNameValid (value: string) {
54 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
be587647
C
55}
56
0d0e8dd0
C
57function isVideoTagValid (tag: string) {
58 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
59}
60
69818c93 61function isVideoTagsValid (tags: string[]) {
65fcc311 62 return isArray(tags) &&
69818c93 63 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
0d0e8dd0 64 tags.every(tag => isVideoTagValid(tag))
be587647
C
65}
66
69818c93
C
67function isVideoAbuseReasonValid (value: string) {
68 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
e4c55619
C
69}
70
69818c93
C
71function isVideoViewsValid (value: string) {
72 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
9e167724
C
73}
74
69818c93 75function isVideoRatingTypeValid (value: string) {
57a49263 76 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
d38b8281
C
77}
78
ac81d1a0
C
79const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
80const videoFileTypesRegex = videoFileTypes.join('|')
b60e5f38 81function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
ac81d1a0
C
82 return isFileValid(files, videoFileTypesRegex, 'videofile')
83}
f6f7dfee 84
ac81d1a0
C
85const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
86 .map(v => v.replace('.', ''))
87 .join('|')
88const videoImageTypesRegex = `image/(${videoImageTypes})`
89function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
90 return isFileValid(files, videoImageTypesRegex, field, true)
f6f7dfee
C
91}
92
d4f1e94c 93function isVideoPrivacyValid (value: string) {
f595d394 94 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined
d4f1e94c
C
95}
96
93e1258c
C
97function isVideoFileInfoHashValid (value: string) {
98 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
99}
100
d4f1e94c
C
101function isVideoFileResolutionValid (value: string) {
102 return exists(value) && validator.isInt(value + '')
103}
104
105function isVideoFileSizeValid (value: string) {
106 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
8d468a16
C
107}
108
a2431b7d 109async function isVideoExist (id: string, res: Response) {
3fd3ab2d 110 let video: VideoModel
4e50b6a1
C
111
112 if (validator.isInt(id)) {
3fd3ab2d 113 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
4e50b6a1 114 } else { // UUID
3fd3ab2d 115 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
4e50b6a1
C
116 }
117
118 if (!video) {
119 res.status(404)
120 .json({ error: 'Video not found' })
121 .end()
122
123 return false
124 }
125
126 res.locals.video = video
127 return true
128}
129
55fa55a9
C
130// ---------------------------------------------------------------------------
131
65fcc311 132export {
65fcc311
C
133 isVideoCategoryValid,
134 isVideoLicenceValid,
135 isVideoLanguageValid,
9567011b 136 isVideoTruncatedDescriptionValid,
65fcc311 137 isVideoDescriptionValid,
93e1258c 138 isVideoFileInfoHashValid,
65fcc311 139 isVideoNameValid,
ae5a3dd6 140 areVideoLanguagesValid,
65fcc311 141 isVideoTagsValid,
65fcc311 142 isVideoAbuseReasonValid,
65fcc311
C
143 isVideoFile,
144 isVideoViewsValid,
65fcc311 145 isVideoRatingTypeValid,
8e10cf1a 146 isVideoDurationValid,
0d0e8dd0 147 isVideoTagValid,
8d468a16 148 isVideoPrivacyValid,
d4f1e94c
C
149 isVideoFileResolutionValid,
150 isVideoFileSizeValid,
ac81d1a0 151 isVideoExist,
2422c46b
C
152 isVideoImage,
153 isVideoSupportValid
65fcc311 154}