diff options
Diffstat (limited to 'server/middlewares/validators/videos/video-studio.ts')
-rw-r--r-- | server/middlewares/validators/videos/video-studio.ts | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/server/middlewares/validators/videos/video-studio.ts b/server/middlewares/validators/videos/video-studio.ts deleted file mode 100644 index a375af60a..000000000 --- a/server/middlewares/validators/videos/video-studio.ts +++ /dev/null | |||
@@ -1,105 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { body, param } from 'express-validator' | ||
3 | import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc' | ||
4 | import { | ||
5 | isStudioCutTaskValid, | ||
6 | isStudioTaskAddIntroOutroValid, | ||
7 | isStudioTaskAddWatermarkValid, | ||
8 | isValidStudioTasksArray | ||
9 | } from '@server/helpers/custom-validators/video-studio' | ||
10 | import { cleanUpReqFiles } from '@server/helpers/express-utils' | ||
11 | import { CONFIG } from '@server/initializers/config' | ||
12 | import { approximateIntroOutroAdditionalSize, getTaskFileFromReq } from '@server/lib/video-studio' | ||
13 | import { isAudioFile } from '@shared/ffmpeg' | ||
14 | import { HttpStatusCode, UserRight, VideoStudioCreateEdition, VideoStudioTask } from '@shared/models' | ||
15 | import { areValidationErrors, checkUserCanManageVideo, checkUserQuota, doesVideoExist } from '../shared' | ||
16 | import { checkVideoFileCanBeEdited } from './shared' | ||
17 | |||
18 | const videoStudioAddEditionValidator = [ | ||
19 | param('videoId') | ||
20 | .custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'), | ||
21 | |||
22 | body('tasks') | ||
23 | .custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'), | ||
24 | |||
25 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
26 | if (CONFIG.VIDEO_STUDIO.ENABLED !== true) { | ||
27 | res.fail({ | ||
28 | status: HttpStatusCode.BAD_REQUEST_400, | ||
29 | message: 'Video studio is disabled on this instance' | ||
30 | }) | ||
31 | |||
32 | return cleanUpReqFiles(req) | ||
33 | } | ||
34 | |||
35 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | ||
36 | |||
37 | const body: VideoStudioCreateEdition = req.body | ||
38 | const files = req.files as Express.Multer.File[] | ||
39 | |||
40 | for (let i = 0; i < body.tasks.length; i++) { | ||
41 | const task = body.tasks[i] | ||
42 | |||
43 | if (!checkTask(req, task, i)) { | ||
44 | res.fail({ | ||
45 | status: HttpStatusCode.BAD_REQUEST_400, | ||
46 | message: `Task ${task.name} is invalid` | ||
47 | }) | ||
48 | |||
49 | return cleanUpReqFiles(req) | ||
50 | } | ||
51 | |||
52 | if (task.name === 'add-intro' || task.name === 'add-outro') { | ||
53 | const filePath = getTaskFileFromReq(files, i).path | ||
54 | |||
55 | // Our concat filter needs a video stream | ||
56 | if (await isAudioFile(filePath)) { | ||
57 | res.fail({ | ||
58 | status: HttpStatusCode.BAD_REQUEST_400, | ||
59 | message: `Task ${task.name} is invalid: file does not contain a video stream` | ||
60 | }) | ||
61 | |||
62 | return cleanUpReqFiles(req) | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req) | ||
68 | |||
69 | const video = res.locals.videoAll | ||
70 | if (!checkVideoFileCanBeEdited(video, res)) return cleanUpReqFiles(req) | ||
71 | |||
72 | const user = res.locals.oauth.token.User | ||
73 | if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req) | ||
74 | |||
75 | // Try to make an approximation of bytes added by the intro/outro | ||
76 | const additionalBytes = await approximateIntroOutroAdditionalSize(video, body.tasks, i => getTaskFileFromReq(files, i).path) | ||
77 | if (await checkUserQuota(user, additionalBytes, res) === false) return cleanUpReqFiles(req) | ||
78 | |||
79 | return next() | ||
80 | } | ||
81 | ] | ||
82 | |||
83 | // --------------------------------------------------------------------------- | ||
84 | |||
85 | export { | ||
86 | videoStudioAddEditionValidator | ||
87 | } | ||
88 | |||
89 | // --------------------------------------------------------------------------- | ||
90 | |||
91 | const taskCheckers: { | ||
92 | [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean | ||
93 | } = { | ||
94 | 'cut': isStudioCutTaskValid, | ||
95 | 'add-intro': isStudioTaskAddIntroOutroValid, | ||
96 | 'add-outro': isStudioTaskAddIntroOutroValid, | ||
97 | 'add-watermark': isStudioTaskAddWatermarkValid | ||
98 | } | ||
99 | |||
100 | function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) { | ||
101 | const checker = taskCheckers[task.name] | ||
102 | if (!checker) return false | ||
103 | |||
104 | return checker(task, indice, req.files as Express.Multer.File[]) | ||
105 | } | ||