aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos/video-transcoding.ts
blob: 2f99ff42c214b7e5243c477d1b594e0fdc8b3a9b (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import express from 'express'
import { body } from 'express-validator'
import { isBooleanValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc'
import { isValidCreateTranscodingType } from '@server/helpers/custom-validators/video-transcoding'
import { CONFIG } from '@server/initializers/config'
import { VideoJobInfoModel } from '@server/models/video/video-job-info'
import { HttpStatusCode, ServerErrorCode, VideoTranscodingCreate } from '@shared/models'
import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'

const createTranscodingValidator = [
  isValidVideoIdParam('videoId'),

  body('transcodingType')
    .custom(isValidCreateTranscodingType),

  body('forceTranscoding')
    .optional()
    .customSanitizer(toBooleanOrNull)
    .custom(isBooleanValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return
    if (!await doesVideoExist(req.params.videoId, res, 'all')) return

    const video = res.locals.videoAll

    if (video.remote) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot run transcoding job on a remote video'
      })
    }

    if (CONFIG.TRANSCODING.ENABLED !== true) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot run transcoding job because transcoding is disabled on this instance'
      })
    }

    const body = req.body as VideoTranscodingCreate
    if (body.forceTranscoding === true) return next()

    const info = await VideoJobInfoModel.load(video.id)
    if (info && info.pendingTranscode > 0) {
      return res.fail({
        status: HttpStatusCode.CONFLICT_409,
        type: ServerErrorCode.VIDEO_ALREADY_BEING_TRANSCODED,
        message: 'This video is already being transcoded'
      })
    }

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  createTranscodingValidator
}