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 { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
9 const createTranscodingValidator = [
10 isValidVideoIdParam('videoId'),
12 body('transcodingType')
13 .custom(isValidCreateTranscodingType),
15 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
16 if (areValidationErrors(req, res)) return
17 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
19 const video = res.locals.videoAll
23 status: HttpStatusCode.BAD_REQUEST_400,
24 message: 'Cannot run transcoding job on a remote video'
28 if (CONFIG.TRANSCODING.ENABLED !== true) {
30 status: HttpStatusCode.BAD_REQUEST_400,
31 message: 'Cannot run transcoding job because transcoding is disabled on this instance'
35 // Prefer using job info table instead of video state because before 4.0 failed transcoded video were stuck in "TO_TRANSCODE" state
36 const info = await VideoJobInfoModel.load(video.id)
37 if (info && info.pendingTranscode > 0) {
39 status: HttpStatusCode.CONFLICT_409,
40 message: 'This video is already being transcoded'
48 // ---------------------------------------------------------------------------
51 createTranscodingValidator