aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/editor.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos/editor.ts')
-rw-r--r--server/controllers/api/videos/editor.ts120
1 files changed, 120 insertions, 0 deletions
diff --git a/server/controllers/api/videos/editor.ts b/server/controllers/api/videos/editor.ts
new file mode 100644
index 000000000..61e2eb5da
--- /dev/null
+++ b/server/controllers/api/videos/editor.ts
@@ -0,0 +1,120 @@
1import express from 'express'
2import { createAnyReqFiles } from '@server/helpers/express-utils'
3import { CONFIG } from '@server/initializers/config'
4import { MIMETYPES } from '@server/initializers/constants'
5import { JobQueue } from '@server/lib/job-queue'
6import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-editor'
7import {
8 HttpStatusCode,
9 VideoEditionTaskPayload,
10 VideoEditorCreateEdition,
11 VideoEditorTask,
12 VideoEditorTaskCut,
13 VideoEditorTaskIntro,
14 VideoEditorTaskOutro,
15 VideoEditorTaskWatermark,
16 VideoState
17} from '@shared/models'
18import { asyncMiddleware, authenticate, videosEditorAddEditionValidator } from '../../../middlewares'
19
20const editorRouter = express.Router()
21
22const tasksFiles = createAnyReqFiles(
23 MIMETYPES.VIDEO.MIMETYPE_EXT,
24 CONFIG.STORAGE.TMP_DIR,
25 (req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
26 const body = req.body as VideoEditorCreateEdition
27
28 // Fetch array element
29 const matches = file.fieldname.match(/tasks\[(\d+)\]/)
30 if (!matches) return cb(new Error('Cannot find array element indice for ' + file.fieldname))
31
32 const indice = parseInt(matches[1])
33 const task = body.tasks[indice]
34
35 if (!task) return cb(new Error('Cannot find array element of indice ' + indice + ' for ' + file.fieldname))
36
37 if (
38 [ 'add-intro', 'add-outro', 'add-watermark' ].includes(task.name) &&
39 file.fieldname === buildTaskFileFieldname(indice)
40 ) {
41 return cb(null, true)
42 }
43
44 return cb(null, false)
45 }
46)
47
48editorRouter.post('/:videoId/editor/edit',
49 authenticate,
50 tasksFiles,
51 asyncMiddleware(videosEditorAddEditionValidator),
52 asyncMiddleware(createEditionTasks)
53)
54
55// ---------------------------------------------------------------------------
56
57export {
58 editorRouter
59}
60
61// ---------------------------------------------------------------------------
62
63async function createEditionTasks (req: express.Request, res: express.Response) {
64 const files = req.files as Express.Multer.File[]
65 const body = req.body as VideoEditorCreateEdition
66 const video = res.locals.videoAll
67
68 video.state = VideoState.TO_EDIT
69 await video.save()
70
71 const payload = {
72 videoUUID: video.uuid,
73 tasks: body.tasks.map((t, i) => buildTaskPayload(t, i, files))
74 }
75
76 JobQueue.Instance.createJob({ type: 'video-edition', payload })
77
78 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
79}
80
81const taskPayloadBuilders: {
82 [id in VideoEditorTask['name']]: (task: VideoEditorTask, indice?: number, files?: Express.Multer.File[]) => VideoEditionTaskPayload
83} = {
84 'add-intro': buildIntroOutroTask,
85 'add-outro': buildIntroOutroTask,
86 'cut': buildCutTask,
87 'add-watermark': buildWatermarkTask
88}
89
90function buildTaskPayload (task: VideoEditorTask, indice: number, files: Express.Multer.File[]): VideoEditionTaskPayload {
91 return taskPayloadBuilders[task.name](task, indice, files)
92}
93
94function buildIntroOutroTask (task: VideoEditorTaskIntro | VideoEditorTaskOutro, indice: number, files: Express.Multer.File[]) {
95 return {
96 name: task.name,
97 options: {
98 file: getTaskFile(files, indice).path
99 }
100 }
101}
102
103function buildCutTask (task: VideoEditorTaskCut) {
104 return {
105 name: task.name,
106 options: {
107 start: task.options.start,
108 end: task.options.end
109 }
110 }
111}
112
113function buildWatermarkTask (task: VideoEditorTaskWatermark, indice: number, files: Express.Multer.File[]) {
114 return {
115 name: task.name,
116 options: {
117 file: getTaskFile(files, indice).path
118 }
119 }
120}