1
0
Fork 0
peertube/server/controllers/api/videos/studio.ts

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-02-11 04:51:33 -05:00
import express from 'express'
import { createAnyReqFiles } from '@server/helpers/express-utils'
import { MIMETYPES } from '@server/initializers/constants'
import { JobQueue } from '@server/lib/job-queue'
2022-03-22 11:58:49 -04:00
import { buildTaskFileFieldname, getTaskFile } from '@server/lib/video-studio'
2022-02-11 04:51:33 -05:00
import {
HttpStatusCode,
2022-03-22 11:58:49 -04:00
VideoState,
VideoStudioCreateEdition,
VideoStudioTask,
VideoStudioTaskCut,
VideoStudioTaskIntro,
VideoStudioTaskOutro,
VideoStudioTaskPayload,
VideoStudioTaskWatermark
2022-02-11 04:51:33 -05:00
} from '@shared/models'
2022-03-22 11:58:49 -04:00
import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares'
2022-02-11 04:51:33 -05:00
2022-03-22 11:58:49 -04:00
const studioRouter = express.Router()
2022-02-11 04:51:33 -05:00
const tasksFiles = createAnyReqFiles(
MIMETYPES.VIDEO.MIMETYPE_EXT,
(req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
2022-03-22 11:58:49 -04:00
const body = req.body as VideoStudioCreateEdition
2022-02-11 04:51:33 -05:00
// Fetch array element
const matches = file.fieldname.match(/tasks\[(\d+)\]/)
if (!matches) return cb(new Error('Cannot find array element indice for ' + file.fieldname))
const indice = parseInt(matches[1])
const task = body.tasks[indice]
if (!task) return cb(new Error('Cannot find array element of indice ' + indice + ' for ' + file.fieldname))
if (
[ 'add-intro', 'add-outro', 'add-watermark' ].includes(task.name) &&
file.fieldname === buildTaskFileFieldname(indice)
) {
return cb(null, true)
}
return cb(null, false)
}
)
2022-03-22 11:58:49 -04:00
studioRouter.post('/:videoId/studio/edit',
2022-02-11 04:51:33 -05:00
authenticate,
tasksFiles,
2022-03-22 11:58:49 -04:00
asyncMiddleware(videoStudioAddEditionValidator),
2022-02-11 04:51:33 -05:00
asyncMiddleware(createEditionTasks)
)
// ---------------------------------------------------------------------------
export {
2022-03-22 11:58:49 -04:00
studioRouter
2022-02-11 04:51:33 -05:00
}
// ---------------------------------------------------------------------------
async function createEditionTasks (req: express.Request, res: express.Response) {
const files = req.files as Express.Multer.File[]
2022-03-22 11:58:49 -04:00
const body = req.body as VideoStudioCreateEdition
2022-02-11 04:51:33 -05:00
const video = res.locals.videoAll
video.state = VideoState.TO_EDIT
await video.save()
const payload = {
videoUUID: video.uuid,
tasks: body.tasks.map((t, i) => buildTaskPayload(t, i, files))
}
2022-08-08 09:48:17 -04:00
JobQueue.Instance.createJobAsync({ type: 'video-studio-edition', payload })
2022-02-11 04:51:33 -05:00
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
}
const taskPayloadBuilders: {
2022-03-22 11:58:49 -04:00
[id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => VideoStudioTaskPayload
2022-02-11 04:51:33 -05:00
} = {
'add-intro': buildIntroOutroTask,
'add-outro': buildIntroOutroTask,
'cut': buildCutTask,
'add-watermark': buildWatermarkTask
}
2022-03-22 11:58:49 -04:00
function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): VideoStudioTaskPayload {
2022-02-11 04:51:33 -05:00
return taskPayloadBuilders[task.name](task, indice, files)
}
2022-03-22 11:58:49 -04:00
function buildIntroOutroTask (task: VideoStudioTaskIntro | VideoStudioTaskOutro, indice: number, files: Express.Multer.File[]) {
2022-02-11 04:51:33 -05:00
return {
name: task.name,
options: {
file: getTaskFile(files, indice).path
}
}
}
2022-03-22 11:58:49 -04:00
function buildCutTask (task: VideoStudioTaskCut) {
2022-02-11 04:51:33 -05:00
return {
name: task.name,
options: {
start: task.options.start,
end: task.options.end
}
}
}
2022-03-22 11:58:49 -04:00
function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
2022-02-11 04:51:33 -05:00
return {
name: task.name,
options: {
file: getTaskFile(files, indice).path
}
}
}