aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos/video-source.ts
blob: 31a2f16b379a5953db9cd2e5223349b0041c5734 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import express from 'express'
import { getVideoWithAttributes } from '@server/helpers/video'
import { VideoSourceModel } from '@server/models/video/video-source'
import { MVideoFullLight } from '@server/types/models'
import { HttpStatusCode, UserRight } from '@shared/models'
import { logger } from '../../../helpers/logger'
import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'

const videoSourceGetValidator = [
  isValidVideoIdParam('id'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videoSourceGet parameters', { parameters: req.params })

    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(req.params.id, res, 'for-api')) return

    const video = getVideoWithAttributes(res) as MVideoFullLight

    res.locals.videoSource = await VideoSourceModel.loadByVideoId(video.id)
    if (!res.locals.videoSource) {
      return res.fail({
        status: HttpStatusCode.NOT_FOUND_404,
        message: 'Video source not found'
      })
    }

    const user = res.locals.oauth.token.User
    if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return

    return next()
  }
]

export {
  videoSourceGetValidator
}