diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-29 14:50:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-08-01 14:55:10 +0200 |
commit | 1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7 (patch) | |
tree | a6554ee0a3ccc2ae402665b2ecf57bb38fd0ed72 /server/controllers/api | |
parent | 12d84abeca4917d2f1e3f308010bfcd56d37cb7c (diff) | |
download | PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.tar.gz PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.tar.zst PeerTube-1bb4c9ab2e8b3b3022351b33a82a5e527fa5d4d7.zip |
Add ability to delete a specific video file
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/videos/files.ts | 62 |
1 files changed, 49 insertions, 13 deletions
diff --git a/server/controllers/api/videos/files.ts b/server/controllers/api/videos/files.ts index 0fbda280e..6d9c0b843 100644 --- a/server/controllers/api/videos/files.ts +++ b/server/controllers/api/videos/files.ts | |||
@@ -2,6 +2,7 @@ import express from 'express' | |||
2 | import toInt from 'validator/lib/toInt' | 2 | import toInt from 'validator/lib/toInt' |
3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | 3 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
4 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | 4 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' |
5 | import { removeAllWebTorrentFiles, removeHLSFile, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file' | ||
5 | import { VideoFileModel } from '@server/models/video/video-file' | 6 | import { VideoFileModel } from '@server/models/video/video-file' |
6 | import { HttpStatusCode, UserRight } from '@shared/models' | 7 | import { HttpStatusCode, UserRight } from '@shared/models' |
7 | import { | 8 | import { |
@@ -9,10 +10,13 @@ import { | |||
9 | authenticate, | 10 | authenticate, |
10 | ensureUserHasRight, | 11 | ensureUserHasRight, |
11 | videoFileMetadataGetValidator, | 12 | videoFileMetadataGetValidator, |
13 | videoFilesDeleteHLSFileValidator, | ||
12 | videoFilesDeleteHLSValidator, | 14 | videoFilesDeleteHLSValidator, |
15 | videoFilesDeleteWebTorrentFileValidator, | ||
13 | videoFilesDeleteWebTorrentValidator, | 16 | videoFilesDeleteWebTorrentValidator, |
14 | videosGetValidator | 17 | videosGetValidator |
15 | } from '../../../middlewares' | 18 | } from '../../../middlewares' |
19 | import { updatePlaylistAfterFileChange } from '@server/lib/hls' | ||
16 | 20 | ||
17 | const lTags = loggerTagsFactory('api', 'video') | 21 | const lTags = loggerTagsFactory('api', 'video') |
18 | const filesRouter = express.Router() | 22 | const filesRouter = express.Router() |
@@ -27,14 +31,26 @@ filesRouter.delete('/:id/hls', | |||
27 | authenticate, | 31 | authenticate, |
28 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | 32 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), |
29 | asyncMiddleware(videoFilesDeleteHLSValidator), | 33 | asyncMiddleware(videoFilesDeleteHLSValidator), |
30 | asyncMiddleware(removeHLSPlaylist) | 34 | asyncMiddleware(removeHLSPlaylistController) |
35 | ) | ||
36 | filesRouter.delete('/:id/hls/:videoFileId', | ||
37 | authenticate, | ||
38 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
39 | asyncMiddleware(videoFilesDeleteHLSFileValidator), | ||
40 | asyncMiddleware(removeHLSFileController) | ||
31 | ) | 41 | ) |
32 | 42 | ||
33 | filesRouter.delete('/:id/webtorrent', | 43 | filesRouter.delete('/:id/webtorrent', |
34 | authenticate, | 44 | authenticate, |
35 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | 45 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), |
36 | asyncMiddleware(videoFilesDeleteWebTorrentValidator), | 46 | asyncMiddleware(videoFilesDeleteWebTorrentValidator), |
37 | asyncMiddleware(removeWebTorrentFiles) | 47 | asyncMiddleware(removeAllWebTorrentFilesController) |
48 | ) | ||
49 | filesRouter.delete('/:id/webtorrent/:videoFileId', | ||
50 | authenticate, | ||
51 | ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES), | ||
52 | asyncMiddleware(videoFilesDeleteWebTorrentFileValidator), | ||
53 | asyncMiddleware(removeWebTorrentFileController) | ||
38 | ) | 54 | ) |
39 | 55 | ||
40 | // --------------------------------------------------------------------------- | 56 | // --------------------------------------------------------------------------- |
@@ -51,33 +67,53 @@ async function getVideoFileMetadata (req: express.Request, res: express.Response | |||
51 | return res.json(videoFile.metadata) | 67 | return res.json(videoFile.metadata) |
52 | } | 68 | } |
53 | 69 | ||
54 | async function removeHLSPlaylist (req: express.Request, res: express.Response) { | 70 | // --------------------------------------------------------------------------- |
71 | |||
72 | async function removeHLSPlaylistController (req: express.Request, res: express.Response) { | ||
55 | const video = res.locals.videoAll | 73 | const video = res.locals.videoAll |
56 | 74 | ||
57 | logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid)) | 75 | logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid)) |
76 | await removeHLSPlaylist(video) | ||
77 | |||
78 | await federateVideoIfNeeded(video, false, undefined) | ||
79 | |||
80 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
81 | } | ||
82 | |||
83 | async function removeHLSFileController (req: express.Request, res: express.Response) { | ||
84 | const video = res.locals.videoAll | ||
85 | const videoFileId = +req.params.videoFileId | ||
58 | 86 | ||
59 | const hls = video.getHLSPlaylist() | 87 | logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid)) |
60 | await video.removeStreamingPlaylistFiles(hls) | ||
61 | await hls.destroy() | ||
62 | 88 | ||
63 | video.VideoStreamingPlaylists = video.VideoStreamingPlaylists.filter(p => p.id !== hls.id) | 89 | const playlist = await removeHLSFile(video, videoFileId) |
90 | if (playlist) await updatePlaylistAfterFileChange(video, playlist) | ||
64 | 91 | ||
65 | await federateVideoIfNeeded(video, false, undefined) | 92 | await federateVideoIfNeeded(video, false, undefined) |
66 | 93 | ||
67 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | 94 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
68 | } | 95 | } |
69 | 96 | ||
70 | async function removeWebTorrentFiles (req: express.Request, res: express.Response) { | 97 | // --------------------------------------------------------------------------- |
98 | |||
99 | async function removeAllWebTorrentFilesController (req: express.Request, res: express.Response) { | ||
71 | const video = res.locals.videoAll | 100 | const video = res.locals.videoAll |
72 | 101 | ||
73 | logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid)) | 102 | logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid)) |
74 | 103 | ||
75 | for (const file of video.VideoFiles) { | 104 | await removeAllWebTorrentFiles(video) |
76 | await video.removeWebTorrentFileAndTorrent(file) | 105 | await federateVideoIfNeeded(video, false, undefined) |
77 | await file.destroy() | 106 | |
78 | } | 107 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
108 | } | ||
109 | |||
110 | async function removeWebTorrentFileController (req: express.Request, res: express.Response) { | ||
111 | const video = res.locals.videoAll | ||
112 | |||
113 | const videoFileId = +req.params.videoFileId | ||
114 | logger.info('Deleting WebTorrent file %d of %s.', videoFileId, video.url, lTags(video.uuid)) | ||
79 | 115 | ||
80 | video.VideoFiles = [] | 116 | await removeWebTorrentFile(video, videoFileId) |
81 | await federateVideoIfNeeded(video, false, undefined) | 117 | await federateVideoIfNeeded(video, false, undefined) |
82 | 118 | ||
83 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | 119 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |