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