]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/middlewares/videos.ts
Added translation using Weblate (Hebrew)
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / videos.ts
index ceb1058ecb7c4e090705a6cbd0c3513eb7cb1c09..77a48a46720a0f2ded170c9277c85357f91a2015 100644 (file)
@@ -1,9 +1,18 @@
 import { Response } from 'express'
 import { fetchVideo, VideoFetchType } from '../video'
-import { UserModel } from '../../models/account/user'
 import { UserRight } from '../../../shared/models/users'
 import { VideoChannelModel } from '../../models/video/video-channel'
-import { VideoModel } from '../../models/video/video'
+import {
+  MUser,
+  MUserAccountId,
+  MVideoAccountLight,
+  MVideoFullLight,
+  MVideoIdThumbnail,
+  MVideoImmutable,
+  MVideoThumbnail,
+  MVideoWithRights
+} from '@server/types/models'
+import { VideoFileModel } from '@server/models/video/video-file'
 
 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
   const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
@@ -18,11 +27,44 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi
     return false
   }
 
-  if (fetchType !== 'none') res.locals.video = video
+  switch (fetchType) {
+    case 'all':
+      res.locals.videoAll = video as MVideoFullLight
+      break
+
+    case 'only-immutable-attributes':
+      res.locals.onlyImmutableVideo = video as MVideoImmutable
+      break
+
+    case 'id':
+      res.locals.videoId = video as MVideoIdThumbnail
+      break
+
+    case 'only-video':
+      res.locals.onlyVideo = video as MVideoThumbnail
+      break
+
+    case 'only-video-with-rights':
+      res.locals.onlyVideoWithRights = video as MVideoWithRights
+      break
+  }
+
+  return true
+}
+
+async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
+  if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
+    res.status(404)
+       .json({ error: 'VideoFile matching Video not found' })
+       .end()
+
+    return false
+  }
+
   return true
 }
 
-async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
+async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
   if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
     const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
     if (videoChannel === null) {
@@ -50,7 +92,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: UserMode
   return true
 }
 
-function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
+function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response) {
   // Retrieve the user who did the request
   if (video.isOwned() === false) {
     res.status(403)
@@ -78,5 +120,6 @@ function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: Use
 export {
   doesVideoChannelOfAccountExist,
   doesVideoExist,
+  doesVideoFileOfVideoExist,
   checkUserCanManageVideo
 }