]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-imports.ts
Disable openapi generation
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-imports.ts
1 import 'multer'
2 import validator from 'validator'
3 import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
4 import { exists, isFileValid } from './misc'
5
6 function isVideoImportTargetUrlValid (url: string) {
7 const isURLOptions = {
8 require_host: true,
9 require_tld: true,
10 require_protocol: true,
11 require_valid_protocol: true,
12 protocols: [ 'http', 'https' ]
13 }
14
15 return exists(url) &&
16 validator.isURL('' + url, isURLOptions) &&
17 validator.isLength('' + url, CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL)
18 }
19
20 function isVideoImportStateValid (value: any) {
21 return exists(value) && VIDEO_IMPORT_STATES[value] !== undefined
22 }
23
24 const videoTorrentImportRegex = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT)
25 .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
26 .map(m => `(${m})`)
27 .join('|')
28 function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
29 return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
30 }
31
32 // ---------------------------------------------------------------------------
33
34 export {
35 isVideoImportStateValid,
36 isVideoImportTargetUrlValid,
37 isVideoImportTorrentFile
38 }