]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/videos.ts
Add ability to set video thumbnail/preview
[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 {
8 CONSTRAINTS_FIELDS,
9 VIDEO_CATEGORIES,
10 VIDEO_LANGUAGES,
11 VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
12 VIDEO_PRIVACIES,
13 VIDEO_RATE_TYPES
14 } from '../../initializers'
15 import { VideoModel } from '../../models/video/video'
16 import { exists, isArray, isFileValid } from './misc'
17
18 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
19 const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
20
21 function isVideoCategoryValid (value: number) {
22 return value === null || VIDEO_CATEGORIES[value] !== undefined
23 }
24
25 function isVideoLicenceValid (value: number) {
26 return value === null || VIDEO_LICENCES[value] !== undefined
27 }
28
29 function isVideoLanguageValid (value: number) {
30 return value === null || VIDEO_LANGUAGES[value] !== undefined
31 }
32
33 function isVideoDurationValid (value: string) {
34 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
35 }
36
37 function isVideoTruncatedDescriptionValid (value: string) {
38 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
39 }
40
41 function isVideoDescriptionValid (value: string) {
42 return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
43 }
44
45 function isVideoNameValid (value: string) {
46 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
47 }
48
49 function isVideoTagValid (tag: string) {
50 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
51 }
52
53 function isVideoTagsValid (tags: string[]) {
54 return isArray(tags) &&
55 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
56 tags.every(tag => isVideoTagValid(tag))
57 }
58
59 function isVideoAbuseReasonValid (value: string) {
60 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
61 }
62
63 function isVideoViewsValid (value: string) {
64 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
65 }
66
67 function isVideoRatingTypeValid (value: string) {
68 return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
69 }
70
71 const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
72 const videoFileTypesRegex = videoFileTypes.join('|')
73 function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
74 return isFileValid(files, videoFileTypesRegex, 'videofile')
75 }
76
77 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
78 .map(v => v.replace('.', ''))
79 .join('|')
80 const videoImageTypesRegex = `image/(${videoImageTypes})`
81 function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
82 return isFileValid(files, videoImageTypesRegex, field, true)
83 }
84
85 function isVideoPrivacyValid (value: string) {
86 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined
87 }
88
89 function isVideoFileInfoHashValid (value: string) {
90 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
91 }
92
93 function isVideoFileResolutionValid (value: string) {
94 return exists(value) && validator.isInt(value + '')
95 }
96
97 function isVideoFileSizeValid (value: string) {
98 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
99 }
100
101 async function isVideoExist (id: string, res: Response) {
102 let video: VideoModel
103
104 if (validator.isInt(id)) {
105 video = await VideoModel.loadAndPopulateAccountAndServerAndTags(+id)
106 } else { // UUID
107 video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(id)
108 }
109
110 if (!video) {
111 res.status(404)
112 .json({ error: 'Video not found' })
113 .end()
114
115 return false
116 }
117
118 res.locals.video = video
119 return true
120 }
121
122 // ---------------------------------------------------------------------------
123
124 export {
125 isVideoCategoryValid,
126 isVideoLicenceValid,
127 isVideoLanguageValid,
128 isVideoTruncatedDescriptionValid,
129 isVideoDescriptionValid,
130 isVideoFileInfoHashValid,
131 isVideoNameValid,
132 isVideoTagsValid,
133 isVideoAbuseReasonValid,
134 isVideoFile,
135 isVideoViewsValid,
136 isVideoRatingTypeValid,
137 isVideoDurationValid,
138 isVideoTagValid,
139 isVideoPrivacyValid,
140 isVideoFileResolutionValid,
141 isVideoFileSizeValid,
142 isVideoExist,
143 isVideoImage
144 }