]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/studio.ts
Use bullmq job dependency
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / studio.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-studio'
6 import {
7 HttpStatusCode,
8 VideoState,
9 VideoStudioCreateEdition,
10 VideoStudioTask,
11 VideoStudioTaskCut,
12 VideoStudioTaskIntro,
13 VideoStudioTaskOutro,
14 VideoStudioTaskPayload,
15 VideoStudioTaskWatermark
16 } from '@shared/models'
17 import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares'
18
19 const studioRouter = 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 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
46 studioRouter.post('/:videoId/studio/edit',
47 authenticate,
48 tasksFiles,
49 asyncMiddleware(videoStudioAddEditionValidator),
50 asyncMiddleware(createEditionTasks)
51 )
52
53 // ---------------------------------------------------------------------------
54
55 export {
56 studioRouter
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 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.createJobAsync({ type: 'video-studio-edition', payload })
75
76 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
77 }
78
79 const 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
88 function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): VideoStudioTaskPayload {
89 return taskPayloadBuilders[task.name](task, indice, files)
90 }
91
92 function 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
101 function 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
111 function 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 }