]>
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 | 6 | import { MUserAccountId, MVideoImport } from '@server/types/models' |
4638cd71 | 7 | import { forceNumber } from '@shared/core-utils' |
419b520c | 8 | import { HttpStatusCode, UserRight, VideoImportState } from '@shared/models' |
2158ac90 | 9 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' |
c8861d5d | 10 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
6e46de09 | 11 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' |
3e753302 | 12 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' |
2158ac90 RK |
13 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
14 | import { logger } from '../../../helpers/logger' | |
6dd9de95 | 15 | import { CONFIG } from '../../../initializers/config' |
74dc3bca | 16 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
419b520c | 17 | import { areValidationErrors, doesVideoChannelOfAccountExist, doesVideoImportExist } from '../shared' |
2158ac90 | 18 | import { getCommonVideoEditAttributes } from './videos' |
fbad87b0 | 19 | |
418d092a | 20 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ |
fbad87b0 | 21 | body('channelId') |
c8861d5d | 22 | .customSanitizer(toIntOrNull) |
396f6f01 | 23 | .custom(isIdValid), |
ce33919c C |
24 | body('targetUrl') |
25 | .optional() | |
396f6f01 | 26 | .custom(isVideoImportTargetUrlValid), |
ce33919c C |
27 | body('magnetUri') |
28 | .optional() | |
396f6f01 | 29 | .custom(isVideoMagnetUriValid), |
990b6a0b | 30 | body('torrentfile') |
a1587156 C |
31 | .custom((value, { req }) => isVideoImportTorrentFile(req.files)) |
32 | .withMessage( | |
33 | 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' + | |
34 | CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ') | |
35 | ), | |
fbad87b0 C |
36 | body('name') |
37 | .optional() | |
7dab0bd6 RK |
38 | .custom(isVideoNameValid).withMessage( |
39 | `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long` | |
40 | ), | |
fbad87b0 C |
41 | |
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
fbad87b0 | 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 | ||
f33e5159 C |
78 | if (await isResolvingToUnicastOnly(hostname) !== true) { |
79 | cleanUpReqFiles(req) | |
7b54a81c | 80 | |
f33e5159 C |
81 | return res.fail({ |
82 | status: HttpStatusCode.FORBIDDEN_403, | |
83 | message: 'Cannot use non unicast IP as targetUrl.' | |
84 | }) | |
7b54a81c C |
85 | } |
86 | } | |
87 | ||
2158ac90 RK |
88 | if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req) |
89 | ||
fbad87b0 C |
90 | return next() |
91 | } | |
92 | ]) | |
93 | ||
a3b472a1 C |
94 | const getMyVideoImportsValidator = [ |
95 | query('videoChannelSyncId') | |
96 | .optional() | |
396f6f01 | 97 | .custom(isIdValid), |
a3b472a1 C |
98 | |
99 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
a3b472a1 C |
100 | if (areValidationErrors(req, res)) return |
101 | ||
102 | return next() | |
103 | } | |
104 | ] | |
105 | ||
419b520c C |
106 | const videoImportDeleteValidator = [ |
107 | param('id') | |
396f6f01 | 108 | .custom(isIdValid), |
419b520c C |
109 | |
110 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
419b520c C |
111 | if (areValidationErrors(req, res)) return |
112 | ||
113 | if (!await doesVideoImportExist(parseInt(req.params.id), res)) return | |
114 | if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return | |
115 | ||
116 | if (res.locals.videoImport.state === VideoImportState.PENDING) { | |
117 | return res.fail({ | |
118 | status: HttpStatusCode.CONFLICT_409, | |
119 | message: 'Cannot delete a pending video import. Cancel it or wait for the end of the import first.' | |
120 | }) | |
121 | } | |
122 | ||
123 | return next() | |
124 | } | |
125 | ] | |
126 | ||
127 | const videoImportCancelValidator = [ | |
128 | param('id') | |
396f6f01 | 129 | .custom(isIdValid), |
419b520c C |
130 | |
131 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
419b520c C |
132 | if (areValidationErrors(req, res)) return |
133 | ||
4638cd71 | 134 | if (!await doesVideoImportExist(forceNumber(req.params.id), res)) return |
419b520c C |
135 | if (!checkUserCanManageImport(res.locals.oauth.token.user, res.locals.videoImport, res)) return |
136 | ||
137 | if (res.locals.videoImport.state !== VideoImportState.PENDING) { | |
138 | return res.fail({ | |
139 | status: HttpStatusCode.CONFLICT_409, | |
140 | message: 'Cannot cancel a non pending video import.' | |
141 | }) | |
142 | } | |
143 | ||
144 | return next() | |
145 | } | |
146 | ] | |
147 | ||
fbad87b0 C |
148 | // --------------------------------------------------------------------------- |
149 | ||
150 | export { | |
419b520c C |
151 | videoImportAddValidator, |
152 | videoImportCancelValidator, | |
a3b472a1 C |
153 | videoImportDeleteValidator, |
154 | getMyVideoImportsValidator | |
fbad87b0 C |
155 | } |
156 | ||
157 | // --------------------------------------------------------------------------- | |
2158ac90 RK |
158 | |
159 | async function isImportAccepted (req: express.Request, res: express.Response) { | |
160 | const body: VideoImportCreate = req.body | |
161 | const hookName = body.targetUrl | |
162 | ? 'filter:api.video.pre-import-url.accept.result' | |
163 | : 'filter:api.video.pre-import-torrent.accept.result' | |
164 | ||
165 | // Check we accept this video | |
166 | const acceptParameters = { | |
167 | videoImportBody: body, | |
168 | user: res.locals.oauth.token.User | |
169 | } | |
170 | const acceptedResult = await Hooks.wrapFun( | |
171 | isPreImportVideoAccepted, | |
172 | acceptParameters, | |
173 | hookName | |
174 | ) | |
175 | ||
176 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
177 | logger.info('Refused to import video.', { acceptedResult, acceptParameters }) | |
2158ac90 | 178 | |
76148b27 RK |
179 | res.fail({ |
180 | status: HttpStatusCode.FORBIDDEN_403, | |
181 | message: acceptedResult.errorMessage || 'Refused to import video' | |
182 | }) | |
2158ac90 RK |
183 | return false |
184 | } | |
185 | ||
186 | return true | |
187 | } | |
419b520c C |
188 | |
189 | function checkUserCanManageImport (user: MUserAccountId, videoImport: MVideoImport, res: express.Response) { | |
190 | if (user.hasRight(UserRight.MANAGE_VIDEO_IMPORTS) === false && videoImport.userId !== user.id) { | |
191 | res.fail({ | |
192 | status: HttpStatusCode.FORBIDDEN_403, | |
193 | message: 'Cannot manage video import of another user' | |
194 | }) | |
195 | return false | |
196 | } | |
197 | ||
198 | return true | |
199 | } |