aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/middlewares/videos.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 17:33:44 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 18:03:36 +0200
commit10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch)
tree008f8dad8032684f46105a261b27b2d6f05b36eb /server/helpers/middlewares/videos.ts
parent5e08989ede1a340b9edb94465a11b1e04bf24094 (diff)
downloadPeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.gz
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.zst
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.zip
Move middleware utils in middlewares
helpers modules should not import models
Diffstat (limited to 'server/helpers/middlewares/videos.ts')
-rw-r--r--server/helpers/middlewares/videos.ts125
1 files changed, 0 insertions, 125 deletions
diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts
deleted file mode 100644
index 52b934eb7..000000000
--- a/server/helpers/middlewares/videos.ts
+++ /dev/null
@@ -1,125 +0,0 @@
1import { Response } from 'express'
2import { fetchVideo, VideoFetchType } from '../video'
3import { UserRight } from '../../../shared/models/users'
4import { VideoChannelModel } from '../../models/video/video-channel'
5import {
6 MUser,
7 MUserAccountId,
8 MVideoAccountLight,
9 MVideoFullLight,
10 MVideoIdThumbnail,
11 MVideoImmutable,
12 MVideoThumbnail,
13 MVideoWithRights
14} from '@server/types/models'
15import { VideoFileModel } from '@server/models/video/video-file'
16import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
17
18async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
19 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
20
21 const video = await fetchVideo(id, fetchType, userId)
22
23 if (video === null) {
24 res.fail({
25 status: HttpStatusCode.NOT_FOUND_404,
26 message: 'Video not found'
27 })
28 return false
29 }
30
31 switch (fetchType) {
32 case 'all':
33 res.locals.videoAll = video as MVideoFullLight
34 break
35
36 case 'only-immutable-attributes':
37 res.locals.onlyImmutableVideo = video as MVideoImmutable
38 break
39
40 case 'id':
41 res.locals.videoId = video as MVideoIdThumbnail
42 break
43
44 case 'only-video':
45 res.locals.onlyVideo = video as MVideoThumbnail
46 break
47
48 case 'only-video-with-rights':
49 res.locals.onlyVideoWithRights = video as MVideoWithRights
50 break
51 }
52
53 return true
54}
55
56async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
57 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
58 res.fail({
59 status: HttpStatusCode.NOT_FOUND_404,
60 message: 'VideoFile matching Video not found'
61 })
62 return false
63 }
64
65 return true
66}
67
68async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
69 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
70
71 if (videoChannel === null) {
72 res.fail({ message: 'Unknown video "video channel" for this instance.' })
73 return false
74 }
75
76 // Don't check account id if the user can update any video
77 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
78 res.locals.videoChannel = videoChannel
79 return true
80 }
81
82 if (videoChannel.Account.id !== user.Account.id) {
83 res.fail({
84 message: 'Unknown video "video channel" for this account.'
85 })
86 return false
87 }
88
89 res.locals.videoChannel = videoChannel
90 return true
91}
92
93function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
94 // Retrieve the user who did the request
95 if (onlyOwned && video.isOwned() === false) {
96 res.fail({
97 status: HttpStatusCode.FORBIDDEN_403,
98 message: 'Cannot manage a video of another server.'
99 })
100 return false
101 }
102
103 // Check if the user can delete the video
104 // The user can delete it if he has the right
105 // Or if s/he is the video's account
106 const account = video.VideoChannel.Account
107 if (user.hasRight(right) === false && account.userId !== user.id) {
108 res.fail({
109 status: HttpStatusCode.FORBIDDEN_403,
110 message: 'Cannot manage a video of another user.'
111 })
112 return false
113 }
114
115 return true
116}
117
118// ---------------------------------------------------------------------------
119
120export {
121 doesVideoChannelOfAccountExist,
122 doesVideoExist,
123 doesVideoFileOfVideoExist,
124 checkUserCanManageVideo
125}