]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-source.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-source.ts
CommitLineData
2e401e85 1import express from 'express'
2import { getVideoWithAttributes } from '@server/helpers/video'
3import { VideoSourceModel } from '@server/models/video/video-source'
4import { MVideoFullLight } from '@server/types/models'
5import { HttpStatusCode, UserRight } from '@shared/models'
2e401e85 6import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
7
8const videoSourceGetValidator = [
9 isValidVideoIdParam('id'),
10
11 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2e401e85 12 if (areValidationErrors(req, res)) return
13 if (!await doesVideoExist(req.params.id, res, 'for-api')) return
14
15 const video = getVideoWithAttributes(res) as MVideoFullLight
16
17 res.locals.videoSource = await VideoSourceModel.loadByVideoId(video.id)
18 if (!res.locals.videoSource) {
19 return res.fail({
20 status: HttpStatusCode.NOT_FOUND_404,
21 message: 'Video source not found'
22 })
23 }
24
25 const user = res.locals.oauth.token.User
26 if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return
27
28 return next()
29 }
30]
31
32export {
33 videoSourceGetValidator
34}