2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2022-08-10 04:51:13 -05:00
|
|
|
import { body, param, query } from 'express-validator'
|
2022-02-07 04:21:25 -06:00
|
|
|
import { isResolvingToUnicastOnly } from '@server/helpers/dns'
|
2020-05-14 04:10:26 -05:00
|
|
|
import { isPreImportVideoAccepted } from '@server/lib/moderation'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks'
|
2022-01-19 07:23:00 -06:00
|
|
|
import { MUserAccountId, MVideoImport } from '@server/types/models'
|
|
|
|
import { HttpStatusCode, UserRight, VideoImportState } from '@shared/models'
|
2020-05-14 04:10:26 -05:00
|
|
|
import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
|
2019-07-25 09:23:44 -05:00
|
|
|
import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
|
2018-10-05 04:15:06 -05:00
|
|
|
import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
|
2019-07-23 03:40:39 -05:00
|
|
|
import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
|
2020-05-14 04:10:26 -05:00
|
|
|
import { cleanUpReqFiles } from '../../../helpers/express-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2019-04-11 04:33:44 -05:00
|
|
|
import { CONFIG } from '../../../initializers/config'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
|
2022-01-19 07:23:00 -06:00
|
|
|
import { areValidationErrors, doesVideoChannelOfAccountExist, doesVideoImportExist } from '../shared'
|
2020-05-14 04:10:26 -05:00
|
|
|
import { getCommonVideoEditAttributes } from './videos'
|
2018-08-02 08:34:09 -05:00
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
const videoImportAddValidator = getCommonVideoEditAttributes().concat([
|
2018-08-02 08:34:09 -05:00
|
|
|
body('channelId')
|
2019-07-25 09:23:44 -05:00
|
|
|
.customSanitizer(toIntOrNull)
|
2018-08-02 08:34:09 -05:00
|
|
|
.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')
|
2020-01-31 09:56:52 -06: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 08:34:09 -05:00
|
|
|
body('name')
|
|
|
|
.optional()
|
2021-05-31 13:46:22 -05:00
|
|
|
.custom(isVideoNameValid).withMessage(
|
|
|
|
`Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
|
|
|
|
),
|
2018-08-02 08:34:09 -05:00
|
|
|
|
|
|
|
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 03:55:40 -05:00
|
|
|
const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
|
2018-08-02 08:34:09 -05:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
|
2018-08-03 04:10:31 -05:00
|
|
|
|
2021-01-15 08:56:56 -06:00
|
|
|
if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) {
|
2018-08-03 04:10:31 -05:00
|
|
|
cleanUpReqFiles(req)
|
2021-05-31 18:36:53 -05:00
|
|
|
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'HTTP import is not enabled on this instance.'
|
|
|
|
})
|
2018-08-03 04:10:31 -05:00
|
|
|
}
|
|
|
|
|
2018-08-07 03:07:53 -05:00
|
|
|
if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
|
|
|
|
cleanUpReqFiles(req)
|
2021-05-31 18:36:53 -05:00
|
|
|
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'Torrent/magnet URI import is not enabled on this instance.'
|
|
|
|
})
|
2018-08-07 03:07:53 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 03:26:50 -05:00
|
|
|
if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
|
2018-08-02 08:34:09 -05:00
|
|
|
|
2018-08-06 10:13:39 -05:00
|
|
|
// Check we have at least 1 required param
|
2018-08-07 03:07:53 -05:00
|
|
|
if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
|
2018-08-06 10:13:39 -05:00
|
|
|
cleanUpReqFiles(req)
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' })
|
2018-08-06 10:13:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-06 04:16:35 -06:00
|
|
|
if (req.body.targetUrl) {
|
|
|
|
const hostname = new URL(req.body.targetUrl).hostname
|
|
|
|
|
2022-02-07 04:21:25 -06:00
|
|
|
if (await isResolvingToUnicastOnly(hostname) !== true) {
|
|
|
|
cleanUpReqFiles(req)
|
2022-01-06 04:16:35 -06:00
|
|
|
|
2022-02-07 04:21:25 -06:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: 'Cannot use non unicast IP as targetUrl.'
|
|
|
|
})
|
2022-01-06 04:16:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 04:10:26 -05:00
|
|
|
if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
|
|
|
|
|
2018-08-02 08:34:09 -05:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
2022-08-10 04:51:13 -05:00
|
|
|
const getMyVideoImportsValidator = [
|
|
|
|
query('videoChannelSyncId')
|
|
|
|
.optional()
|
|
|
|
.custom(isIdValid).withMessage('Should have correct videoChannelSync id'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking getMyVideoImportsValidator parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-01-19 07:23:00 -06:00
|
|
|
const videoImportDeleteValidator = [
|
|
|
|
param('id')
|
|
|
|
.custom(isIdValid).withMessage('Should have correct import id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
if (!await doesVideoImportExist(parseInt(req.params.id), res)) return
|
|
|
|
if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return
|
|
|
|
|
|
|
|
if (res.locals.videoImport.state === VideoImportState.PENDING) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'Cannot delete a pending video import. Cancel it or wait for the end of the import first.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const videoImportCancelValidator = [
|
|
|
|
param('id')
|
|
|
|
.custom(isIdValid).withMessage('Should have correct import id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoImportCancelValidator parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
if (!await doesVideoImportExist(parseInt(req.params.id), res)) return
|
|
|
|
if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return
|
|
|
|
|
|
|
|
if (res.locals.videoImport.state !== VideoImportState.PENDING) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.CONFLICT_409,
|
|
|
|
message: 'Cannot cancel a non pending video import.'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-02 08:34:09 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2022-01-19 07:23:00 -06:00
|
|
|
videoImportAddValidator,
|
|
|
|
videoImportCancelValidator,
|
2022-08-10 04:51:13 -05:00
|
|
|
videoImportDeleteValidator,
|
|
|
|
getMyVideoImportsValidator
|
2018-08-02 08:34:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2020-05-14 04:10:26 -05: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 })
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: acceptedResult.errorMessage || 'Refused to import video'
|
|
|
|
})
|
2020-05-14 04:10:26 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2022-01-19 07:23:00 -06:00
|
|
|
|
|
|
|
function checkUserCanManageImport (user: MUserAccountId, videoImport: MVideoImport, res: express.Response) {
|
|
|
|
if (user.hasRight(UserRight.MANAGE_VIDEO_IMPORTS) === false && videoImport.userId !== user.id) {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: 'Cannot manage video import of another user'
|
|
|
|
})
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|