PeerTube/server/helpers/custom-validators/video-imports.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import 'multer'
2020-01-07 14:56:07 +01:00
import validator from 'validator'
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
2018-08-07 09:54:36 +02:00
import { exists, isFileValid } from './misc'
2018-08-02 16:33:29 +02:00
import * as express from 'express'
import { VideoImportModel } from '../../models/video/video-import'
function isVideoImportTargetUrlValid (url: string) {
const isURLOptions = {
require_host: true,
require_tld: true,
require_protocol: true,
require_valid_protocol: true,
protocols: [ 'http', 'https' ]
}
return exists(url) &&
validator.isURL('' + url, isURLOptions) &&
validator.isLength('' + url, CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL)
}
function isVideoImportStateValid (value: any) {
return exists(value) && VIDEO_IMPORT_STATES[ value ] !== undefined
}
2018-12-11 14:52:50 +01:00
const videoTorrentImportTypes = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT).map(m => `(${m})`)
2018-08-07 09:54:36 +02:00
const videoTorrentImportRegex = videoTorrentImportTypes.join('|')
function isVideoImportTorrentFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
return isFileValid(files, videoTorrentImportRegex, 'torrentfile', CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.FILE_SIZE.max, true)
}
2019-03-19 09:26:50 +01:00
async function doesVideoImportExist (id: number, res: express.Response) {
2018-08-02 16:33:29 +02:00
const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
if (!videoImport) {
res.status(404)
.json({ error: 'Video import not found' })
.end()
return false
}
res.locals.videoImport = videoImport
return true
}
// ---------------------------------------------------------------------------
export {
isVideoImportStateValid,
2018-08-02 16:33:29 +02:00
isVideoImportTargetUrlValid,
2019-03-19 09:26:50 +01:00
doesVideoImportExist,
2018-08-07 09:54:36 +02:00
isVideoImportTorrentFile
}