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