2018-08-02 08:34:09 -05:00
|
|
|
import 'multer'
|
2020-01-07 07:56:07 -06:00
|
|
|
import validator from 'validator'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants'
|
2018-08-07 02:54:36 -05:00
|
|
|
import { exists, isFileValid } from './misc'
|
2018-08-02 09:33:29 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { VideoImportModel } from '../../models/video/video-import'
|
2020-12-07 07:32:36 -06:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2018-08-02 08:34:09 -05:00
|
|
|
|
|
|
|
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) {
|
2020-01-31 09:56:52 -06:00
|
|
|
return exists(value) && VIDEO_IMPORT_STATES[value] !== undefined
|
2018-08-02 08:34:09 -05:00
|
|
|
}
|
|
|
|
|
2020-04-07 03:40:52 -05:00
|
|
|
const videoTorrentImportRegex = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT)
|
|
|
|
.concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream
|
|
|
|
.map(m => `(${m})`)
|
|
|
|
.join('|')
|
2018-08-07 02:54:36 -05:00
|
|
|
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 03:26:50 -05:00
|
|
|
async function doesVideoImportExist (id: number, res: express.Response) {
|
2018-08-02 09:33:29 -05:00
|
|
|
const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
|
|
|
|
|
|
|
|
if (!videoImport) {
|
2021-05-31 18:36:53 -05:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Video import not found'
|
|
|
|
})
|
2018-08-02 09:33:29 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.videoImport = videoImport
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-02 08:34:09 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isVideoImportStateValid,
|
2018-08-02 09:33:29 -05:00
|
|
|
isVideoImportTargetUrlValid,
|
2019-03-19 03:26:50 -05:00
|
|
|
doesVideoImportExist,
|
2018-08-07 02:54:36 -05:00
|
|
|
isVideoImportTorrentFile
|
2018-08-02 08:34:09 -05:00
|
|
|
}
|