video-imports.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'express-validator'
  2. import 'multer'
  3. import * as validator from 'validator'
  4. import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
  5. import { exists, isFileValid } from './misc'
  6. import * as express from 'express'
  7. import { VideoImportModel } from '../../models/video/video-import'
  8. function isVideoImportTargetUrlValid (url: string) {
  9. const isURLOptions = {
  10. require_host: true,
  11. require_tld: true,
  12. require_protocol: true,
  13. require_valid_protocol: true,
  14. protocols: [ 'http', 'https' ]
  15. }
  16. return exists(url) &&
  17. validator.isURL('' + url, isURLOptions) &&
  18. validator.isLength('' + url, CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL)
  19. }
  20. function isVideoImportStateValid (value: any) {
  21. return exists(value) && VIDEO_IMPORT_STATES[ value ] !== undefined
  22. }
  23. const videoTorrentImportTypes = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT).map(m => `(${m})`)
  24. const videoTorrentImportRegex = videoTorrentImportTypes.join('|')
  25. function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
  26. return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
  27. }
  28. async function doesVideoImportExist (id: number, res: express.Response) {
  29. const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
  30. if (!videoImport) {
  31. res.status(404)
  32. .json({ error: 'Video import not found' })
  33. .end()
  34. return false
  35. }
  36. res.locals.videoImport = videoImport
  37. return true
  38. }
  39. // ---------------------------------------------------------------------------
  40. export {
  41. isVideoImportStateValid,
  42. isVideoImportTargetUrlValid,
  43. doesVideoImportExist,
  44. isVideoImportTorrentFile
  45. }