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/controllers/api/videos/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/controllers/api/videos/files.ts')
-rw-r--r-- | server/controllers/api/videos/files.ts | 122 |
1 files changed, 0 insertions, 122 deletions
diff --git a/server/controllers/api/videos/files.ts b/server/controllers/api/videos/files.ts deleted file mode 100644 index 67b60ff63..000000000 --- a/server/controllers/api/videos/files.ts +++ /dev/null | |||
@@ -1,122 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import toInt from 'validator/lib/toInt' | ||
3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | ||
4 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | ||
5 | import { updatePlaylistAfterFileChange } from '@server/lib/hls' | ||
6 | import { removeAllWebVideoFiles, removeHLSFile, removeHLSPlaylist, removeWebVideoFile } from '@server/lib/video-file' | ||
7 | import { VideoFileModel } from '@server/models/video/video-file' | ||
8 | import { HttpStatusCode, UserRight } from '@shared/models' | ||
9 | import { | ||
10 | asyncMiddleware, | ||
11 | authenticate, | ||
12 | ensureUserHasRight, | ||
13 | videoFileMetadataGetValidator, | ||
14 | videoFilesDeleteHLSFileValidator, | ||
15 | videoFilesDeleteHLSValidator, | ||
16 | videoFilesDeleteWebVideoFileValidator, | ||
17 | videoFilesDeleteWebVideoValidator, | ||
18 | videosGetValidator | ||
19 | } from '../../../middlewares' | ||
20 | |||
21 | const lTags = loggerTagsFactory('api', 'video') | ||
22 | const filesRouter = express.Router() | ||
23 | |||
24 | filesRouter.get('/:id/metadata/:videoFileId', | ||
25 | asyncMiddleware(videosGetValidator), | ||
26 | asyncMiddleware(videoFileMetadataGetValidator), | ||
27 | asyncMiddleware(getVideoFileMetadata) | ||
28 | ) | ||
29 | |||
30 | filesRouter.delete('/:id/hls', | ||
31 | authenticate, | ||
32 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
33 | asyncMiddleware(videoFilesDeleteHLSValidator), | ||
34 | asyncMiddleware(removeHLSPlaylistController) | ||
35 | ) | ||
36 | filesRouter.delete('/:id/hls/:videoFileId', | ||
37 | authenticate, | ||
38 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
39 | asyncMiddleware(videoFilesDeleteHLSFileValidator), | ||
40 | asyncMiddleware(removeHLSFileController) | ||
41 | ) | ||
42 | |||
43 | filesRouter.delete( | ||
44 | [ '/:id/webtorrent', '/:id/web-videos' ], // TODO: remove webtorrent in V7 | ||
45 | authenticate, | ||
46 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
47 | asyncMiddleware(videoFilesDeleteWebVideoValidator), | ||
48 | asyncMiddleware(removeAllWebVideoFilesController) | ||
49 | ) | ||
50 | filesRouter.delete( | ||
51 | [ '/:id/webtorrent/:videoFileId', '/:id/web-videos/:videoFileId' ], // TODO: remove webtorrent in V7 | ||
52 | authenticate, | ||
53 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
54 | asyncMiddleware(videoFilesDeleteWebVideoFileValidator), | ||
55 | asyncMiddleware(removeWebVideoFileController) | ||
56 | ) | ||
57 | |||
58 | // --------------------------------------------------------------------------- | ||
59 | |||
60 | export { | ||
61 | filesRouter | ||
62 | } | ||
63 | |||
64 | // --------------------------------------------------------------------------- | ||
65 | |||
66 | async function getVideoFileMetadata (req: express.Request, res: express.Response) { | ||
67 | const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId)) | ||
68 | |||
69 | return res.json(videoFile.metadata) | ||
70 | } | ||
71 | |||
72 | // --------------------------------------------------------------------------- | ||
73 | |||
74 | async function removeHLSPlaylistController (req: express.Request, res: express.Response) { | ||
75 | const video = res.locals.videoAll | ||
76 | |||
77 | logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid)) | ||
78 | await removeHLSPlaylist(video) | ||
79 | |||
80 | await federateVideoIfNeeded(video, false, undefined) | ||
81 | |||
82 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
83 | } | ||
84 | |||
85 | async function removeHLSFileController (req: express.Request, res: express.Response) { | ||
86 | const video = res.locals.videoAll | ||
87 | const videoFileId = +req.params.videoFileId | ||
88 | |||
89 | logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid)) | ||
90 | |||
91 | const playlist = await removeHLSFile(video, videoFileId) | ||
92 | if (playlist) await updatePlaylistAfterFileChange(video, playlist) | ||
93 | |||
94 | await federateVideoIfNeeded(video, false, undefined) | ||
95 | |||
96 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
97 | } | ||
98 | |||
99 | // --------------------------------------------------------------------------- | ||
100 | |||
101 | async function removeAllWebVideoFilesController (req: express.Request, res: express.Response) { | ||
102 | const video = res.locals.videoAll | ||
103 | |||
104 | logger.info('Deleting Web Video files of %s.', video.url, lTags(video.uuid)) | ||
105 | |||
106 | await removeAllWebVideoFiles(video) | ||
107 | await federateVideoIfNeeded(video, false, undefined) | ||
108 | |||
109 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
110 | } | ||
111 | |||
112 | async function removeWebVideoFileController (req: express.Request, res: express.Response) { | ||
113 | const video = res.locals.videoAll | ||
114 | |||
115 | const videoFileId = +req.params.videoFileId | ||
116 | logger.info('Deleting Web Video file %d of %s.', videoFileId, video.url, lTags(video.uuid)) | ||
117 | |||
118 | await removeWebVideoFile(video, videoFileId) | ||
119 | await federateVideoIfNeeded(video, false, undefined) | ||
120 | |||
121 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
122 | } | ||