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

139 lines
4 KiB
TypeScript
Raw Normal View History

import Bluebird from 'bluebird'
2022-02-11 04:51:33 -05:00
import express from 'express'
import { move } from 'fs-extra'
import { basename, join } from 'path'
2022-02-11 04:51:33 -05:00
import { createAnyReqFiles } from '@server/helpers/express-utils'
import { CONFIG } from '@server/initializers/config'
2022-02-11 04:51:33 -05:00
import { MIMETYPES } from '@server/initializers/constants'
import { JobQueue } from '@server/lib/job-queue'
import { buildTaskFileFieldname, getTaskFileFromReq } 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: await Bluebird.mapSeries(body.tasks, (t, i) => buildTaskPayload(t, i, files))
2022-02-11 04:51:33 -05:00
}
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: {
[id in VideoStudioTask['name']]: (
task: VideoStudioTask,
indice?: number,
files?: Express.Multer.File[]
) => Promise<VideoStudioTaskPayload>
2022-02-11 04:51:33 -05:00
} = {
'add-intro': buildIntroOutroTask,
'add-outro': buildIntroOutroTask,
'cut': buildCutTask,
'add-watermark': buildWatermarkTask
}
function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): Promise<VideoStudioTaskPayload> {
2022-02-11 04:51:33 -05:00
return taskPayloadBuilders[task.name](task, indice, files)
}
async function buildIntroOutroTask (task: VideoStudioTaskIntro | VideoStudioTaskOutro, indice: number, files: Express.Multer.File[]) {
const destination = await moveStudioFileToPersistentTMP(getTaskFileFromReq(files, indice).path)
2022-02-11 04:51:33 -05:00
return {
name: task.name,
options: {
file: destination
2022-02-11 04:51:33 -05:00
}
}
}
2022-03-22 11:58:49 -04:00
function buildCutTask (task: VideoStudioTaskCut) {
return Promise.resolve({
2022-02-11 04:51:33 -05:00
name: task.name,
options: {
start: task.options.start,
end: task.options.end
}
})
2022-02-11 04:51:33 -05:00
}
async function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
const destination = await moveStudioFileToPersistentTMP(getTaskFileFromReq(files, indice).path)
2022-02-11 04:51:33 -05:00
return {
name: task.name,
options: {
file: destination
2022-02-11 04:51:33 -05:00
}
}
}
async function moveStudioFileToPersistentTMP (file: string) {
const destination = join(CONFIG.STORAGE.TMP_PERSISTENT_DIR, basename(file))
await move(file, destination)
return destination
}