2023-05-03 08:17:11 -05:00
|
|
|
import Bluebird from 'bluebird'
|
2022-02-11 03:51:33 -06:00
|
|
|
import express from 'express'
|
2023-05-03 08:17:11 -05:00
|
|
|
import { move } from 'fs-extra'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { basename } from 'path'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { createAnyReqFiles } from '@server/helpers/express-utils'
|
2023-05-04 08:29:34 -05:00
|
|
|
import { MIMETYPES, VIDEO_FILTERS } from '@server/initializers/constants'
|
|
|
|
import { buildTaskFileFieldname, createVideoStudioJob, getStudioTaskFilePath, getTaskFileFromReq } from '@server/lib/video-studio'
|
2022-02-11 03:51:33 -06:00
|
|
|
import {
|
|
|
|
HttpStatusCode,
|
2022-03-22 10:58:49 -05:00
|
|
|
VideoState,
|
|
|
|
VideoStudioCreateEdition,
|
|
|
|
VideoStudioTask,
|
|
|
|
VideoStudioTaskCut,
|
|
|
|
VideoStudioTaskIntro,
|
|
|
|
VideoStudioTaskOutro,
|
|
|
|
VideoStudioTaskPayload,
|
|
|
|
VideoStudioTaskWatermark
|
2022-02-11 03:51:33 -06:00
|
|
|
} from '@shared/models'
|
2022-03-22 10:58:49 -05:00
|
|
|
import { asyncMiddleware, authenticate, videoStudioAddEditionValidator } from '../../../middlewares'
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
const studioRouter = express.Router()
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
const tasksFiles = createAnyReqFiles(
|
|
|
|
MIMETYPES.VIDEO.MIMETYPE_EXT,
|
|
|
|
(req: express.Request, file: Express.Multer.File, cb: (err: Error, result?: boolean) => void) => {
|
2022-03-22 10:58:49 -05:00
|
|
|
const body = req.body as VideoStudioCreateEdition
|
2022-02-11 03:51:33 -06: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 10:58:49 -05:00
|
|
|
studioRouter.post('/:videoId/studio/edit',
|
2022-02-11 03:51:33 -06:00
|
|
|
authenticate,
|
|
|
|
tasksFiles,
|
2022-03-22 10:58:49 -05:00
|
|
|
asyncMiddleware(videoStudioAddEditionValidator),
|
2022-02-11 03:51:33 -06:00
|
|
|
asyncMiddleware(createEditionTasks)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2022-03-22 10:58:49 -05:00
|
|
|
studioRouter
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function createEditionTasks (req: express.Request, res: express.Response) {
|
|
|
|
const files = req.files as Express.Multer.File[]
|
2022-03-22 10:58:49 -05:00
|
|
|
const body = req.body as VideoStudioCreateEdition
|
2022-02-11 03:51:33 -06:00
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
video.state = VideoState.TO_EDIT
|
|
|
|
await video.save()
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
videoUUID: video.uuid,
|
2023-05-03 08:17:11 -05:00
|
|
|
tasks: await Bluebird.mapSeries(body.tasks, (t, i) => buildTaskPayload(t, i, files))
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
await createVideoStudioJob({
|
|
|
|
user: res.locals.oauth.token.User,
|
|
|
|
payload,
|
|
|
|
video
|
|
|
|
})
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
|
|
|
const taskPayloadBuilders: {
|
2023-05-03 08:17:11 -05:00
|
|
|
[id in VideoStudioTask['name']]: (
|
|
|
|
task: VideoStudioTask,
|
|
|
|
indice?: number,
|
|
|
|
files?: Express.Multer.File[]
|
|
|
|
) => Promise<VideoStudioTaskPayload>
|
2022-02-11 03:51:33 -06:00
|
|
|
} = {
|
|
|
|
'add-intro': buildIntroOutroTask,
|
|
|
|
'add-outro': buildIntroOutroTask,
|
|
|
|
'cut': buildCutTask,
|
|
|
|
'add-watermark': buildWatermarkTask
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
function buildTaskPayload (task: VideoStudioTask, indice: number, files: Express.Multer.File[]): Promise<VideoStudioTaskPayload> {
|
2022-02-11 03:51:33 -06:00
|
|
|
return taskPayloadBuilders[task.name](task, indice, files)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
async function buildIntroOutroTask (task: VideoStudioTaskIntro | VideoStudioTaskOutro, indice: number, files: Express.Multer.File[]) {
|
|
|
|
const destination = await moveStudioFileToPersistentTMP(getTaskFileFromReq(files, indice).path)
|
|
|
|
|
2022-02-11 03:51:33 -06:00
|
|
|
return {
|
|
|
|
name: task.name,
|
|
|
|
options: {
|
2023-05-03 08:17:11 -05:00
|
|
|
file: destination
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
function buildCutTask (task: VideoStudioTaskCut) {
|
2023-05-03 08:17:11 -05:00
|
|
|
return Promise.resolve({
|
2022-02-11 03:51:33 -06:00
|
|
|
name: task.name,
|
|
|
|
options: {
|
|
|
|
start: task.options.start,
|
|
|
|
end: task.options.end
|
|
|
|
}
|
2023-05-03 08:17:11 -05:00
|
|
|
})
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
2023-05-03 08:17:11 -05:00
|
|
|
async function buildWatermarkTask (task: VideoStudioTaskWatermark, indice: number, files: Express.Multer.File[]) {
|
|
|
|
const destination = await moveStudioFileToPersistentTMP(getTaskFileFromReq(files, indice).path)
|
|
|
|
|
2022-02-11 03:51:33 -06:00
|
|
|
return {
|
|
|
|
name: task.name,
|
|
|
|
options: {
|
2023-05-04 08:29:34 -05:00
|
|
|
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 03:51:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 08:17:11 -05:00
|
|
|
|
|
|
|
async function moveStudioFileToPersistentTMP (file: string) {
|
2023-05-04 08:29:34 -05:00
|
|
|
const destination = getStudioTaskFilePath(basename(file))
|
2023-05-03 08:17:11 -05:00
|
|
|
|
|
|
|
await move(file, destination)
|
|
|
|
|
|
|
|
return destination
|
|
|
|
}
|