diff options
author | Chocobozzz <me@florianbigard.com> | 2021-11-17 16:04:53 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-11-18 09:04:30 +0100 |
commit | b46cf4b920984492df598c1b61179acfc7f6f22e (patch) | |
tree | 21fda049c85be48ab3d37b537aafa98e94649ad7 /server/controllers/api/videos/files.ts | |
parent | 3cfa817672657df18260ece5b354efa0f3b6e317 (diff) | |
download | PeerTube-b46cf4b920984492df598c1b61179acfc7f6f22e.tar.gz PeerTube-b46cf4b920984492df598c1b61179acfc7f6f22e.tar.zst PeerTube-b46cf4b920984492df598c1b61179acfc7f6f22e.zip |
Add ability to remove hls/webtorrent files
Diffstat (limited to 'server/controllers/api/videos/files.ts')
-rw-r--r-- | server/controllers/api/videos/files.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/server/controllers/api/videos/files.ts b/server/controllers/api/videos/files.ts new file mode 100644 index 000000000..2fe4b5a3f --- /dev/null +++ b/server/controllers/api/videos/files.ts | |||
@@ -0,0 +1,79 @@ | |||
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 { VideoFileModel } from '@server/models/video/video-file' | ||
6 | import { HttpStatusCode } from '@shared/models' | ||
7 | import { | ||
8 | asyncMiddleware, | ||
9 | authenticate, | ||
10 | videoFileMetadataGetValidator, | ||
11 | videoFilesDeleteHLSValidator, | ||
12 | videoFilesDeleteWebTorrentValidator | ||
13 | } from '../../../middlewares' | ||
14 | |||
15 | const lTags = loggerTagsFactory('api', 'video') | ||
16 | const filesRouter = express.Router() | ||
17 | |||
18 | filesRouter.get('/:id/metadata/:videoFileId', | ||
19 | asyncMiddleware(videoFileMetadataGetValidator), | ||
20 | asyncMiddleware(getVideoFileMetadata) | ||
21 | ) | ||
22 | |||
23 | filesRouter.delete('/:id/hls', | ||
24 | authenticate, | ||
25 | asyncMiddleware(videoFilesDeleteHLSValidator), | ||
26 | asyncMiddleware(removeHLSPlaylist) | ||
27 | ) | ||
28 | |||
29 | filesRouter.delete('/:id/webtorrent', | ||
30 | authenticate, | ||
31 | asyncMiddleware(videoFilesDeleteWebTorrentValidator), | ||
32 | asyncMiddleware(removeWebTorrentFiles) | ||
33 | ) | ||
34 | |||
35 | // --------------------------------------------------------------------------- | ||
36 | |||
37 | export { | ||
38 | filesRouter | ||
39 | } | ||
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | async function getVideoFileMetadata (req: express.Request, res: express.Response) { | ||
44 | const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId)) | ||
45 | |||
46 | return res.json(videoFile.metadata) | ||
47 | } | ||
48 | |||
49 | async function removeHLSPlaylist (req: express.Request, res: express.Response) { | ||
50 | const video = res.locals.videoAll | ||
51 | |||
52 | logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid)) | ||
53 | |||
54 | const hls = video.getHLSPlaylist() | ||
55 | await video.removeStreamingPlaylistFiles(hls) | ||
56 | await hls.destroy() | ||
57 | |||
58 | video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id) | ||
59 | |||
60 | await federateVideoIfNeeded(video, false, undefined) | ||
61 | |||
62 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
63 | } | ||
64 | |||
65 | async function removeWebTorrentFiles (req: express.Request, res: express.Response) { | ||
66 | const video = res.locals.videoAll | ||
67 | |||
68 | logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid)) | ||
69 | |||
70 | for (const file of video.VideoFiles) { | ||
71 | await video.removeWebTorrentFileAndTorrent(file) | ||
72 | await file.destroy() | ||
73 | } | ||
74 | |||
75 | video.VideoFiles = [] | ||
76 | await federateVideoIfNeeded(video, false, undefined) | ||
77 | |||
78 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
79 | } | ||