aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-11-18 14:35:08 +0100
committerChocobozzz <me@florianbigard.com>2021-11-18 15:20:57 +0100
commitad5db1044c8599eaaaa2a578b350777ae996b068 (patch)
tree3e003cccf021152405d49b21c6c91b703c8ae96c /server/middlewares/validators/videos
parentb46cf4b920984492df598c1b61179acfc7f6f22e (diff)
downloadPeerTube-ad5db1044c8599eaaaa2a578b350777ae996b068.tar.gz
PeerTube-ad5db1044c8599eaaaa2a578b350777ae996b068.tar.zst
PeerTube-ad5db1044c8599eaaaa2a578b350777ae996b068.zip
Add ability to run transcoding jobs
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r--server/middlewares/validators/videos/index.ts1
-rw-r--r--server/middlewares/validators/videos/video-files.ts21
-rw-r--r--server/middlewares/validators/videos/video-transcoding.ts55
3 files changed, 58 insertions, 19 deletions
diff --git a/server/middlewares/validators/videos/index.ts b/server/middlewares/validators/videos/index.ts
index fd1d58093..f365d8ee1 100644
--- a/server/middlewares/validators/videos/index.ts
+++ b/server/middlewares/validators/videos/index.ts
@@ -9,4 +9,5 @@ export * from './video-ownership-changes'
9export * from './video-watch' 9export * from './video-watch'
10export * from './video-rates' 10export * from './video-rates'
11export * from './video-shares' 11export * from './video-shares'
12export * from './video-transcoding'
12export * from './videos' 13export * from './videos'
diff --git a/server/middlewares/validators/videos/video-files.ts b/server/middlewares/validators/videos/video-files.ts
index 282594ab6..c1fa77502 100644
--- a/server/middlewares/validators/videos/video-files.ts
+++ b/server/middlewares/validators/videos/video-files.ts
@@ -1,6 +1,6 @@
1import express from 'express' 1import express from 'express'
2import { MUser, MVideo } from '@server/types/models' 2import { MVideo } from '@server/types/models'
3import { HttpStatusCode, UserRight } from '../../../../shared' 3import { HttpStatusCode } from '../../../../shared'
4import { logger } from '../../../helpers/logger' 4import { logger } from '../../../helpers/logger'
5import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' 5import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
6 6
@@ -14,9 +14,7 @@ const videoFilesDeleteWebTorrentValidator = [
14 if (!await doesVideoExist(req.params.id, res)) return 14 if (!await doesVideoExist(req.params.id, res)) return
15 15
16 const video = res.locals.videoAll 16 const video = res.locals.videoAll
17 const user = res.locals.oauth.token.User
18 17
19 if (!checkUserCanDeleteFiles(user, res)) return
20 if (!checkLocalVideo(video, res)) return 18 if (!checkLocalVideo(video, res)) return
21 19
22 if (!video.hasWebTorrentFiles()) { 20 if (!video.hasWebTorrentFiles()) {
@@ -47,9 +45,7 @@ const videoFilesDeleteHLSValidator = [
47 if (!await doesVideoExist(req.params.id, res)) return 45 if (!await doesVideoExist(req.params.id, res)) return
48 46
49 const video = res.locals.videoAll 47 const video = res.locals.videoAll
50 const user = res.locals.oauth.token.User
51 48
52 if (!checkUserCanDeleteFiles(user, res)) return
53 if (!checkLocalVideo(video, res)) return 49 if (!checkLocalVideo(video, res)) return
54 50
55 if (!video.getHLSPlaylist()) { 51 if (!video.getHLSPlaylist()) {
@@ -89,16 +85,3 @@ function checkLocalVideo (video: MVideo, res: express.Response) {
89 85
90 return true 86 return true
91} 87}
92
93function checkUserCanDeleteFiles (user: MUser, res: express.Response) {
94 if (user.hasRight(UserRight.MANAGE_VIDEO_FILES) !== true) {
95 res.fail({
96 status: HttpStatusCode.FORBIDDEN_403,
97 message: 'User cannot update video files'
98 })
99
100 return false
101 }
102
103 return true
104}
diff --git a/server/middlewares/validators/videos/video-transcoding.ts b/server/middlewares/validators/videos/video-transcoding.ts
new file mode 100644
index 000000000..34f231d45
--- /dev/null
+++ b/server/middlewares/validators/videos/video-transcoding.ts
@@ -0,0 +1,55 @@
1import express from 'express'
2import { body } from 'express-validator'
3import { isValidCreateTranscodingType } from '@server/helpers/custom-validators/video-transcoding'
4import { CONFIG } from '@server/initializers/config'
5import { VideoJobInfoModel } from '@server/models/video/video-job-info'
6import { HttpStatusCode } from '@shared/models'
7import { logger } from '../../../helpers/logger'
8import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
9
10const createTranscodingValidator = [
11 isValidVideoIdParam('videoId'),
12
13 body('transcodingType')
14 .custom(isValidCreateTranscodingType).withMessage('Should have a valid transcoding type'),
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
53export {
54 createTranscodingValidator
55}