diff options
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r-- | server/middlewares/validators/videos/index.ts | 1 | ||||
-rw-r--r-- | server/middlewares/validators/videos/video-files.ts | 104 |
2 files changed, 105 insertions, 0 deletions
diff --git a/server/middlewares/validators/videos/index.ts b/server/middlewares/validators/videos/index.ts index 369c2c9b6..fd1d58093 100644 --- a/server/middlewares/validators/videos/index.ts +++ b/server/middlewares/validators/videos/index.ts | |||
@@ -2,6 +2,7 @@ export * from './video-blacklist' | |||
2 | export * from './video-captions' | 2 | export * from './video-captions' |
3 | export * from './video-channels' | 3 | export * from './video-channels' |
4 | export * from './video-comments' | 4 | export * from './video-comments' |
5 | export * from './video-files' | ||
5 | export * from './video-imports' | 6 | export * from './video-imports' |
6 | export * from './video-live' | 7 | export * from './video-live' |
7 | export * from './video-ownership-changes' | 8 | export * from './video-ownership-changes' |
diff --git a/server/middlewares/validators/videos/video-files.ts b/server/middlewares/validators/videos/video-files.ts new file mode 100644 index 000000000..282594ab6 --- /dev/null +++ b/server/middlewares/validators/videos/video-files.ts | |||
@@ -0,0 +1,104 @@ | |||
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 | } | ||