]>
Commit | Line | Data |
---|---|---|
ad5db104 C |
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' | |
ad5db104 C |
7 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' |
8 | ||
9 | const createTranscodingValidator = [ | |
10 | isValidVideoIdParam('videoId'), | |
11 | ||
12 | body('transcodingType') | |
396f6f01 | 13 | .custom(isValidCreateTranscodingType), |
ad5db104 C |
14 | |
15 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
ad5db104 C |
16 | if (areValidationErrors(req, res)) return |
17 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
18 | ||
19 | const video = res.locals.videoAll | |
20 | ||
21 | if (video.remote) { | |
22 | return res.fail({ | |
23 | status: HttpStatusCode.BAD_REQUEST_400, | |
24 | message: 'Cannot run transcoding job on a remote video' | |
25 | }) | |
26 | } | |
27 | ||
28 | if (CONFIG.TRANSCODING.ENABLED !== true) { | |
29 | return res.fail({ | |
30 | status: HttpStatusCode.BAD_REQUEST_400, | |
31 | message: 'Cannot run transcoding job because transcoding is disabled on this instance' | |
32 | }) | |
33 | } | |
34 | ||
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) | |
f6f48301 | 37 | if (info && info.pendingTranscode > 0) { |
ad5db104 C |
38 | return res.fail({ |
39 | status: HttpStatusCode.CONFLICT_409, | |
40 | message: 'This video is already being transcoded' | |
41 | }) | |
42 | } | |
43 | ||
44 | return next() | |
45 | } | |
46 | ] | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | export { | |
51 | createTranscodingValidator | |
52 | } |