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'
12 videoFileMetadataGetValidator,
13 videoFilesDeleteHLSFileValidator,
14 videoFilesDeleteHLSValidator,
15 videoFilesDeleteWebTorrentFileValidator,
16 videoFilesDeleteWebTorrentValidator,
18 } from '../../../middlewares'
19 import { updatePlaylistAfterFileChange } from '@server/lib/hls'
21 const lTags = loggerTagsFactory('api', 'video')
22 const filesRouter = express.Router()
24 filesRouter.get('/:id/metadata/:videoFileId',
25 asyncMiddleware(videosGetValidator),
26 asyncMiddleware(videoFileMetadataGetValidator),
27 asyncMiddleware(getVideoFileMetadata)
30 filesRouter.delete('/:id/hls',
32 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
33 asyncMiddleware(videoFilesDeleteHLSValidator),
34 asyncMiddleware(removeHLSPlaylistController)
36 filesRouter.delete('/:id/hls/:videoFileId',
38 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
39 asyncMiddleware(videoFilesDeleteHLSFileValidator),
40 asyncMiddleware(removeHLSFileController)
43 filesRouter.delete('/:id/webtorrent',
45 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
46 asyncMiddleware(videoFilesDeleteWebTorrentValidator),
47 asyncMiddleware(removeAllWebTorrentFilesController)
49 filesRouter.delete('/:id/webtorrent/:videoFileId',
51 ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
52 asyncMiddleware(videoFilesDeleteWebTorrentFileValidator),
53 asyncMiddleware(removeWebTorrentFileController)
56 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 async function getVideoFileMetadata (req: express.Request, res: express.Response) {
65 const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId))
67 return res.json(videoFile.metadata)
70 // ---------------------------------------------------------------------------
72 async function removeHLSPlaylistController (req: express.Request, res: express.Response) {
73 const video = res.locals.videoAll
75 logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid))
76 await removeHLSPlaylist(video)
78 await federateVideoIfNeeded(video, false, undefined)
80 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
83 async function removeHLSFileController (req: express.Request, res: express.Response) {
84 const video = res.locals.videoAll
85 const videoFileId = +req.params.videoFileId
87 logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid))
89 const playlist = await removeHLSFile(video, videoFileId)
90 if (playlist) await updatePlaylistAfterFileChange(video, playlist)
92 await federateVideoIfNeeded(video, false, undefined)
94 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
97 // ---------------------------------------------------------------------------
99 async function removeAllWebTorrentFilesController (req: express.Request, res: express.Response) {
100 const video = res.locals.videoAll
102 logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid))
104 await removeAllWebTorrentFiles(video)
105 await federateVideoIfNeeded(video, false, undefined)
107 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
110 async function removeWebTorrentFileController (req: express.Request, res: express.Response) {
111 const video = res.locals.videoAll
113 const videoFileId = +req.params.videoFileId
114 logger.info('Deleting WebTorrent file %d of %s.', videoFileId, video.url, lTags(video.uuid))
116 await removeWebTorrentFile(video, videoFileId)
117 await federateVideoIfNeeded(video, false, undefined)
119 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)