]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
c8861d5d | 2 | import { body } from 'express-validator' |
2158ac90 RK |
3 | import { isPreImportVideoAccepted } from '@server/lib/moderation' |
4 | import { Hooks } from '@server/lib/plugins/hooks' | |
c0e8b12e | 5 | import { HttpStatusCode } from '@shared/models' |
2158ac90 | 6 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' |
c8861d5d | 7 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
6e46de09 | 8 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' |
3e753302 | 9 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' |
2158ac90 RK |
10 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
11 | import { logger } from '../../../helpers/logger' | |
6dd9de95 | 12 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 13 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
10363c74 | 14 | import { areValidationErrors, doesVideoChannelOfAccountExist } from '../shared' |
2158ac90 | 15 | import { getCommonVideoEditAttributes } from './videos' |
7b54a81c | 16 | import { isValid as isIPValid, parse as parseIP } from 'ipaddr.js' |
fbad87b0 | 17 | |
418d092a | 18 | const 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() | |
7dab0bd6 RK |
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 | ), | |
fbad87b0 C |
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 | |
faa9d434 | 44 | const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined |
fbad87b0 C |
45 | |
46 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
5d08a6a7 | 47 | |
454c20fa | 48 | if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) { |
5d08a6a7 | 49 | cleanUpReqFiles(req) |
76148b27 RK |
50 | |
51 | return res.fail({ | |
52 | status: HttpStatusCode.CONFLICT_409, | |
53 | message: 'HTTP import is not enabled on this instance.' | |
54 | }) | |
5d08a6a7 C |
55 | } |
56 | ||
a84b8fa5 C |
57 | if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { |
58 | cleanUpReqFiles(req) | |
76148b27 RK |
59 | |
60 | return res.fail({ | |
61 | status: HttpStatusCode.CONFLICT_409, | |
62 | message: 'Torrent/magnet URI import is not enabled on this instance.' | |
63 | }) | |
a84b8fa5 C |
64 | } |
65 | ||
0f6acda1 | 66 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) |
fbad87b0 | 67 | |
ce33919c | 68 | // Check we have at least 1 required param |
a84b8fa5 | 69 | if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { |
ce33919c C |
70 | cleanUpReqFiles(req) |
71 | ||
76148b27 | 72 | return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' }) |
ce33919c C |
73 | } |
74 | ||
7b54a81c C |
75 | if (req.body.targetUrl) { |
76 | const hostname = new URL(req.body.targetUrl).hostname | |
77 | ||
78 | if (isIPValid(hostname)) { | |
79 | const parsed = parseIP(hostname) | |
80 | ||
81 | if (parsed.range() !== 'unicast') { | |
82 | cleanUpReqFiles(req) | |
83 | ||
84 | return res.fail({ | |
85 | status: HttpStatusCode.FORBIDDEN_403, | |
86 | message: 'Cannot use non unicast IP as targetUrl.' | |
87 | }) | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
2158ac90 RK |
92 | if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) |
93 | ||
fbad87b0 C |
94 | return next() |
95 | } | |
96 | ]) | |
97 | ||
fbad87b0 C |
98 | // --------------------------------------------------------------------------- |
99 | ||
100 | export { | |
516df59b | 101 | videoImportAddValidator |
fbad87b0 C |
102 | } |
103 | ||
104 | // --------------------------------------------------------------------------- | |
2158ac90 RK |
105 | |
106 | async function isImportAccepted (req: express.Request, res: express.Response) { | |
107 | const body: VideoImportCreate = req.body | |
108 | const hookName = body.targetUrl | |
109 | ? 'filter:api.video.pre-import-url.accept.result' | |
110 | : 'filter:api.video.pre-import-torrent.accept.result' | |
111 | ||
112 | // Check we accept this video | |
113 | const acceptParameters = { | |
114 | videoImportBody: body, | |
115 | user: res.locals.oauth.token.User | |
116 | } | |
117 | const acceptedResult = await Hooks.wrapFun( | |
118 | isPreImportVideoAccepted, | |
119 | acceptParameters, | |
120 | hookName | |
121 | ) | |
122 | ||
123 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
124 | logger.info('Refused to import video.', { acceptedResult, acceptParameters }) | |
2158ac90 | 125 | |
76148b27 RK |
126 | res.fail({ |
127 | status: HttpStatusCode.FORBIDDEN_403, | |
128 | message: acceptedResult.errorMessage || 'Refused to import video' | |
129 | }) | |
2158ac90 RK |
130 | return false |
131 | } | |
132 | ||
133 | return true | |
134 | } |