]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/videos.ts
Add beautiful loading bar
[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'
8d468a16 7import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_RATE_TYPES } from '../../initializers'
a2431b7d 8import { VIDEO_PRIVACIES } from '../../initializers/constants'
8d468a16
C
9import { database as db } from '../../initializers/database'
10import { VideoInstance } from '../../models/video/video-interface'
79d5caf9 11import { exists, isArray } from './misc'
65fcc311
C
12
13const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
14const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
0b697522 15
69818c93 16function isVideoCategoryValid (value: number) {
f595d394 17 return value === null || VIDEO_CATEGORIES[value] !== undefined
6e07c3de
C
18}
19
69818c93 20function isVideoLicenceValid (value: number) {
f595d394 21 return value === null || VIDEO_LICENCES[value] !== undefined
6f0c39e2
C
22}
23
69818c93 24function isVideoLanguageValid (value: number) {
65fcc311 25 return value === null || VIDEO_LANGUAGES[value] !== undefined
3092476e
C
26}
27
69818c93
C
28function isVideoNSFWValid (value: any) {
29 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
31b59b47
C
30}
31
8e10cf1a
C
32function isVideoDurationValid (value: string) {
33 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
34}
35
9567011b
C
36function isVideoTruncatedDescriptionValid (value: string) {
37 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
38}
39
69818c93 40function isVideoDescriptionValid (value: string) {
f595d394 41 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
be587647
C
42}
43
69818c93
C
44function isVideoNameValid (value: string) {
45 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
be587647
C
46}
47
0d0e8dd0
C
48function isVideoTagValid (tag: string) {
49 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
50}
51
69818c93 52function isVideoTagsValid (tags: string[]) {
65fcc311 53 return isArray(tags) &&
69818c93 54 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
0d0e8dd0 55 tags.every(tag => isVideoTagValid(tag))
be587647
C
56}
57
69818c93
C
58function isVideoAbuseReasonValid (value: string) {
59 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
e4c55619
C
60}
61
69818c93
C
62function isVideoViewsValid (value: string) {
63 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
9e167724
C
64}
65
69818c93 66function isVideoRatingTypeValid (value: string) {
ee9e7b61 67 return values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
d38b8281
C
68}
69
b60e5f38 70function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
f6f7dfee
C
71 // Should have files
72 if (!files) return false
b60e5f38 73 if (isArray(files)) return false
f6f7dfee
C
74
75 // Should have videofile file
b60e5f38 76 const videofile = files['videofile']
f6f7dfee
C
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
d4f1e94c 86function isVideoPrivacyValid (value: string) {
f595d394 87 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined
d4f1e94c
C
88}
89
93e1258c
C
90function isVideoFileInfoHashValid (value: string) {
91 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
92}
93
d4f1e94c
C
94function isVideoFileResolutionValid (value: string) {
95 return exists(value) && validator.isInt(value + '')
96}
97
98function isVideoFileSizeValid (value: string) {
99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
8d468a16
C
100}
101
a2431b7d 102async function isVideoExist (id: string, res: Response) {
4e50b6a1
C
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
55fa55a9
C
123// ---------------------------------------------------------------------------
124
65fcc311 125export {
65fcc311
C
126 isVideoCategoryValid,
127 isVideoLicenceValid,
128 isVideoLanguageValid,
129 isVideoNSFWValid,
9567011b 130 isVideoTruncatedDescriptionValid,
65fcc311 131 isVideoDescriptionValid,
93e1258c 132 isVideoFileInfoHashValid,
65fcc311
C
133 isVideoNameValid,
134 isVideoTagsValid,
65fcc311 135 isVideoAbuseReasonValid,
65fcc311
C
136 isVideoFile,
137 isVideoViewsValid,
65fcc311 138 isVideoRatingTypeValid,
8e10cf1a 139 isVideoDurationValid,
0d0e8dd0 140 isVideoTagValid,
8d468a16 141 isVideoPrivacyValid,
d4f1e94c
C
142 isVideoFileResolutionValid,
143 isVideoFileSizeValid,
a2431b7d 144 isVideoExist
65fcc311 145}