]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/videos/video-imports.ts
improve api param message for video names and tags
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-imports.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { body } from 'express-validator'
3import { isPreImportVideoAccepted } from '@server/lib/moderation'
4import { Hooks } from '@server/lib/plugins/hooks'
5import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
6import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
7import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
8import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
9import { cleanUpReqFiles } from '../../../helpers/express-utils'
10import { logger } from '../../../helpers/logger'
11import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
12import { CONFIG } from '../../../initializers/config'
13import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
14import { areValidationErrors } from '../utils'
15import { getCommonVideoEditAttributes } from './videos'
16import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
17
18const videoImportAddValidator = getCommonVideoEditAttributes().concat([
19 body('channelId')
20 .customSanitizer(toIntOrNull)
21 .custom(isIdValid).withMessage('Should have correct video channel id'),
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'),
28 body('torrentfile')
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 ),
34 body('name')
35 .optional()
36 .custom(isVideoNameValid).withMessage(
37 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
38 ),
39
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
42
43 const user = res.locals.oauth.token.User
44 const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
45
46 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
47
48 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) {
49 cleanUpReqFiles(req)
50 return res.status(HttpStatusCode.CONFLICT_409)
51 .json({ error: 'HTTP import is not enabled on this instance.' })
52 }
53
54 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
55 cleanUpReqFiles(req)
56 return res.status(HttpStatusCode.CONFLICT_409)
57 .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' })
58 }
59
60 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
61
62 // Check we have at least 1 required param
63 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
64 cleanUpReqFiles(req)
65
66 return res.status(HttpStatusCode.BAD_REQUEST_400)
67 .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' })
68 }
69
70 if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
71
72 return next()
73 }
74])
75
76// ---------------------------------------------------------------------------
77
78export {
79 videoImportAddValidator
80}
81
82// ---------------------------------------------------------------------------
83
84async function isImportAccepted (req: express.Request, res: express.Response) {
85 const body: VideoImportCreate = req.body
86 const hookName = body.targetUrl
87 ? 'filter:api.video.pre-import-url.accept.result'
88 : 'filter:api.video.pre-import-torrent.accept.result'
89
90 // Check we accept this video
91 const acceptParameters = {
92 videoImportBody: body,
93 user: res.locals.oauth.token.User
94 }
95 const acceptedResult = await Hooks.wrapFun(
96 isPreImportVideoAccepted,
97 acceptParameters,
98 hookName
99 )
100
101 if (!acceptedResult || acceptedResult.accepted !== true) {
102 logger.info('Refused to import video.', { acceptedResult, acceptParameters })
103 res.status(HttpStatusCode.FORBIDDEN_403)
104 .json({ error: acceptedResult.errorMessage || 'Refused to import video' })
105
106 return false
107 }
108
109 return true
110}