]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
a3b472a1 | 2 | import { body, param, query } from 'express-validator' |
f33e5159 | 3 | import { isResolvingToUnicastOnly } from '@server/helpers/dns' |
2158ac90 RK |
4 | import { isPreImportVideoAccepted } from '@server/lib/moderation' |
5 | import { Hooks } from '@server/lib/plugins/hooks' | |
419b520c C |
6 | import { MUserAccountId, MVideoImport } from '@server/types/models' |
7 | import { HttpStatusCode, UserRight, VideoImportState } from '@shared/models' | |
2158ac90 | 8 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' |
c8861d5d | 9 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
6e46de09 | 10 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' |
3e753302 | 11 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' |
2158ac90 RK |
12 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
13 | import { logger } from '../../../helpers/logger' | |
6dd9de95 | 14 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 15 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
419b520c | 16 | import { areValidationErrors, doesVideoChannelOfAccountExist, doesVideoImportExist } from '../shared' |
2158ac90 | 17 | import { getCommonVideoEditAttributes } from './videos' |
fbad87b0 | 18 | |
418d092a | 19 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ |
fbad87b0 | 20 | body('channelId') |
c8861d5d | 21 | .customSanitizer(toIntOrNull) |
fbad87b0 | 22 | .custom(isIdValid).withMessage('Should have correct video channel id'), |
ce33919c C |
23 | body('targetUrl') |
24 | .optional() | |
25 | .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'), | |
26 | body('magnetUri') | |
27 | .optional() | |
28 | .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'), | |
990b6a0b | 29 | body('torrentfile') |
a1587156 C |
30 | .custom((value, { req }) => isVideoImportTorrentFile(req.files)) |
31 | .withMessage( | |
32 | 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' + | |
33 | CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ') | |
34 | ), | |
fbad87b0 C |
35 | body('name') |
36 | .optional() | |
7dab0bd6 RK |
37 | .custom(isVideoNameValid).withMessage( |
38 | `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long` | |
39 | ), | |
fbad87b0 C |
40 | |
41 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
42 | logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body }) | |
43 | ||
44 | const user = res.locals.oauth.token.User | |
faa9d434 | 45 | const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined |
fbad87b0 C |
46 | |
47 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
5d08a6a7 | 48 | |
454c20fa | 49 | if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) { |
5d08a6a7 | 50 | cleanUpReqFiles(req) |
76148b27 RK |
51 | |
52 | return res.fail({ | |
53 | status: HttpStatusCode.CONFLICT_409, | |
54 | message: 'HTTP import is not enabled on this instance.' | |
55 | }) | |
5d08a6a7 C |
56 | } |
57 | ||
a84b8fa5 C |
58 | if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { |
59 | cleanUpReqFiles(req) | |
76148b27 RK |
60 | |
61 | return res.fail({ | |
62 | status: HttpStatusCode.CONFLICT_409, | |
63 | message: 'Torrent/magnet URI import is not enabled on this instance.' | |
64 | }) | |
a84b8fa5 C |
65 | } |
66 | ||
0f6acda1 | 67 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) |
fbad87b0 | 68 | |
ce33919c | 69 | // Check we have at least 1 required param |
a84b8fa5 | 70 | if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { |
ce33919c C |
71 | cleanUpReqFiles(req) |
72 | ||
76148b27 | 73 | return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' }) |
ce33919c C |
74 | } |
75 | ||
7b54a81c C |
76 | if (req.body.targetUrl) { |
77 | const hostname = new URL(req.body.targetUrl).hostname | |
78 | ||
f33e5159 C |
79 | if (await isResolvingToUnicastOnly(hostname) !== true) { |
80 | cleanUpReqFiles(req) | |
7b54a81c | 81 | |
f33e5159 C |
82 | return res.fail({ |
83 | status: HttpStatusCode.FORBIDDEN_403, | |
84 | message: 'Cannot use non unicast IP as targetUrl.' | |
85 | }) | |
7b54a81c C |
86 | } |
87 | } | |
88 | ||
2158ac90 RK |
89 | if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) |
90 | ||
fbad87b0 C |
91 | return next() |
92 | } | |
93 | ]) | |
94 | ||
a3b472a1 C |
95 | const getMyVideoImportsValidator = [ |
96 | query('videoChannelSyncId') | |
97 | .optional() | |
98 | .custom(isIdValid).withMessage('Should have correct videoChannelSync id'), | |
99 | ||
100 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
101 | logger.debug('Checking getMyVideoImportsValidator parameters', { parameters: req.params }) | |
102 | ||
103 | if (areValidationErrors(req, res)) return | |
104 | ||
105 | return next() | |
106 | } | |
107 | ] | |
108 | ||
419b520c C |
109 | const videoImportDeleteValidator = [ |
110 | param('id') | |
111 | .custom(isIdValid).withMessage('Should have correct import id'), | |
112 | ||
113 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
114 | logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.params }) | |
115 | ||
116 | if (areValidationErrors(req, res)) return | |
117 | ||
118 | if (!await doesVideoImportExist(parseInt(req.params.id), res)) return | |
119 | if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return | |
120 | ||
121 | if (res.locals.videoImport.state === VideoImportState.PENDING) { | |
122 | return res.fail({ | |
123 | status: HttpStatusCode.CONFLICT_409, | |
124 | message: 'Cannot delete a pending video import. Cancel it or wait for the end of the import first.' | |
125 | }) | |
126 | } | |
127 | ||
128 | return next() | |
129 | } | |
130 | ] | |
131 | ||
132 | const videoImportCancelValidator = [ | |
133 | param('id') | |
134 | .custom(isIdValid).withMessage('Should have correct import id'), | |
135 | ||
136 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
137 | logger.debug('Checking videoImportCancelValidator parameters', { parameters: req.params }) | |
138 | ||
139 | if (areValidationErrors(req, res)) return | |
140 | ||
141 | if (!await doesVideoImportExist(parseInt(req.params.id), res)) return | |
142 | if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return | |
143 | ||
144 | if (res.locals.videoImport.state !== VideoImportState.PENDING) { | |
145 | return res.fail({ | |
146 | status: HttpStatusCode.CONFLICT_409, | |
147 | message: 'Cannot cancel a non pending video import.' | |
148 | }) | |
149 | } | |
150 | ||
151 | return next() | |
152 | } | |
153 | ] | |
154 | ||
fbad87b0 C |
155 | // --------------------------------------------------------------------------- |
156 | ||
157 | export { | |
419b520c C |
158 | videoImportAddValidator, |
159 | videoImportCancelValidator, | |
a3b472a1 C |
160 | videoImportDeleteValidator, |
161 | getMyVideoImportsValidator | |
fbad87b0 C |
162 | } |
163 | ||
164 | // --------------------------------------------------------------------------- | |
2158ac90 RK |
165 | |
166 | async function isImportAccepted (req: express.Request, res: express.Response) { | |
167 | const body: VideoImportCreate = req.body | |
168 | const hookName = body.targetUrl | |
169 | ? 'filter:api.video.pre-import-url.accept.result' | |
170 | : 'filter:api.video.pre-import-torrent.accept.result' | |
171 | ||
172 | // Check we accept this video | |
173 | const acceptParameters = { | |
174 | videoImportBody: body, | |
175 | user: res.locals.oauth.token.User | |
176 | } | |
177 | const acceptedResult = await Hooks.wrapFun( | |
178 | isPreImportVideoAccepted, | |
179 | acceptParameters, | |
180 | hookName | |
181 | ) | |
182 | ||
183 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
184 | logger.info('Refused to import video.', { acceptedResult, acceptParameters }) | |
2158ac90 | 185 | |
76148b27 RK |
186 | res.fail({ |
187 | status: HttpStatusCode.FORBIDDEN_403, | |
188 | message: acceptedResult.errorMessage || 'Refused to import video' | |
189 | }) | |
2158ac90 RK |
190 | return false |
191 | } | |
192 | ||
193 | return true | |
194 | } | |
419b520c C |
195 | |
196 | function checkUserCanManageImport (user: MUserAccountId, videoImport: MVideoImport, res: express.Response) { | |
197 | if (user.hasRight(UserRight.MANAGE_VIDEO_IMPORTS) === false && videoImport.userId !== user.id) { | |
198 | res.fail({ | |
199 | status: HttpStatusCode.FORBIDDEN_403, | |
200 | message: 'Cannot manage video import of another user' | |
201 | }) | |
202 | return false | |
203 | } | |
204 | ||
205 | return true | |
206 | } |