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