]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/videos.ts
Update server dependencies
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / videos.ts
index dd9d62d24192e315bc9dbb3a68aa5590895e3abc..5f75ec27c8a0b43f29ffbaf1897619d5cd722e40 100644 (file)
@@ -1,40 +1,39 @@
-import { Response } from 'express'
-import 'express-validator'
-import { values } from 'lodash'
-import 'multer'
-import * as validator from 'validator'
-import { UserRight, VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared'
+import { UploadFilesForCheck } from 'express'
+import { decode as magnetUriDecode } from 'magnet-uri'
+import validator from 'validator'
+import { VideoFilter, VideoInclude, VideoPrivacy, VideoRateType } from '@shared/models'
 import {
-  CONSTRAINTS_FIELDS, MIMETYPES,
+  CONSTRAINTS_FIELDS,
+  MIMETYPES,
   VIDEO_CATEGORIES,
   VIDEO_LICENCES,
+  VIDEO_LIVE,
   VIDEO_PRIVACIES,
   VIDEO_RATE_TYPES,
   VIDEO_STATES
-} from '../../initializers'
-import { VideoModel } from '../../models/video/video'
+} from '../../initializers/constants'
 import { exists, isArray, isDateValid, isFileValid } from './misc'
-import { VideoChannelModel } from '../../models/video/video-channel'
-import { UserModel } from '../../models/account/user'
-import * as magnetUtil from 'magnet-uri'
-import { fetchVideo, VideoFetchType } from '../video'
 
 const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
 
 function isVideoFilterValid (filter: VideoFilter) {
-  return filter === 'local' || filter === 'all-local'
+  return filter === 'local' || filter === 'all-local' || filter === 'all'
+}
+
+function isVideoIncludeValid (include: VideoInclude) {
+  return exists(include) && validator.isInt('' + include)
 }
 
 function isVideoCategoryValid (value: any) {
-  return value === null || VIDEO_CATEGORIES[ value ] !== undefined
+  return value === null || VIDEO_CATEGORIES[value] !== undefined
 }
 
 function isVideoStateValid (value: any) {
-  return exists(value) && VIDEO_STATES[ value ] !== undefined
+  return exists(value) && VIDEO_STATES[value] !== undefined
 }
 
 function isVideoLicenceValid (value: any) {
-  return value === null || VIDEO_LICENCES[ value ] !== undefined
+  return value === null || VIDEO_LICENCES[value] !== undefined
 }
 
 function isVideoLanguageValid (value: any) {
@@ -46,10 +45,6 @@ function isVideoDurationValid (value: string) {
   return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
 }
 
-function isVideoTruncatedDescriptionValid (value: string) {
-  return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.TRUNCATED_DESCRIPTION)
-}
-
 function isVideoDescriptionValid (value: string) {
   return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
 }
@@ -66,7 +61,7 @@ function isVideoTagValid (tag: string) {
   return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
 }
 
