2018-08-02 15:34:09 +02:00
|
|
|
import * as express from 'express'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { body } from 'express-validator'
|
2020-05-14 11:10:26 +02:00
|
|
|
import { isPreImportVideoAccepted } from '@server/lib/moderation'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
|
|
|
import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
|
2019-07-25 16:23:44 +02:00
|
|
|
import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
|
2018-10-05 11:15:06 +02:00
|
|
|
import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
|
2019-07-23 10:40:39 +02:00
|
|
|
import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
|
2020-05-14 11:10:26 +02:00
|
|
|
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
|
2019-04-11 11:33:44 +02:00
|
|
|
import { CONFIG } from '../../../initializers/config'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
2020-05-14 11:10:26 +02:00
|
|
|
import { areValidationErrors } from '../utils'
|
|
|
|
import { getCommonVideoEditAttributes } from './videos'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
|
2018-08-02 15:34:09 +02:00
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
const videoImportAddValidator = getCommonVideoEditAttributes().concat([
|
2018-08-02 15:34:09 +02:00
|
|
|
body('channelId')
|
2019-07-25 16:23:44 +02:00
|
|
|
.customSanitizer(toIntOrNull)
|
2018-08-02 15:34:09 +02:00
|
|
|
.custom(isIdValid).withMessage('Should have correct video channel id'),
|
2018-08-06 17:13:39 +02: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 09:54:36 +02:00
|
|
|
body('torrentfile')
|
2020-01-31 16:56:52 +01:00
|
|
|
.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 15:34:09 +02: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
|
2020-06-17 10:55:40 +02:00
|
|
|
const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
|
2018-08-02 15:34:09 +02:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
2018-08-03 11:10:31 +02:00
|
|
|
|
2018-08-07 10:07:53 +02:00
|
|
|
if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
|
2018-08-03 11:10:31 +02:00
|
|
|
cleanUpReqFiles(req)
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.CONFLICT_409)
|
2018-08-07 10:07:53 +02:00
|
|
|
.json({ error: 'HTTP import is not enabled on this instance.' })
|
2018-08-03 11:10:31 +02:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2018-08-07 10:07:53 +02:00
|
|
|
if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
|
|
|
|
cleanUpReqFiles(req)
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.CONFLICT_409)
|
2018-08-07 10:07:53 +02:00
|
|
|
.json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2019-03-19 09:26:50 +01:00
|
|
|
if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
|
2018-08-02 15:34:09 +02:00
|
|
|
|
2018-08-06 17:13:39 +02:00
|
|
|
// Check we have at least 1 required param
|
2018-08-07 10:07:53 +02:00
|
|
|
if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
|
2018-08-06 17:13:39 +02:00
|
|
|
cleanUpReqFiles(req)
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
return res.status(HttpStatusCode.BAD_REQUEST_400)
|
2018-08-07 09:54:36 +02:00
|
|
|
.json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
|
2018-08-06 17:13:39 +02:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2020-05-14 11:10:26 +02:00
|
|
|
if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
|
|
|
|
|
2018-08-02 15:34:09 +02:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-08-03 09:43:00 +02:00
|
|
|
videoImportAddValidator
|
2018-08-02 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2020-05-14 11:10:26 +02:00
|
|
|
|
|
|
|
async function isImportAccepted (req: express.Request, res: express.Response) {
|
|
|
|
const body: VideoImportCreate = req.body
|
|
|
|
const hookName = body.targetUrl
|
|
|
|
? 'filter:api.video.pre-import-url.accept.result'
|
|
|
|
: 'filter:api.video.pre-import-torrent.accept.result'
|
|
|
|
|
|
|
|
// Check we accept this video
|
|
|
|
const acceptParameters = {
|
|
|
|
videoImportBody: body,
|
|
|
|
user: res.locals.oauth.token.User
|
|
|
|
}
|
|
|
|
const acceptedResult = await Hooks.wrapFun(
|
|
|
|
isPreImportVideoAccepted,
|
|
|
|
acceptParameters,
|
|
|
|
hookName
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!acceptedResult || acceptedResult.accepted !== true) {
|
|
|
|
logger.info('Refused to import video.', { acceptedResult, acceptParameters })
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.FORBIDDEN_403)
|
2020-05-14 11:10:26 +02:00
|
|
|
.json({ error: acceptedResult.errorMessage || 'Refused to import video' })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|