X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fvideo-imports.ts;h=0063d3337fd96749c65396f3514363577e9587e1;hb=f2eb23cd87cf32b8fe545178143b5f49e06a58da;hp=d8b9bfaff09ba6274e00e79975a0ba6536192671;hpb=9a12f169c15b638fe78cf6e85a1993550a25e404;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts index d8b9bfaff..0063d3337 100644 --- a/server/helpers/custom-validators/video-imports.ts +++ b/server/helpers/custom-validators/video-imports.ts @@ -1,11 +1,10 @@ -import 'express-validator' import 'multer' -import * as validator from 'validator' -import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers' -import { exists } from './misc' +import validator from 'validator' +import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants' +import { exists, isFileValid } from './misc' import * as express from 'express' -import { VideoChannelModel } from '../../models/video/video-channel' import { VideoImportModel } from '../../models/video/video-import' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function isVideoImportTargetUrlValid (url: string) { const isURLOptions = { @@ -22,14 +21,22 @@ function isVideoImportTargetUrlValid (url: string) { } function isVideoImportStateValid (value: any) { - return exists(value) && VIDEO_IMPORT_STATES[ value ] !== undefined + return exists(value) && VIDEO_IMPORT_STATES[value] !== undefined } -async function isVideoImportExist (id: number, res: express.Response) { +const videoTorrentImportRegex = Object.keys(MIMETYPES.TORRENT.MIMETYPE_EXT) + .concat([ 'application/octet-stream' ]) // MacOS sends application/octet-stream + .map(m => `(${m})`) + .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) +} + +async function doesVideoImportExist (id: number, res: express.Response) { const videoImport = await VideoImportModel.loadAndPopulateVideo(id) if (!videoImport) { - res.status(404) + res.status(HttpStatusCode.NOT_FOUND_404) .json({ error: 'Video import not found' }) .end() @@ -45,5 +52,6 @@ async function isVideoImportExist (id: number, res: express.Response) { export { isVideoImportStateValid, isVideoImportTargetUrlValid, - isVideoImportExist + doesVideoImportExist, + isVideoImportTorrentFile }