]>
Commit | Line | Data |
---|---|---|
b46cf4b9 C |
1 | import express from 'express' |
2 | import { MUser, MVideo } from '@server/types/models' | |
3 | import { HttpStatusCode, UserRight } from '../../../../shared' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' | |
6 | ||
7 | const videoFilesDeleteWebTorrentValidator = [ | |
8 | isValidVideoIdParam('id'), | |
9 | ||
10 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
11 | logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params }) | |
12 | ||
13 | if (areValidationErrors(req, res)) return | |
14 | if (!await doesVideoExist(req.params.id, res)) return | |
15 | ||
16 | const video = res.locals.videoAll | |
17 | const user = res.locals.oauth.token.User | |
18 | ||
19 | if (!checkUserCanDeleteFiles(user, res)) return | |
20 | if (!checkLocalVideo(video, res)) return | |
21 | ||
22 | if (!video.hasWebTorrentFiles()) { | |
23 | return res.fail({ | |
24 | status: HttpStatusCode.BAD_REQUEST_400, | |
25 | message: 'This video does not have WebTorrent files' | |
26 | }) | |
27 | } | |
28 | ||
29 | if (!video.getHLSPlaylist()) { | |
30 | return res.fail({ | |
31 | status: HttpStatusCode.BAD_REQUEST_400, | |
32 | message: 'Cannot delete WebTorrent files since this video does not have HLS playlist' | |
33 | }) | |
34 | } | |
35 | ||
36 | return next() | |
37 | } | |
38 | ] | |
39 | ||
40 | const videoFilesDeleteHLSValidator = [ | |
41 | isValidVideoIdParam('id'), | |
42 | ||
43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
44 | logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params }) | |
45 | ||
46 | if (areValidationErrors(req, res)) return | |
47 | if (!await doesVideoExist(req.params.id, res)) return | |
48 | ||
49 | const video = res.locals.videoAll | |
50 | const user = res.locals.oauth.token.User | |
51 | ||
52 | if (!checkUserCanDeleteFiles(user, res)) return | |
53 | if (!checkLocalVideo(video, res)) return | |
54 | ||
55 | if (!video.getHLSPlaylist()) { | |
56 | return res.fail({ | |
57 | status: HttpStatusCode.BAD_REQUEST_400, | |
58 | message: 'This video does not have HLS files' | |
59 | }) | |
60 | } | |
61 | ||
62 | if (!video.hasWebTorrentFiles()) { | |
63 | return res.fail({ | |
64 | status: HttpStatusCode.BAD_REQUEST_400, | |
65 | message: 'Cannot delete HLS playlist since this video does not have WebTorrent files' | |
66 | }) | |
67 | } | |
68 | ||
69 | return next() | |
70 | } | |
71 | ] | |
72 | ||
73 | export { | |
74 | videoFilesDeleteWebTorrentValidator, | |
75 | videoFilesDeleteHLSValidator | |
76 | } | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | ||
80 | function checkLocalVideo (video: MVideo, res: express.Response) { | |
81 | if (video.remote) { | |
82 | res.fail({ | |
83 | status: HttpStatusCode.BAD_REQUEST_400, | |
84 | message: 'Cannot delete files of remote video' | |
85 | }) | |
86 | ||
87 | return false | |
88 | } | |
89 | ||
90 | return true | |
91 | } | |
92 | ||
93 | function checkUserCanDeleteFiles (user: MUser, res: express.Response) { | |
94 | if (user.hasRight(UserRight.MANAGE_VIDEO_FILES) !== true) { | |
95 | res.fail({ | |
96 | status: HttpStatusCode.FORBIDDEN_403, | |
97 | message: 'User cannot update video files' | |
98 | }) | |
99 | ||
100 | return false | |
101 | } | |
102 | ||
103 | return true | |
104 | } |