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