PeerTube/server/middlewares/validators/video-imports.ts

69 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as express from 'express'
import { body } from 'express-validator/check'
import { isIdValid } from '../../helpers/custom-validators/misc'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
import { getCommonVideoAttributes } from './videos'
2018-08-07 02:54:36 -05:00
import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../helpers/custom-validators/video-imports'
import { cleanUpReqFiles } from '../../helpers/utils'
2018-08-06 10:13:39 -05:00
import { isVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../helpers/custom-validators/videos'
2018-08-03 04:10:31 -05:00
import { CONFIG } from '../../initializers/constants'
2018-08-07 02:54:36 -05:00
import { CONSTRAINTS_FIELDS } from '../../initializers'
const videoImportAddValidator = getCommonVideoAttributes().concat([
body('channelId')
.toInt()
.custom(isIdValid).withMessage('Should have correct video channel id'),
2018-08-06 10:13:39 -05:00
body('targetUrl')
.optional()
.custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
body('magnetUri')
.optional()
.custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
2018-08-07 02:54:36 -05:00
body('torrentfile')
.custom((value, { req }) => isVideoImportTorrentFile(req.files)).withMessage(
'This torrent file is not supported or too large. Please, make sure it is of the following type: '
+ CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ')
),
body('name')
.optional()
.custom(isVideoNameValid).withMessage('Should have a valid name'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
const user = res.locals.oauth.token.User
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
2018-08-03 04:10:31 -05:00
if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
cleanUpReqFiles(req)
return res.status(409)
.json({ error: 'Import is not enabled on this instance.' })
.end()
}
if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
2018-08-06 10:13:39 -05:00
// Check we have at least 1 required param
2018-08-07 02:54:36 -05:00
const file = req.files['torrentfile'][0]
if (!req.body.targetUrl && !req.body.magnetUri && !file) {
2018-08-06 10:13:39 -05:00
cleanUpReqFiles(req)
return res.status(400)
2018-08-07 02:54:36 -05:00
.json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
2018-08-06 10:13:39 -05:00
.end()
}
return next()
}
])
// ---------------------------------------------------------------------------
export {
videoImportAddValidator
}
// ---------------------------------------------------------------------------