video-imports.ts 3.0 KB

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