]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/videos.ts
esModuleInterop to true
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / videos.ts
CommitLineData
3e753302 1import { Response } from 'express'
868fce62 2import { loadVideo, VideoLoadType } from '@server/lib/model-loaders'
10363c74
C
3import { VideoChannelModel } from '@server/models/video/video-channel'
4import { VideoFileModel } from '@server/models/video/video-file'
7eba5e1f
C
5import {
6 MUser,
7 MUserAccountId,
8 MVideoAccountLight,
ca4b4b2e 9 MVideoFormattableDetails,
7eba5e1f 10 MVideoFullLight,
71d4af1e 11 MVideoId,
7eba5e1f 12 MVideoImmutable,
20a206c3 13 MVideoThumbnail
26d6bf65 14} from '@server/types/models'
4c7e60bc 15import { HttpStatusCode, UserRight } from '@shared/models'
3e753302 16
868fce62 17async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
3e753302
C
18 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
19
868fce62 20 const video = await loadVideo(id, fetchType, userId)
3e753302
C
21
22 if (video === null) {
76148b27
RK
23 res.fail({
24 status: HttpStatusCode.NOT_FOUND_404,
25 message: 'Video not found'
26 })
3e753302
C
27 return false
28 }
29
453e83ea 30 switch (fetchType) {
ca4b4b2e
C
31 case 'for-api':
32 res.locals.videoAPI = video as MVideoFormattableDetails
33 break
34
453e83ea
C
35 case 'all':
36 res.locals.videoAll = video as MVideoFullLight
37 break
38
7eba5e1f
C
39 case 'only-immutable-attributes':
40 res.locals.onlyImmutableVideo = video as MVideoImmutable
41 break
42
453e83ea 43 case 'id':
71d4af1e 44 res.locals.videoId = video as MVideoId
453e83ea
C
45 break
46
47 case 'only-video':
0283eaac 48 res.locals.onlyVideo = video as MVideoThumbnail
453e83ea 49 break
453e83ea
C
50 }
51
3e753302
C
52 return true
53}
54
8319d6ae
RK
55async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
56 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
76148b27
RK
57 res.fail({
58 status: HttpStatusCode.NOT_FOUND_404,
59 message: 'VideoFile matching Video not found'
60 })
8319d6ae
RK
61 return false
62 }
63
64 return true
65}
66
453e83ea 67async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
2cb03dc1 68 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
3e753302 69
2cb03dc1 70 if (videoChannel === null) {
76148b27 71 res.fail({ message: 'Unknown video "video channel" for this instance.' })
2cb03dc1
C
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) {
3e753302
C
77 res.locals.videoChannel = videoChannel
78 return true
79 }
80
2cb03dc1 81 if (videoChannel.Account.id !== user.Account.id) {
76148b27
RK
82 res.fail({
83 message: 'Unknown video "video channel" for this account.'
84 })
3e753302
C
85 return false
86 }
87
88 res.locals.videoChannel = videoChannel
89 return true
90}
91
af4ae64f 92function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
3e753302 93 // Retrieve the user who did the request
af4ae64f 94 if (onlyOwned && video.isOwned() === false) {
76148b27
RK
95 res.fail({
96 status: HttpStatusCode.FORBIDDEN_403,
97 message: 'Cannot manage a video of another server.'
98 })
3e753302
C
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) {
76148b27
RK
107 res.fail({
108 status: HttpStatusCode.FORBIDDEN_403,
109 message: 'Cannot manage a video of another user.'
110 })
3e753302
C
111 return false
112 }
113
114 return true
115}
116
117// ---------------------------------------------------------------------------
118
119export {
120 doesVideoChannelOfAccountExist,
121 doesVideoExist,
8319d6ae 122 doesVideoFileOfVideoExist,
3e753302
C
123 checkUserCanManageVideo
124}