]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-transcoding.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-transcoding.ts
1 import express from 'express'
2 import { body } from 'express-validator'
3 import { isValidCreateTranscodingType } from '@server/helpers/custom-validators/video-transcoding'
4 import { CONFIG } from '@server/initializers/config'
5 import { VideoJobInfoModel } from '@server/models/video/video-job-info'
6 import { HttpStatusCode } from '@shared/models'
7 import { logger } from '../../../helpers/logger'
8 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
9
10 const createTranscodingValidator = [
11 isValidVideoIdParam('videoId'),
12
13 body('transcodingType')
14 .custom(isValidCreateTranscodingType),
15
16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking createTranscodingValidator parameters', { parameters: req.body })
18
19 if (areValidationErrors(req, res)) return
20 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
21
22 const video = res.locals.videoAll
23
24 if (video.remote) {
25 return res.fail({
26 status: HttpStatusCode.BAD_REQUEST_400,
27 message: 'Cannot run transcoding job on a remote video'
28 })
29 }
30
31 if (CONFIG.TRANSCODING.ENABLED !== true) {
32 return res.fail({
33 status: HttpStatusCode.BAD_REQUEST_400,
34 message: 'Cannot run transcoding job because transcoding is disabled on this instance'
35 })
36 }
37
38 // Prefer using job info table instead of video state because before 4.0 failed transcoded video were stuck in "TO_TRANSCODE" state
39 const info = await VideoJobInfoModel.load(video.id)
40 if (info && info.pendingTranscode > 0) {
41 return res.fail({
42 status: HttpStatusCode.CONFLICT_409,
43 message: 'This video is already being transcoded'
44 })
45 }
46
47 return next()
48 }
49 ]
50
51 // ---------------------------------------------------------------------------
52
53 export {
54 createTranscodingValidator
55 }