diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/middlewares/validators/videos/video-files.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/middlewares/validators/videos/video-files.ts')
-rw-r--r-- | server/middlewares/validators/videos/video-files.ts | 163 |
1 files changed, 0 insertions, 163 deletions
diff --git a/server/middlewares/validators/videos/video-files.ts b/server/middlewares/validators/videos/video-files.ts deleted file mode 100644 index 6c0ecda42..000000000 --- a/server/middlewares/validators/videos/video-files.ts +++ /dev/null | |||
@@ -1,163 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { param } from 'express-validator' | ||
3 | import { isIdValid } from '@server/helpers/custom-validators/misc' | ||
4 | import { MVideo } from '@server/types/models' | ||
5 | import { HttpStatusCode } from '@shared/models' | ||
6 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' | ||
7 | |||
8 | const videoFilesDeleteWebVideoValidator = [ | ||
9 | isValidVideoIdParam('id'), | ||
10 | |||
11 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
12 | if (areValidationErrors(req, res)) return | ||
13 | if (!await doesVideoExist(req.params.id, res)) return | ||
14 | |||
15 | const video = res.locals.videoAll | ||
16 | |||
17 | if (!checkLocalVideo(video, res)) return | ||
18 | |||
19 | if (!video.hasWebVideoFiles()) { | ||
20 | return res.fail({ | ||
21 | status: HttpStatusCode.BAD_REQUEST_400, | ||
22 | message: 'This video does not have Web Video files' | ||
23 | }) | ||
24 | } | ||
25 | |||
26 | if (!video.getHLSPlaylist()) { | ||
27 | return res.fail({ | ||
28 | status: HttpStatusCode.BAD_REQUEST_400, | ||
29 | message: 'Cannot delete Web Video files since this video does not have HLS playlist' | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | return next() | ||
34 | } | ||
35 | ] | ||
36 | |||
37 | const videoFilesDeleteWebVideoFileValidator = [ | ||
38 | isValidVideoIdParam('id'), | ||
39 | |||
40 | param('videoFileId') | ||
41 | .custom(isIdValid), | ||
42 | |||
43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
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 Web Video 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 Web Video files since this video does not have HLS playlist' | ||
63 | }) | ||
64 | } | ||
65 | |||
66 | return next() | ||
67 | } | ||
68 | ] | ||
69 | |||
70 | // --------------------------------------------------------------------------- | ||
71 | |||
72 | const videoFilesDeleteHLSValidator = [ | ||
73 | isValidVideoIdParam('id'), | ||
74 | |||
75 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
76 | if (areValidationErrors(req, res)) return | ||
77 | if (!await doesVideoExist(req.params.id, res)) return | ||
78 | |||
79 | const video = res.locals.videoAll | ||
80 | |||
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.hasWebVideoFiles()) { | ||
91 | return res.fail({ | ||
92 | status: HttpStatusCode.BAD_REQUEST_400, | ||
93 | message: 'Cannot delete HLS playlist since this video does not have Web Video files' | ||
94 | }) | ||
95 | } | ||
96 | |||
97 | return next() | ||
98 | } | ||
99 | ] | ||
100 | |||
101 | const videoFilesDeleteHLSFileValidator = [ | ||
102 | isValidVideoIdParam('id'), | ||
103 | |||
104 | param('videoFileId') | ||
105 | .custom(isIdValid), | ||
106 | |||
107 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
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.hasWebVideoFiles()) { | ||
132 | return res.fail({ | ||
133 | status: HttpStatusCode.BAD_REQUEST_400, | ||
134 | message: 'Cannot delete last HLS playlist file since this video does not have Web Video files' | ||
135 | }) | ||
136 | } | ||
137 | |||
138 | return next() | ||
139 | } | ||
140 | ] | ||
141 | |||
142 | export { | ||
143 | videoFilesDeleteWebVideoValidator, | ||
144 | videoFilesDeleteWebVideoFileValidator, | ||
145 | |||
146 | videoFilesDeleteHLSValidator, | ||
147 | videoFilesDeleteHLSFileValidator | ||
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 | } | ||