PeerTube/server/helpers/custom-validators/video-studio.ts

54 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-02-11 10:51:33 +01:00
import validator from 'validator'
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
2022-03-22 16:58:49 +01:00
import { buildTaskFileFieldname } from '@server/lib/video-studio'
import { VideoStudioTask } from '@shared/models'
2022-02-11 10:51:33 +01:00
import { isArray } from './misc'
import { isVideoFileMimeTypeValid, isVideoImageValid } from './videos'
import { forceNumber } from '@shared/core-utils'
2022-02-11 10:51:33 +01:00
2022-03-22 16:58:49 +01:00
function isValidStudioTasksArray (tasks: any) {
2022-02-11 10:51:33 +01:00
if (!isArray(tasks)) return false
2022-03-22 16:58:49 +01:00
return tasks.length >= CONSTRAINTS_FIELDS.VIDEO_STUDIO.TASKS.min &&
tasks.length <= CONSTRAINTS_FIELDS.VIDEO_STUDIO.TASKS.max
2022-02-11 10:51:33 +01:00
}
2022-03-22 16:58:49 +01:00
function isStudioCutTaskValid (task: VideoStudioTask) {
2022-02-11 10:51:33 +01:00
if (task.name !== 'cut') return false
if (!task.options) return false
const { start, end } = task.options
if (!start && !end) return false
2022-03-22 16:58:49 +01:00
if (start && !validator.isInt(start + '', CONSTRAINTS_FIELDS.VIDEO_STUDIO.CUT_TIME)) return false
if (end && !validator.isInt(end + '', CONSTRAINTS_FIELDS.VIDEO_STUDIO.CUT_TIME)) return false
2022-02-11 10:51:33 +01:00
if (!start || !end) return true
return forceNumber(start) < forceNumber(end)
2022-02-11 10:51:33 +01:00
}
2022-03-22 16:58:49 +01:00
function isStudioTaskAddIntroOutroValid (task: VideoStudioTask, indice: number, files: Express.Multer.File[]) {
2022-02-11 10:51:33 +01:00
const file = files.find(f => f.fieldname === buildTaskFileFieldname(indice, 'file'))
return (task.name === 'add-intro' || task.name === 'add-outro') &&
file && isVideoFileMimeTypeValid([ file ], null)
}
2022-03-22 16:58:49 +01:00
function isStudioTaskAddWatermarkValid (task: VideoStudioTask, indice: number, files: Express.Multer.File[]) {
2022-02-11 10:51:33 +01:00
const file = files.find(f => f.fieldname === buildTaskFileFieldname(indice, 'file'))
return task.name === 'add-watermark' &&
file && isVideoImageValid([ file ], null, true)
}
// ---------------------------------------------------------------------------
export {
2022-03-22 16:58:49 +01:00
isValidStudioTasksArray,
2022-02-11 10:51:33 +01:00
2022-03-22 16:58:49 +01:00
isStudioCutTaskValid,
isStudioTaskAddIntroOutroValid,
isStudioTaskAddWatermarkValid
2022-02-11 10:51:33 +01:00
}