]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/shared/videos.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / videos.ts
1 import { Response } from 'express'
2 import { loadVideo, VideoLoadType } from '@server/lib/model-loaders'
3 import { VideoChannelModel } from '@server/models/video/video-channel'
4 import { VideoFileModel } from '@server/models/video/video-file'
5 import {
6 MUser,
7 MUserAccountId,
8 MVideoAccountLight,
9 MVideoFormattableDetails,
10 MVideoFullLight,
11 MVideoId,
12 MVideoImmutable,
13 MVideoThumbnail
14 } from '@server/types/models'
15 import { HttpStatusCode, UserRight } from '@shared/models'
16
17 async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
18 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
19
20 const video = await loadVideo(id, fetchType, userId)
21
22 if (video === null) {
23 res.fail({
24 status: HttpStatusCode.NOT_FOUND_404,
25 message: 'Video not found'
26 })
27 return false
28 }
29
30 switch (fetchType) {
31 case 'for-api':
32 res.locals.videoAPI = video as MVideoFormattableDetails
33 break
34
35 case 'all':
36 res.locals.videoAll = video as MVideoFullLight
37 break
38
39 case 'only-immutable-attributes':
40 res.locals.onlyImmutableVideo = video as MVideoImmutable
41 break
42
43 case 'id':
44 res.locals.videoId = video as MVideoId
45 break
46
47 case 'only-video':
48 res.locals.onlyVideo = video as MVideoThumbnail
49 break
50 }
51
52 return true
53 }
54
55 async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
56 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
57 res.fail({
58 status: HttpStatusCode.NOT_FOUND_404,
59 message: 'VideoFile matching Video not found'
60 })
61 return false
62 }
63
64 return true
65 }
66
67 async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
68 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
69
70 if (videoChannel === null) {
71 res.fail({ message: 'Unknown video "video channel" for this instance.' })
72 return false
73 }
74
75 // Don't check account id if the user can update any video
76 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
77 res.locals.videoChannel = videoChannel
78 return true
79 }
80
81 if (videoChannel.Account.id !== user.Account.id) {
82 res.fail({
83 message: 'Unknown video "video channel" for this account.'
84 })
85 return false
86 }
87
88 res.locals.videoChannel = videoChannel
89 return true
90 }
91
92 function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
93 // Retrieve the user who did the request
94 if (onlyOwned && video.isOwned() === false) {
95 res.fail({
96 status: HttpStatusCode.FORBIDDEN_403,
97 message: 'Cannot manage a video of another server.'
98 })
99 return false
100 }
101
102 // Check if the user can delete the video
103 // The user can delete it if he has the right
104 // Or if s/he is the video's account
105 const account = video.VideoChannel.Account
106 if (user.hasRight(right) === false && account.userId !== user.id) {
107 res.fail({
108 status: HttpStatusCode.FORBIDDEN_403,
109 message: 'Cannot manage a video of another user.'
110 })
111 return false
112 }
113
114 return true
115 }
116
117 // ---------------------------------------------------------------------------
118
119 export {
120 doesVideoChannelOfAccountExist,
121 doesVideoExist,
122 doesVideoFileOfVideoExist,
123 checkUserCanManageVideo
124 }