]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-imports.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-imports.ts
CommitLineData
fbad87b0 1import 'multer'
7cde3b9c 2import validator from 'validator'
74dc3bca 3import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
990b6a0b 4import { exists, isFileValid } from './misc'
299474e8 5import * as express from 'express'
299474e8 6import { VideoImportModel } from '../../models/video/video-import'
2d53be02 7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
fbad87b0
C
8
9function isVideoImportTargetUrlValid (url: string) {
10 const isURLOptions = {
11 require_host: true,
12 require_tld: true,
13 require_protocol: true,
14 require_valid_protocol: true,
15 protocols: [ 'http', 'https' ]
16 }
17
18 return exists(url) &&
19 validator.isURL('' + url, isURLOptions) &&
20 validator.isLength('' + url, CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL)
21}
22
23function isVideoImportStateValid (value: any) {
a1587156 24 return exists(value) && VIDEO_IMPORT_STATES[value] !== undefined
fbad87b0
C
25}
26
edaf5b86
C
27const videoTorrentImportRegex = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT)
28 .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
29 .map(m => `(${m})`)
30 .join('|')
990b6a0b
C
31function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
32 return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
33}
34
0f6acda1 35async function doesVideoImportExist (id: number, res: express.Response) {
299474e8
C
36 const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
37
38 if (!videoImport) {
2d53be02 39 res.status(HttpStatusCode.NOT_FOUND_404)
299474e8
C
40 .json({ error: 'Video import not found' })
41 .end()
42
43 return false
44 }
45
46 res.locals.videoImport = videoImport
47 return true
48}
49
fbad87b0
C
50// ---------------------------------------------------------------------------
51
52export {
53 isVideoImportStateValid,
299474e8 54 isVideoImportTargetUrlValid,
0f6acda1 55 doesVideoImportExist,
990b6a0b 56 isVideoImportTorrentFile
fbad87b0 57}