2022-02-11 03:51:33 -06:00
|
|
|
import express from 'express'
|
|
|
|
import { body, param } from 'express-validator'
|
|
|
|
import { isIdOrUUIDValid } from '@server/helpers/custom-validators/misc'
|
|
|
|
import {
|
2022-03-22 10:58:49 -05:00
|
|
|
isStudioCutTaskValid,
|
|
|
|
isStudioTaskAddIntroOutroValid,
|
|
|
|
isStudioTaskAddWatermarkValid,
|
|
|
|
isValidStudioTasksArray
|
|
|
|
} from '@server/helpers/custom-validators/video-studio'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { cleanUpReqFiles } from '@server/helpers/express-utils'
|
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2023-05-03 08:17:11 -05:00
|
|
|
import { approximateIntroOutroAdditionalSize, getTaskFileFromReq } from '@server/lib/video-studio'
|
2023-04-21 07:55:10 -05:00
|
|
|
import { isAudioFile } from '@shared/ffmpeg'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { HttpStatusCode, UserRight, VideoStudioCreateEdition, VideoStudioTask } from '@shared/models'
|
2022-02-11 03:51:33 -06:00
|
|
|
import { areValidationErrors, checkUserCanManageVideo, checkUserQuota, doesVideoExist } from '../shared'
|
2023-07-19 09:02:49 -05:00
|
|
|
import { checkVideoFileCanBeEdited } from './shared'
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
const videoStudioAddEditionValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('videoId')
|
|
|
|
.custom(isIdOrUUIDValid).withMessage('Should have a valid video id/uuid/short uuid'),
|
2022-02-11 03:51:33 -06:00
|
|
|
|
2022-08-17 07:27:04 -05:00
|
|
|
body('tasks')
|
|
|
|
.custom(isValidStudioTasksArray).withMessage('Should have a valid array of tasks'),
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2022-03-22 10:58:49 -05:00
|
|
|
if (CONFIG.VIDEO_STUDIO.ENABLED !== true) {
|
2022-02-11 03:51:33 -06:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2022-03-22 10:58:49 -05:00
|
|
|
message: 'Video studio is disabled on this instance'
|
2022-02-11 03:51:33 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
return cleanUpReqFiles(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
const body: VideoStudioCreateEdition = req.body
|
2022-02-11 03:51:33 -06:00
|
|
|
const files = req.files as Express.Multer.File[]
|
|
|
|
|
|
|
|
for (let i = 0; i < body.tasks.length; i++) {
|
|
|
|
const task = body.tasks[i]
|
|
|
|
|
|
|
|
if (!checkTask(req, task, i)) {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: `Task ${task.name} is invalid`
|
|
|
|
})
|
|
|
|
|
|
|
|
return cleanUpReqFiles(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task.name === 'add-intro' || task.name === 'add-outro') {
|
2023-05-03 08:17:11 -05:00
|
|
|
const filePath = getTaskFileFromReq(files, i).path
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
// Our concat filter needs a video stream
|
|
|
|
if (await isAudioFile(filePath)) {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: `Task ${task.name} is invalid: file does not contain a video stream`
|
|
|
|
})
|
|
|
|
|
|
|
|
return cleanUpReqFiles(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
|
|
|
|
|
|
|
|
const video = res.locals.videoAll
|
2023-07-19 09:02:49 -05:00
|
|
|
if (!checkVideoFileCanBeEdited(video, res)) return cleanUpReqFiles(req)
|
2022-02-11 03:51:33 -06:00
|
|
|
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
if (!checkUserCanManageVideo(user, video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
|
|
|
|
|
|
|
|
// Try to make an approximation of bytes added by the intro/outro
|
2023-05-03 08:17:11 -05:00
|
|
|
const additionalBytes = await approximateIntroOutroAdditionalSize(video, body.tasks, i => getTaskFileFromReq(files, i).path)
|
2022-02-11 03:51:33 -06:00
|
|
|
if (await checkUserQuota(user, additionalBytes, res) === false) return cleanUpReqFiles(req)
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2022-03-22 10:58:49 -05:00
|
|
|
videoStudioAddEditionValidator
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const taskCheckers: {
|
2022-03-22 10:58:49 -05:00
|
|
|
[id in VideoStudioTask['name']]: (task: VideoStudioTask, indice?: number, files?: Express.Multer.File[]) => boolean
|
2022-02-11 03:51:33 -06:00
|
|
|
} = {
|
2022-03-22 10:58:49 -05:00
|
|
|
'cut': isStudioCutTaskValid,
|
|
|
|
'add-intro': isStudioTaskAddIntroOutroValid,
|
|
|
|
'add-outro': isStudioTaskAddIntroOutroValid,
|
|
|
|
'add-watermark': isStudioTaskAddWatermarkValid
|
2022-02-11 03:51:33 -06:00
|
|
|
}
|
|
|
|
|
2022-03-22 10:58:49 -05:00
|
|
|
function checkTask (req: express.Request, task: VideoStudioTask, indice?: number) {
|
2022-02-11 03:51:33 -06:00
|
|
|
const checker = taskCheckers[task.name]
|
|
|
|
if (!checker) return false
|
|
|
|
|
|
|
|
return checker(task, indice, req.files as Express.Multer.File[])
|
|
|
|
}
|