PeerTube/server/core/controllers/api/videos/studio.ts

144 lines
4.2 KiB
TypeScript
Raw Normal View History

import Bluebird from 'bluebird'
2022-02-11 10:51:33 +01:00
import express from 'express'
import { move } from 'fs-extra/esm'
import { basename } from 'path'
import { createAnyReqFiles } from '@server/helpers/express-utils.js'
import { MIMETYPES, VIDEO_FILTERS } from '@server/initializers/constants.js'
import { buildTaskFileFieldname, createVideoStudioJob, getStudioTaskFilePath, getTaskFileFromReq } from '@server/lib/video-studio.js'
2022-02-11 10:51:33 +01:00
import {
HttpStatusCode,
2022-03-22 16:58:49 +01:00
VideoState,
VideoStudioCreateEdition,
VideoStudioTask,
VideoStudioTaskCut,
VideoStudioTaskIntro,
VideoStudioTaskOutro,
VideoStudioTaskPayload,
VideoStudioTaskWatermark
} from '@peertube/peertube-models'
import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares/index.js'
2022-02-11 10:51:33 +01:00
2022-03-22 16:58:49 +01:00
const studioRouter = express.Router()
2022-02-11 10:51:33 +01:00
const tasksFiles = createAnyReqFiles(
MIMETYPES.VIDEO.MIMETYPE_EXT,
(req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
2022-03-22 16:58:49 +01:00
const body = req.body as VideoStudioCreateEdition
2022-02-11 10:51:33 +01: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 16:58:49 +01:00
studioRouter.post('/:videoId/studio/edit',
2022-02-11 10:51:33 +01:00
authenticate,
tasksFiles,
2022-03-22 16:58:49 +01:00
asyncMiddleware(videoStudioAddEditionValidator),
2022-02-11 10:51:33 +01:00
asyncMiddleware(createEditionTasks)
)
// ---------------------------------------------------------------------------
export {
2022-03-22 16:58:49 +01:00
studioRouter
2022-02-11 10:51:33 +01:00
}
// ---------------------------------------------------------------------------
async function createEditionTasks (req: express.Request, res: express.Response) {
const files = req.files as Express.Multer.File[]
2022-03-22 16:58:49 +01:00
const body = req.body as VideoStudioCreateEdition
2022-02-11 10:51:33 +01: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 10:51:33 +01:00
}
await createVideoStudioJob({
user: res.locals.oauth.token.User,
payload,
video
})
2022-02-11 10:51:33 +01: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 10:51:33 +01: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 10:51:33 +01: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 10:51:33 +01:00
return {
name: task.name,
options: {
file: destination
2022-02-11 10:51:33 +01:00
}
}
}
2022-03-22 16:58:49 +01:00
function buildCutTask (task: VideoStudioTaskCut) {
return Promise.resolve({
2022-02-11 10:51:33 +01:00
name: task.name,
options: {
start: task.options.start,
end: task.options.end
}
})
2022-02-11 10:51:33 +01:00
}
async function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
const destination = await moveStudioFileToPersistentTMP(getTaskFileFromReq(files, indice).path)
2022-02-11 10:51:33 +01:00
return {
name: task.name,
options: {
file: destination,
watermarkSizeRatio: VIDEO_FILTERS.WATERMARK.SIZE_RATIO,
horitonzalMarginRatio: VIDEO_FILTERS.WATERMARK.HORIZONTAL_MARGIN_RATIO,
verticalMarginRatio: VIDEO_FILTERS.WATERMARK.VERTICAL_MARGIN_RATIO
2022-02-11 10:51:33 +01:00
}
}
}
async function moveStudioFileToPersistentTMP (file: string) {
const destination = getStudioTaskFilePath(basename(file))
await move(file, destination)
return destination
}