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'
10 const createTranscodingValidator = [
11 isValidVideoIdParam('videoId'),
13 body('transcodingType')
14 .custom(isValidCreateTranscodingType).withMessage('Should have a valid transcoding type'),
16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking createTranscodingValidator parameters', { parameters: req.body })
19 if (areValidationErrors(req, res)) return
20 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
22 const video = res.locals.videoAll
26 status: HttpStatusCode.BAD_REQUEST_400,
27 message: 'Cannot run transcoding job on a remote video'
31 if (CONFIG.TRANSCODING.ENABLED !== true) {
33 status: HttpStatusCode.BAD_REQUEST_400,
34 message: 'Cannot run transcoding job because transcoding is disabled on this instance'
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) {
42 status: HttpStatusCode.CONFLICT_409,
43 message: 'This video is already being transcoded'
51 // ---------------------------------------------------------------------------
54 createTranscodingValidator