]>
Commit | Line | Data |
---|---|---|
b46cf4b9 | 1 | import express from 'express' |
ad5db104 | 2 | import { MVideo } from '@server/types/models' |
d17c7b4e | 3 | import { HttpStatusCode } from '@shared/models' |
b46cf4b9 C |
4 | import { logger } from '../../../helpers/logger' |
5 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' | |
1bb4c9ab C |
6 | import { isIdValid } from '@server/helpers/custom-validators/misc' |
7 | import { param } from 'express-validator' | |
b46cf4b9 C |
8 | |
9 | const videoFilesDeleteWebTorrentValidator = [ | |
10 | isValidVideoIdParam('id'), | |
11 | ||
12 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
13 | logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params }) | |
14 | ||
15 | if (areValidationErrors(req, res)) return | |
16 | if (!await doesVideoExist(req.params.id, res)) return | |
17 | ||
18 | const video = res.locals.videoAll | |
b46cf4b9 | 19 | |
b46cf4b9 C |
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 | ||
1bb4c9ab C |
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 | ||
b46cf4b9 C |
77 | const videoFilesDeleteHLSValidator = [ |
78 | isValidVideoIdParam('id'), | |
79 | ||
80 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
81 | logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params }) | |
82 | ||
83 | if (areValidationErrors(req, res)) return | |
84 | if (!await doesVideoExist(req.params.id, res)) return | |
85 | ||
86 | const video = res.locals.videoAll | |
b46cf4b9 | 87 | |
b46cf4b9 C |
88 | if (!checkLocalVideo(video, res)) return |
89 | ||
90 | if (!video.getHLSPlaylist()) { | |
91 | return res.fail({ | |
92 | status: HttpStatusCode.BAD_REQUEST_400, | |
93 | message: 'This video does not have HLS files' | |
94 | }) | |
95 | } | |
96 | ||
97 | if (!video.hasWebTorrentFiles()) { | |
98 | return res.fail({ | |
99 | status: HttpStatusCode.BAD_REQUEST_400, | |
100 | message: 'Cannot delete HLS playlist since this video does not have WebTorrent files' | |
101 | }) | |
102 | } | |
103 | ||
104 | return next() | |
105 | } | |
106 | ] | |
107 | ||
1bb4c9ab C |
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 | ||
b46cf4b9 C |
151 | export { |
152 | videoFilesDeleteWebTorrentValidator, | |
1bb4c9ab C |
153 | videoFilesDeleteWebTorrentFileValidator, |
154 | ||
155 | videoFilesDeleteHLSValidator, | |
156 | videoFilesDeleteHLSFileValidator | |
b46cf4b9 C |
157 | } |
158 | ||
159 | // --------------------------------------------------------------------------- | |
160 | ||
161 | function checkLocalVideo (video: MVideo, res: express.Response) { | |
162 | if (video.remote) { | |
163 | res.fail({ | |
164 | status: HttpStatusCode.BAD_REQUEST_400, | |
165 | message: 'Cannot delete files of remote video' | |
166 | }) | |
167 | ||
168 | return false | |
169 | } | |
170 | ||
171 | return true | |
172 | } |