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