]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body } from 'express-validator' | |
3 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { areValidationErrors } from '../utils' | |
6 | import { getCommonVideoEditAttributes } from './videos' | |
7 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' | |
8 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | |
9 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' | |
10 | import { CONFIG } from '../../../initializers/config' | |
11 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | |
12 | import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' | |
13 | ||
14 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ | |
15 | body('channelId') | |
16 | .customSanitizer(toIntOrNull) | |
17 | .custom(isIdValid).withMessage('Should have correct video channel id'), | |
18 | body('targetUrl') | |
19 | .optional() | |
20 | .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'), | |
21 | body('magnetUri') | |
22 | .optional() | |
23 | .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'), | |
24 | body('torrentfile') | |
25 | .custom((value, { req }) => isVideoImportTorrentFile(req.files)) | |
26 | .withMessage( | |
27 | 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' + | |
28 | CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ') | |
29 | ), | |
30 | body('name') | |
31 | .optional() | |
32 | .custom(isVideoNameValid).withMessage('Should have a valid name'), | |
33 | ||
34 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
35 | logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body }) | |
36 | ||
37 | const user = res.locals.oauth.token.User | |
38 | const torrentFile = req.files && req.files['torrentfile'] ? req.files['torrentfile'][0] : undefined | |
39 | ||
40 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
41 | ||
42 | if (req.body.targetUrl && CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) { | |
43 | cleanUpReqFiles(req) | |
44 | return res.status(409) | |
45 | .json({ error: 'HTTP import is not enabled on this instance.' }) | |
46 | .end() | |
47 | } | |
48 | ||
49 | if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) { | |
50 | cleanUpReqFiles(req) | |
51 | return res.status(409) | |
52 | .json({ error: 'Torrent/magnet URI import is not enabled on this instance.' }) | |
53 | .end() | |
54 | } | |
55 | ||
56 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
57 | ||
58 | // Check we have at least 1 required param | |
59 | if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) { | |
60 | cleanUpReqFiles(req) | |
61 | ||
62 | return res.status(400) | |
63 | .json({ error: 'Should have a magnetUri or a targetUrl or a torrent file.' }) | |
64 | .end() | |
65 | } | |
66 | ||
67 | return next() | |
68 | } | |
69 | ]) | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
73 | export { | |
74 | videoImportAddValidator | |
75 | } | |
76 | ||
77 | // --------------------------------------------------------------------------- |