aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/videos.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-23 10:40:39 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commit3e753302d8c911b59971c16a8018df0e1ab78465 (patch)
treeefce7ece3273589228c5c948ea6757b2bdf65429 /server/helpers/custom-validators/videos.ts
parenta8b666e9f1ed002230869606308749614390c82f (diff)
downloadPeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.gz
PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.zst
PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.zip
Refactor middleware helpers
Diffstat (limited to 'server/helpers/custom-validators/videos.ts')
-rw-r--r--server/helpers/custom-validators/videos.ts73
1 files changed, 0 insertions, 73 deletions
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 214db17a1..157e1a8e3 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -15,10 +15,8 @@ import {
15} from '../../initializers/constants' 15} from '../../initializers/constants'
16import { VideoModel } from '../../models/video/video' 16import { VideoModel } from '../../models/video/video'
17import { exists, isArray, isDateValid, isFileValid } from './misc' 17import { exists, isArray, isDateValid, isFileValid } from './misc'
18import { VideoChannelModel } from '../../models/video/video-channel'
19import { UserModel } from '../../models/account/user' 18import { UserModel } from '../../models/account/user'
20import * as magnetUtil from 'magnet-uri' 19import * as magnetUtil from 'magnet-uri'
21import { fetchVideo, VideoFetchType } from '../video'
22 20
23const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 21const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
24 22
@@ -143,79 +141,10 @@ function isVideoMagnetUriValid (value: string) {
143 return parsed && isVideoFileInfoHashValid(parsed.infoHash) 141 return parsed && isVideoFileInfoHashValid(parsed.infoHash)
144} 142}
145 143
146function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
147 // Retrieve the user who did the request
148 if (video.isOwned() === false) {
149 res.status(403)
150 .json({ error: 'Cannot manage a video of another server.' })
151 .end()
152 return false
153 }
154
155 // Check if the user can delete the video
156 // The user can delete it if he has the right
157 // Or if s/he is the video's account
158 const account = video.VideoChannel.Account
159 if (user.hasRight(right) === false && account.userId !== user.id) {
160 res.status(403)
161 .json({ error: 'Cannot manage a video of another user.' })
162 .end()
163 return false
164 }
165
166 return true
167}
168
169async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
170 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
171
172 const video = await fetchVideo(id, fetchType, userId)
173
174 if (video === null) {
175 res.status(404)
176 .json({ error: 'Video not found' })
177 .end()
178
179 return false
180 }
181
182 if (fetchType !== 'none') res.locals.video = video
183 return true
184}
185
186async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
187 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
188 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
189 if (videoChannel === null) {
190 res.status(400)
191 .json({ error: 'Unknown video `video channel` on this instance.' })
192 .end()
193
194 return false
195 }
196
197 res.locals.videoChannel = videoChannel
198 return true
199 }
200
201 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
202 if (videoChannel === null) {
203 res.status(400)
204 .json({ error: 'Unknown video `video channel` for this account.' })
205 .end()
206
207 return false
208 }
209
210 res.locals.videoChannel = videoChannel
211 return true
212}
213
214// --------------------------------------------------------------------------- 144// ---------------------------------------------------------------------------
215 145
216export { 146export {
217 isVideoCategoryValid, 147 isVideoCategoryValid,
218 checkUserCanManageVideo,
219 isVideoLicenceValid, 148 isVideoLicenceValid,
220 isVideoLanguageValid, 149 isVideoLanguageValid,
221 isVideoTruncatedDescriptionValid, 150 isVideoTruncatedDescriptionValid,
@@ -237,9 +166,7 @@ export {
237 isVideoPrivacyValid, 166 isVideoPrivacyValid,
238 isVideoFileResolutionValid, 167 isVideoFileResolutionValid,
239 isVideoFileSizeValid, 168 isVideoFileSizeValid,
240 doesVideoExist,
241 isVideoImage, 169 isVideoImage,
242 doesVideoChannelOfAccountExist,
243 isVideoSupportValid, 170 isVideoSupportValid,
244 isVideoFilterValid 171 isVideoFilterValid
245} 172}