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