diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-29 14:50:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-08-01 14:55:10 +0200 |
commit | 1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7 (patch) | |
tree | a6554ee0a3ccc2ae402665b2ecf57bb38fd0ed72 /server/middlewares/validators/videos | |
parent | 12d84abeca4917d2f1e3f308010bfcd56d37cb7c (diff) | |
download | PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.tar.gz PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.tar.zst PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.zip |
Add ability to delete a specific video file
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r-- | server/middlewares/validators/videos/video-files.ts | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/server/middlewares/validators/videos/video-files.ts b/server/middlewares/validators/videos/video-files.ts index 35b0ac757..b3db3f4f7 100644 --- a/server/middlewares/validators/videos/video-files.ts +++ b/server/middlewares/validators/videos/video-files.ts | |||
@@ -3,6 +3,8 @@ import { MVideo } from '@server/types/models' | |||
3 | import { HttpStatusCode } from '@shared/models' | 3 | import { HttpStatusCode } from '@shared/models' |
4 | import { logger } from '../../../helpers/logger' | 4 | import { logger } from '../../../helpers/logger' |
5 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' | 5 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' |
6 | import { isIdValid } from '@server/helpers/custom-validators/misc' | ||
7 | import { param } from 'express-validator' | ||
6 | 8 | ||
7 | const videoFilesDeleteWebTorrentValidator = [ | 9 | const videoFilesDeleteWebTorrentValidator = [ |
8 | isValidVideoIdParam('id'), | 10 | isValidVideoIdParam('id'), |
@@ -35,6 +37,43 @@ const videoFilesDeleteWebTorrentValidator = [ | |||
35 | } | 37 | } |
36 | ] | 38 | ] |
37 | 39 | ||
40 | const videoFilesDeleteWebTorrentFileValidator = [ | ||
41 | isValidVideoIdParam('id'), | ||
42 | |||
43 | param('videoFileId') | ||
44 | .custom(isIdValid).withMessage('Should have a valid file id'), | ||
45 | |||
46 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
47 | logger.debug('Checking videoFilesDeleteWebTorrentFile parameters', { parameters: req.params }) | ||
48 | |||
49 | if (areValidationErrors(req, res)) return | ||
50 | if (!await doesVideoExist(req.params.id, res)) return | ||
51 | |||
52 | const video = res.locals.videoAll | ||
53 | |||
54 | if (!checkLocalVideo(video, res)) return | ||
55 | |||
56 | const files = video.VideoFiles | ||
57 | if (!files.find(f => f.id === +req.params.videoFileId)) { | ||
58 | return res.fail({ | ||
59 | status: HttpStatusCode.NOT_FOUND_404, | ||
60 | message: 'This video does not have this WebTorrent file id' | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | if (files.length === 1 && !video.getHLSPlaylist()) { | ||
65 | return res.fail({ | ||
66 | status: HttpStatusCode.BAD_REQUEST_400, | ||
67 | message: 'Cannot delete WebTorrent files since this video does not have HLS playlist' | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | return next() | ||
72 | } | ||
73 | ] | ||
74 | |||
75 | // --------------------------------------------------------------------------- | ||
76 | |||
38 | const videoFilesDeleteHLSValidator = [ | 77 | const videoFilesDeleteHLSValidator = [ |
39 | isValidVideoIdParam('id'), | 78 | isValidVideoIdParam('id'), |
40 | 79 | ||
@@ -66,9 +105,55 @@ const videoFilesDeleteHLSValidator = [ | |||
66 | } | 105 | } |
67 | ] | 106 | ] |
68 | 107 | ||
108 | const videoFilesDeleteHLSFileValidator = [ | ||
109 | isValidVideoIdParam('id'), | ||
110 | |||
111 | param('videoFileId') | ||
112 | .custom(isIdValid).withMessage('Should have a valid file id'), | ||
113 | |||
114 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
115 | logger.debug('Checking videoFilesDeleteHLSFile parameters', { parameters: req.params }) | ||
116 | |||
117 | if (areValidationErrors(req, res)) return | ||
118 | if (!await doesVideoExist(req.params.id, res)) return | ||
119 | |||
120 | const video = res.locals.videoAll | ||
121 | |||
122 | if (!checkLocalVideo(video, res)) return | ||
123 | |||
124 | if (!video.getHLSPlaylist()) { | ||
125 | return res.fail({ | ||
126 | status: HttpStatusCode.BAD_REQUEST_400, | ||
127 | message: 'This video does not have HLS files' | ||
128 | }) | ||
129 | } | ||
130 | |||
131 | const hlsFiles = video.getHLSPlaylist().VideoFiles | ||
132 | if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) { | ||
133 | return res.fail({ | ||
134 | status: HttpStatusCode.NOT_FOUND_404, | ||
135 | message: 'This HLS playlist does not have this file id' | ||
136 | }) | ||
137 | } | ||
138 | |||
139 | // Last file to delete | ||
140 | if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) { | ||
141 | return res.fail({ | ||
142 | status: HttpStatusCode.BAD_REQUEST_400, | ||
143 | message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files' | ||
144 | }) | ||
145 | } | ||
146 | |||
147 | return next() | ||
148 | } | ||
149 | ] | ||
150 | |||
69 | export { | 151 | export { |
70 | videoFilesDeleteWebTorrentValidator, | 152 | videoFilesDeleteWebTorrentValidator, |
71 | videoFilesDeleteHLSValidator | 153 | videoFilesDeleteWebTorrentFileValidator, |
154 | |||
155 | videoFilesDeleteHLSValidator, | ||
156 | videoFilesDeleteHLSFileValidator | ||
72 | } | 157 | } |
73 | 158 | ||
74 | // --------------------------------------------------------------------------- | 159 | // --------------------------------------------------------------------------- |