diff options
author | Chocobozzz <me@florianbigard.com> | 2022-02-11 10:51:33 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-02-28 10:42:19 +0100 |
commit | c729caf6cc34630877a0e5a1bda1719384cd0c8a (patch) | |
tree | 1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/middlewares/validators/videos/video-editor.ts | |
parent | a24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff) | |
download | PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip |
Add basic video editor support
Diffstat (limited to 'server/middlewares/validators/videos/video-editor.ts')
-rw-r--r-- | server/middlewares/validators/videos/video-editor.ts | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/server/middlewares/validators/videos/video-editor.ts b/server/middlewares/validators/videos/video-editor.ts new file mode 100644 index 000000000..9be97be93 --- /dev/null +++ b/server/middlewares/validators/videos/video-editor.ts | |||
@@ -0,0 +1,112 @@ | |||
1 | import express from 'express' | ||
2 | import { body, param } from 'express-validator' | ||
3 | import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc' | ||
4 | import { | ||
5 | isEditorCutTaskValid, | ||
6 | isEditorTaskAddIntroOutroValid, | ||
7 | isEditorTaskAddWatermarkValid, | ||
8 | isValidEditorTasksArray | ||
9 | } from '@server/helpers/custom-validators/video-editor' | ||
10 | import { cleanUpReqFiles } from '@server/helpers/express-utils' | ||
11 | import { CONFIG } from '@server/initializers/config' | ||
12 | import { approximateIntroOutroAdditionalSize, getTaskFile } from '@server/lib/video-editor' | ||
13 | import { isAudioFile } from '@shared/extra-utils' | ||
14 | import { HttpStatusCode, UserRight, VideoEditorCreateEdition, VideoEditorTask, VideoState } from '@shared/models' | ||
15 | import { logger } from '../../../helpers/logger' | ||
16 | import { areValidationErrors, checkUserCanManageVideo, checkUserQuota, doesVideoExist } from '../shared' | ||
17 | |||
18 | const videosEditorAddEditionValidator = [ | ||
19 | param('videoId').custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid'), | ||
20 | |||
21 | body('tasks').custom(isValidEditorTasksArray).withMessage('Should have a valid array of tasks'), | ||
22 | |||
23 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
24 | logger.debug('Checking videosEditorAddEditionValidator parameters.', { parameters: req.params, body: req.body, files: req.files }) | ||
25 | |||
26 | if (CONFIG.VIDEO_EDITOR.ENABLED !== true) { | ||
27 | res.fail({ | ||
28 | status: HttpStatusCode.BAD_REQUEST_400, | ||
29 | message: 'Video editor 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: VideoEditorCreateEdition = 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 = getTaskFile(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 (video.state === VideoState.TO_TRANSCODE || video.state === VideoState.TO_EDIT) { | ||
71 | res.fail({ | ||
72 | status: HttpStatusCode.CONFLICT_409, | ||
73 | message: 'Cannot edit video that is already waiting for transcoding/edition' | ||
74 | }) | ||
75 | |||
76 | return cleanUpReqFiles(req) | ||
77 | } | ||
78 | |||
79 | const user = res.locals.oauth.token.User | ||
80 | if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req) | ||
81 | |||
82 | // Try to make an approximation of bytes added by the intro/outro | ||
83 | const additionalBytes = await approximateIntroOutroAdditionalSize(video, body.tasks, i => getTaskFile(files, i).path) | ||
84 | if (await checkUserQuota(user, additionalBytes, res) === false) return cleanUpReqFiles(req) | ||
85 | |||
86 | return next() | ||
87 | } | ||
88 | ] | ||
89 | |||
90 | // --------------------------------------------------------------------------- | ||
91 | |||
92 | export { | ||
93 | videosEditorAddEditionValidator | ||
94 | } | ||
95 | |||
96 | // --------------------------------------------------------------------------- | ||
97 | |||
98 | const taskCheckers: { | ||
99 | [id in VideoEditorTask['name']]: (task: VideoEditorTask, indice?: number, files?: Express.Multer.File[]) => boolean | ||
100 | } = { | ||
101 | 'cut': isEditorCutTaskValid, | ||
102 | 'add-intro': isEditorTaskAddIntroOutroValid, | ||
103 | 'add-outro': isEditorTaskAddIntroOutroValid, | ||
104 | 'add-watermark': isEditorTaskAddWatermarkValid | ||
105 | } | ||
106 | |||
107 | function checkTask (req: express.Request, task: VideoEditorTask, indice?: number) { | ||
108 | const checker = taskCheckers[task.name] | ||
109 | if (!checker) return false | ||
110 | |||
111 | return checker(task, indice, req.files as Express.Multer.File[]) | ||
112 | } | ||