2018-08-02 09:34:09 -04:00
|
|
|
import * as express from 'express'
|
2018-08-03 03:43:00 -04:00
|
|
|
import { body } from 'express-validator/check'
|
2018-10-05 05:15:06 -04:00
|
|
|
import { isIdValid } from '../../../helpers/custom-validators/misc'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { areValidationErrors } from '../utils'
|
2019-02-26 04:55:40 -05:00
|
|
|
import { getCommonVideoEditAttributes } from './videos'
|
2018-10-05 05:15:06 -04:00
|
|
|
import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
|
|
|
|
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
2019-03-19 04:26:50 -04:00
|
|
|
import { doesVideoChannelOfAccountExist, isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
|
2018-10-05 05:15:06 -04:00
|
|
|
import { CONFIG } from '../../../initializers/constants'
|
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers'
|
2018-08-02 09:34:09 -04:00
|
|
|
|
2019-02-26 04:55:40 -05:00
|
|
|
const videoImportAddValidator = getCommonVideoEditAttributes().concat([
|
2018-08-02 09:34:09 -04:00
|
|
|
body('channelId')
|
|
|
|
.toInt()
|
|
|
|
.custom(isIdValid).withMessage('Should have correct video channel id'),
|
2018-08-06 11:13:39 -04: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 03:54:36 -04: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(', ')
|
|
|
|
),
|
2018-08-02 09:34:09 -04:00
|
|
|
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
|
2018-08-07 04:07:53 -04:00
|
|
|
const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined
|
2018-08-02 09:34:09 -04:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
2018-08-03 05:10:31 -04:00
|
|
|
|
2018-08-07 04:07:53 -04:00
|
|
|
if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
|
2018-08-03 05:10:31 -04:00
|
|
|
cleanUpReqFiles(req)
|
|
|
|
return res.status(409)
|
2018-08-07 04:07:53 -04:00
|
|
|
.json({ error: 'HTTP import is not enabled on this instance.' })
|
2018-08-03 05:10:31 -04:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2018-08-07 04:07:53 -04:00
|
|
|
if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
|
|
|
|
cleanUpReqFiles(req)
|
|
|
|
return res.status(409)
|
|
|
|
.json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
|
2018-08-02 09:34:09 -04:00
|
|
|
|
2018-08-06 11:13:39 -04:00
|
|
|
// Check we have at least 1 required param
|
2018-08-07 04:07:53 -04:00
|
|
|
if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
|
2018-08-06 11:13:39 -04:00
|
|
|
cleanUpReqFiles(req)
|
|
|
|
|
|
|
|
return res.status(400)
|
2018-08-07 03:54:36 -04:00
|
|
|
.json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
|
2018-08-06 11:13:39 -04:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:34:09 -04:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-08-03 03:43:00 -04:00
|
|
|
videoImportAddValidator
|
2018-08-02 09:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|