]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-imports.ts
Move middleware utils in middlewares
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-imports.ts
CommitLineData
fbad87b0 1import * as express from 'express'
c8861d5d 2import { body } from 'express-validator'
2158ac90
RK
3import { isPreImportVideoAccepted } from '@server/lib/moderation'
4import { Hooks } from '@server/lib/plugins/hooks'
10363c74 5import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
2158ac90 6import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
c8861d5d 7import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
6e46de09 8import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
3e753302 9import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
2158ac90
RK
10import { cleanUpReqFiles } from '../../../helpers/express-utils'
11import { logger } from '../../../helpers/logger'
6dd9de95 12import { CONFIG } from '../../../initializers/config'
74dc3bca 13import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
10363c74 14import { areValidationErrors, doesVideoChannelOfAccountExist } from '../shared'
2158ac90 15import { getCommonVideoEditAttributes } from './videos'
fbad87b0 16
418d092a 17const videoImportAddValidator = getCommonVideoEditAttributes().concat([
fbad87b0 18 body('channelId')
c8861d5d 19 .customSanitizer(toIntOrNull)
fbad87b0 20 .custom(isIdValid).withMessage('Should have correct video channel id'),
ce33919c
C
21 body('targetUrl')
22 .optional()
23 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
24 body('magnetUri')
25 .optional()
26 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
990b6a0b 27 body('torrentfile')
a1587156
C
28 .custom((value, { req }) => isVideoImportTorrentFile(req.files))
29 .withMessage(
30 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' +
31 CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ')
32 ),
fbad87b0
C
33 body('name')
34 .optional()
7dab0bd6
RK
35 .custom(isVideoNameValid).withMessage(
36 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
37 ),
fbad87b0
C
38
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
41
42 const user = res.locals.oauth.token.User
faa9d434 43 const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
fbad87b0
C
44
45 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
5d08a6a7 46
454c20fa 47 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) {
5d08a6a7 48 cleanUpReqFiles(req)
76148b27
RK
49
50 return res.fail({
51 status: HttpStatusCode.CONFLICT_409,
52 message: 'HTTP import is not enabled on this instance.'
53 })
5d08a6a7
C
54 }
55
a84b8fa5
C
56 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
57 cleanUpReqFiles(req)
76148b27
RK
58
59 return res.fail({
60 status: HttpStatusCode.CONFLICT_409,
61 message: 'Torrent/magnet URI import is not enabled on this instance.'
62 })
a84b8fa5
C
63 }
64
0f6acda1 65 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
fbad87b0 66
ce33919c 67 // Check we have at least 1 required param
a84b8fa5 68 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
ce33919c
C
69 cleanUpReqFiles(req)
70
76148b27 71 return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' })
ce33919c
C
72 }
73
2158ac90
RK
74 if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
75
fbad87b0
C
76 return next()
77 }
78])
79
fbad87b0
C
80// ---------------------------------------------------------------------------
81
82export {
516df59b 83 videoImportAddValidator
fbad87b0
C
84}
85
86// ---------------------------------------------------------------------------
2158ac90
RK
87
88async function isImportAccepted (req: express.Request, res: express.Response) {
89 const body: VideoImportCreate = req.body
90 const hookName = body.targetUrl
91 ? 'filter:api.video.pre-import-url.accept.result'
92 : 'filter:api.video.pre-import-torrent.accept.result'
93
94 // Check we accept this video
95 const acceptParameters = {
96 videoImportBody: body,
97 user: res.locals.oauth.token.User
98 }
99 const acceptedResult = await Hooks.wrapFun(
100 isPreImportVideoAccepted,
101 acceptParameters,
102 hookName
103 )
104
105 if (!acceptedResult || acceptedResult.accepted !== true) {
106 logger.info('Refused to import video.', { acceptedResult, acceptParameters })
2158ac90 107
76148b27
RK
108 res.fail({
109 status: HttpStatusCode.FORBIDDEN_403,
110 message: acceptedResult.errorMessage || 'Refused to import video'
111 })
2158ac90
RK
112 return false
113 }
114
115 return true
116}