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'
9 VideoStudioCreateEdition,
14 VideoStudioTaskPayload,
15 VideoStudioTaskWatermark
16 } from '@shared/models'
17 import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares'
19 const studioRouter = express.Router()
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
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))
30 const indice = parseInt(matches[1])
31 const task = body.tasks[indice]
33 if (!task) return cb(new Error('Cannot find array element of indice ' + indice + ' for ' + file.fieldname))
36 [ 'add-intro', 'add-outro', 'add-watermark' ].includes(task.name) &&
37 file.fieldname === buildTaskFileFieldname(indice)
42 return cb(null, false)
46 studioRouter.post('/:videoId/studio/edit',
49 asyncMiddleware(videoStudioAddEditionValidator),
50 asyncMiddleware(createEditionTasks)
53 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
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
66 video.state = VideoState.TO_EDIT
70 videoUUID: video.uuid,
71 tasks: body.tasks.map((t, i) => buildTaskPayload(t, i, files))
74 JobQueue.Instance.createJobAsync({ type: 'video-studio-edition', payload })
76 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
79 const taskPayloadBuilders: {
80 [id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => VideoStudioTaskPayload
82 'add-intro': buildIntroOutroTask,
83 'add-outro': buildIntroOutroTask,
85 'add-watermark': buildWatermarkTask
88 function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): VideoStudioTaskPayload {
89 return taskPayloadBuilders[task.name](task, indice, files)
92 function buildIntroOutroTask (task: VideoStudioTaskIntro | VideoStudioTaskOutro, indice: number, files: Express.Multer.File[]) {
96 file: getTaskFile(files, indice).path
101 function buildCutTask (task: VideoStudioTaskCut) {
105 start: task.options.start,
106 end: task.options.end
111 function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
115 file: getTaskFile(files, indice).path