]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/files.ts
6d9c0b843a90c769a01de7367b0ee1b1db2b3357
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / files.ts
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 { removeAllWebTorrentFiles, removeHLSFile, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file'
6 import { VideoFileModel } from '@server/models/video/video-file'
7 import { HttpStatusCode, UserRight } from '@shared/models'
8 import {
9 asyncMiddleware,
10 authenticate,
11 ensureUserHasRight,
12 videoFileMetadataGetValidator,
13 videoFilesDeleteHLSFileValidator,
14 videoFilesDeleteHLSValidator,
15 videoFilesDeleteWebTorrentFileValidator,
16 videoFilesDeleteWebTorrentValidator,
17 videosGetValidator
18 } from '../../../middlewares'
19 import { updatePlaylistAfterFileChange } from '@server/lib/hls'
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('/:id/webtorrent',
44 authenticate,
45 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
46 asyncMiddleware(videoFilesDeleteWebTorrentValidator),
47 asyncMiddleware(removeAllWebTorrentFilesController)
48 )
49 filesRouter.delete('/:id/webtorrent/:videoFileId',
50 authenticate,
51 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
52 asyncMiddleware(videoFilesDeleteWebTorrentFileValidator),
53 asyncMiddleware(removeWebTorrentFileController)
54 )
55
56 // ---------------------------------------------------------------------------
57
58 export {
59 filesRouter
60 }
61
62 // ---------------------------------------------------------------------------
63
64 async function getVideoFileMetadata (req: express.Request, res: express.Response) {
65 const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId))
66
67 return res.json(videoFile.metadata)
68 }
69
70 // ---------------------------------------------------------------------------
71
72 async function removeHLSPlaylistController (req: express.Request, res: express.Response) {
73 const video = res.locals.videoAll
74
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
86
87 logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid))
88
89 const playlist = await removeHLSFile(video, videoFileId)
90 if (playlist) await updatePlaylistAfterFileChange(video, playlist)
91
92 await federateVideoIfNeeded(video, false, undefined)
93
94 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
95 }
96
97 // ---------------------------------------------------------------------------
98
99 async function removeAllWebTorrentFilesController (req: express.Request, res: express.Response) {
100 const video = res.locals.videoAll
101
102 logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid))
103
104 await removeAllWebTorrentFiles(video)
105 await federateVideoIfNeeded(video, false, undefined)
106
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))
115
116 await removeWebTorrentFile(video, videoFileId)
117 await federateVideoIfNeeded(video, false, undefined)
118
119 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
120 }