aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/studio.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos/studio.ts')
-rw-r--r--server/controllers/api/videos/studio.ts118
1 files changed, 118 insertions, 0 deletions
diff --git a/server/controllers/api/videos/studio.ts b/server/controllers/api/videos/studio.ts
new file mode 100644
index 000000000..bff344f3f
--- /dev/null
+++ b/server/controllers/api/videos/studio.ts
@@ -0,0 +1,118 @@
1import express from 'express'
2import { createAnyReqFiles } from '@server/helpers/express-utils'
3import { MIMETYPES } from '@server/initializers/constants'
4import { JobQueue } from '@server/lib/job-queue'
5import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-studio'
6import {
7 HttpStatusCode,
8 VideoState,
9 VideoStudioCreateEdition,
10 VideoStudioTask,
11 VideoStudioTaskCut,
12 VideoStudioTaskIntro,
13 VideoStudioTaskOutro,
14 VideoStudioTaskPayload,
15 VideoStudioTaskWatermark
16} from '@shared/models'
17import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares'
18
19const studioRouter = express.Router()
20
21const 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 VideoStudioCreateEdition
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
46studioRouter.post('/:videoId/studio/edit',
47 authenticate,
48 tasksFiles,
49 asyncMiddleware(videoStudioAddEditionValidator),
50 asyncMiddleware(createEditionTasks)
51)
52
53// ---------------------------------------------------------------------------
54
55export {
56 studioRouter
57}
58
59// ---------------------------------------------------------------------------
60
61async function createEditionTasks (req: express.Request, res: express.Response) {
62 const files = req.files as Express.Multer.File[]
63 const body = req.body as VideoStudioCreateEdition
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-studio-edition', payload })
75
76 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
77}
78
79const taskPayloadBuilders: {
80 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => VideoStudioTaskPayload
81} = {
82 'add-intro': buildIntroOutroTask,
83 'add-outro': buildIntroOutroTask,
84 'cut': buildCutTask,
85 'add-watermark': buildWatermarkTask
86}
87
88function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): VideoStudioTaskPayload {
89 return taskPayloadBuilders[task.name](task, indice, files)
90}
91
92function buildIntroOutroTask (task: VideoStudioTaskIntro | VideoStudioTaskOutro, indice: number, files: Express.Multer.File[]) {
93 return {
94 name: task.name,
95 options: {
96 file: getTaskFile(files, indice).path
97 }
98 }
99}
100
101function buildCutTask (task: VideoStudioTaskCut) {
102 return {
103 name: task.name,
104 options: {
105 start: task.options.start,
106 end: task.options.end
107 }
108 }
109}
110
111function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
112 return {
113 name: task.name,
114 options: {
115 file: getTaskFile(files, indice).path
116 }
117 }
118}