-function isVideoTagsValid (tags: string[]) {
+function areVideoTagsValid (tags: string[]) {
   return tags === null || (
     isArray(tags) &&
     validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
@@ -78,20 +73,22 @@ function isVideoViewsValid (value: string) {
   return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
 }
 
+const ratingTypes = new Set(Object.values(VIDEO_RATE_TYPES))
 function isVideoRatingTypeValid (value: string) {
-  return value === 'none' || values(VIDEO_RATE_TYPES).indexOf(value as VideoRateType) !== -1
+  return value === 'none' || ratingTypes.has(value as VideoRateType)
 }
 
 function isVideoFileExtnameValid (value: string) {
-  return exists(value) && MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined
+  return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined)
 }
 
-function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
-  const videoFileTypesRegex = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
-                                    .map(m => `(${m})`)
-                                    .join('|')
-
-  return isFileValid(files, videoFileTypesRegex, 'videofile', null)
+function isVideoFileMimeTypeValid (files: UploadFilesForCheck, field = 'videofile') {
+  return isFileValid({
+    files,
+    mimeTypeRegex: MIMETYPES.VIDEO.MIMETYPES_REGEX,
+    field,
+    maxSize: null
+  })
 }
 
 const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
@@ -99,20 +96,22 @@ const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
                                           .join('|')
 const videoImageTypesRegex = `image/(${videoImageTypes})`
 
-function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
-  return isFileValid(files, videoImageTypesRegex, field, CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max, true)
+function isVideoImageValid (files: UploadFilesForCheck, field: string, optional = true) {
+  return isFileValid({
+    files,
+    mimeTypeRegex: videoImageTypesRegex,
+    field,
+    maxSize: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max,
+    optional
+  })
 }
 
 function isVideoPrivacyValid (value: number) {
-  return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
+  return VIDEO_PRIVACIES[value] !== undefined
 }
 
 function isScheduleVideoUpdatePrivacyValid (value: number) {
-  return validator.isInt(value + '') &&
-    (
-      value === VideoPrivacy.UNLISTED ||
-      value === VideoPrivacy.PUBLIC
-    )
+  return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL
 }
 
 function isVideoOriginallyPublishedAtValid (value: string | null) {
@@ -138,107 +137,36 @@ function isVideoFileSizeValid (value: string) {
 function isVideoMagnetUriValid (value: string) {
   if (!exists(value)) return false
 
-  const parsed = magnetUtil.decode(value)
+  const parsed = magnetUriDecode(value)
   return parsed && isVideoFileInfoHashValid(parsed.infoHash)
 }
 
-function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
-  // Retrieve the user who did the request
-  if (video.isOwned() === false) {
-    res.status(403)
-       .json({ error: 'Cannot manage a video of another server.' })
-       .end()
-    return false
-  }
-
-  // Check if the user can delete the video
-  // The user can delete it if he has the right
-  // Or if s/he is the video's account
-  const account = video.VideoChannel.Account
-  if (user.hasRight(right) === false && account.userId !== user.id) {
-    res.status(403)
-       .json({ error: 'Cannot manage a video of another user.' })
-       .end()
-    return false
-  }
-
-  return true
-}
-
-async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
-  const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
-
-  const video = await fetchVideo(id, fetchType, userId)
-
-  if (video === null) {
-    res.status(404)
-       .json({ error: 'Video not found' })
-       .end()
-
-    return false
-  }
-
-  if (fetchType !== 'none') res.locals.video = video
-  return true
-}
-
-async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
-  if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
-    const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
-    if (videoChannel === null) {
-      res.status(400)
-         .json({ error: 'Unknown video `video channel` on this instance.' })
-         .end()
-
-      return false
-    }
-
-    res.locals.videoChannel = videoChannel
-    return true
-  }
-
-  const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
-  if (videoChannel === null) {
-    res.status(400)
-       .json({ error: 'Unknown video `video channel` for this account.' })
-       .end()
-
-    return false
-  }
-
-  res.locals.videoChannel = videoChannel
-  return true
-}
-
 // ---------------------------------------------------------------------------
 
 export {
   isVideoCategoryValid,
-  checkUserCanManageVideo,
   isVideoLicenceValid,
   isVideoLanguageValid,
-  isVideoTruncatedDescriptionValid,
   isVideoDescriptionValid,
   isVideoFileInfoHashValid,
   isVideoNameValid,
-  isVideoTagsValid,
+  areVideoTagsValid,
   isVideoFPSResolutionValid,
   isScheduleVideoUpdatePrivacyValid,
   isVideoOriginallyPublishedAtValid,
-  isVideoFile,
   isVideoMagnetUriValid,
   isVideoStateValid,
+  isVideoIncludeValid,
   isVideoViewsValid,
   isVideoRatingTypeValid,
   isVideoFileExtnameValid,
+  isVideoFileMimeTypeValid,
   isVideoDurationValid,
   isVideoTagValid,
   isVideoPrivacyValid,
   isVideoFileResolutionValid,
   isVideoFileSizeValid,
-  doesVideoExist,
-  isVideoImage,
-  doesVideoChannelOfAccountExist,
+  isVideoImageValid,
   isVideoSupportValid,
   isVideoFilterValid
 }