]>
Commit | Line | Data |
---|---|---|
b46cf4b9 | 1 | import express from 'express' |
a85d5303 C |
2 | import { param } from 'express-validator' |
3 | import { isIdValid } from '@server/helpers/custom-validators/misc' | |
ad5db104 | 4 | import { MVideo } from '@server/types/models' |
d17c7b4e | 5 | import { HttpStatusCode } from '@shared/models' |
b46cf4b9 C |
6 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' |
7 | ||
8 | const videoFilesDeleteWebTorrentValidator = [ | |
9 | isValidVideoIdParam('id'), | |
10 | ||
11 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
b46cf4b9 C |
12 | if (areValidationErrors(req, res)) return |
13 | if (!await doesVideoExist(req.params.id, res)) return | |
14 | ||
15 | const video = res.locals.videoAll | |
b46cf4b9 | 16 | |
b46cf4b9 C |
17 | if (!checkLocalVideo(video, res)) return |
18 | ||
19 | if (!video.hasWebTorrentFiles()) { | |
20 | return res.fail({ | |
21 | status: HttpStatusCode.BAD_REQUEST_400, | |
22 | message: 'This video does not have WebTorrent files' | |
23 | }) | |
24 | } | |
25 | ||
26 | if (!video.getHLSPlaylist()) { | |
27 | return res.fail({ | |
28 | status: HttpStatusCode.BAD_REQUEST_400, | |
29 | message: 'Cannot delete WebTorrent files since this video does not have HLS playlist' | |
30 | }) | |
31 | } | |
32 | ||
33 | return next() | |
34 | } | |
35 | ] | |
36 | ||
1bb4c9ab C |
37 | const videoFilesDeleteWebTorrentFileValidator = [ |
38 | isValidVideoIdParam('id'), | |
39 | ||
40 | param('videoFileId') | |
396f6f01 | 41 | .custom(isIdValid), |
1bb4c9ab C |
42 | |
43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
1bb4c9ab C |
44 | if (areValidationErrors(req, res)) return |
45 | if (!await doesVideoExist(req.params.id, res)) return | |
46 | ||
47 | const video = res.locals.videoAll | |
48 | ||
49 | if (!checkLocalVideo(video, res)) return | |
50 | ||
51 | const files = video.VideoFiles | |
52 | if (!files.find(f => f.id === +req.params.videoFileId)) { | |
53 | return res.fail({ | |
54 | status: HttpStatusCode.NOT_FOUND_404, | |
55 | message: 'This video does not have this WebTorrent file id' | |
56 | }) | |
57 | } | |
58 | ||
59 | if (files.length === 1 && !video.getHLSPlaylist()) { | |
60 | return res.fail({ | |
61 | status: HttpStatusCode.BAD_REQUEST_400, | |
62 | message: 'Cannot delete WebTorrent files since this video does not have HLS playlist' | |
63 | }) | |
64 | } | |
65 | ||
66 | return next() | |
67 | } | |
68 | ] | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
b46cf4b9 C |
72 | const videoFilesDeleteHLSValidator = [ |
73 | isValidVideoIdParam('id'), | |
74 | ||
75 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
b46cf4b9 C |
76 | if (areValidationErrors(req, res)) return |
77 | if (!await doesVideoExist(req.params.id, res)) return | |
78 | ||
79 | const video = res.locals.videoAll | |
b46cf4b9 | 80 | |
b46cf4b9 C |
81 | if (!checkLocalVideo(video, res)) return |
82 | ||
83 | if (!video.getHLSPlaylist()) { | |
84 | return res.fail({ | |
85 | status: HttpStatusCode.BAD_REQUEST_400, | |
86 | message: 'This video does not have HLS files' | |
87 | }) | |
88 | } | |
89 | ||
90 | if (!video.hasWebTorrentFiles()) { | |
91 | return res.fail({ | |
92 | status: HttpStatusCode.BAD_REQUEST_400, | |
93 | message: 'Cannot delete HLS playlist since this video does not have WebTorrent files' | |
94 | }) | |
95 | } | |
96 | ||
97 | return next() | |
98 | } | |
99 | ] | |
100 | ||
1bb4c9ab C |
101 | const videoFilesDeleteHLSFileValidator = [ |
102 | isValidVideoIdParam('id'), | |
103 | ||
104 | param('videoFileId') | |
396f6f01 | 105 | .custom(isIdValid), |
1bb4c9ab C |
106 | |
107 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
1bb4c9ab C |
108 | if (areValidationErrors(req, res)) return |
109 | if (!await doesVideoExist(req.params.id, res)) return | |
110 | ||
111 | const video = res.locals.videoAll | |
112 | ||
113 | if (!checkLocalVideo(video, res)) return | |
114 | ||
115 | if (!video.getHLSPlaylist()) { | |
116 | return res.fail({ | |
117 | status: HttpStatusCode.BAD_REQUEST_400, | |
118 | message: 'This video does not have HLS files' | |
119 | }) | |
120 | } | |
121 | ||
122 | const hlsFiles = video.getHLSPlaylist().VideoFiles | |
123 | if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) { | |
124 | return res.fail({ | |
125 | status: HttpStatusCode.NOT_FOUND_404, | |
126 | message: 'This HLS playlist does not have this file id' | |
127 | }) | |
128 | } | |
129 | ||
130 | // Last file to delete | |
131 | if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) { | |
132 | return res.fail({ | |
133 | status: HttpStatusCode.BAD_REQUEST_400, | |
134 | message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files' | |
135 | }) | |
136 | } | |
137 | ||
138 | return next() | |
139 | } | |
140 | ] | |
141 | ||
b46cf4b9 C |
142 | export { |
143 | videoFilesDeleteWebTorrentValidator, | |
1bb4c9ab C |
144 | videoFilesDeleteWebTorrentFileValidator, |
145 | ||
146 | videoFilesDeleteHLSValidator, | |
147 | videoFilesDeleteHLSFileValidator | |
b46cf4b9 C |
148 | } |
149 | ||
150 | // --------------------------------------------------------------------------- | |
151 | ||
152 | function checkLocalVideo (video: MVideo, res: express.Response) { | |
153 | if (video.remote) { | |
154 | res.fail({ | |
155 | status: HttpStatusCode.BAD_REQUEST_400, | |
156 | message: 'Cannot delete files of remote video' | |
157 | }) | |
158 | ||
159 | return false | |
160 | } | |
161 | ||
162 | return true | |
163 | } |