2021-11-17 09:04:53 -06:00
|
|
|
import express from 'express'
|
|
|
|
import toInt from 'validator/lib/toInt'
|
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
|
|
|
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
|
2022-07-29 07:50:41 -05:00
|
|
|
import { removeAllWebTorrentFiles, removeHLSFile, removeHLSPlaylist, removeWebTorrentFile } from '@server/lib/video-file'
|
2021-11-17 09:04:53 -06:00
|
|
|
import { VideoFileModel } from '@server/models/video/video-file'
|
2021-11-18 07:35:08 -06:00
|
|
|
import { HttpStatusCode, UserRight } from '@shared/models'
|
2021-11-17 09:04:53 -06:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
2021-11-18 07:35:08 -06:00
|
|
|
ensureUserHasRight,
|
2021-11-17 09:04:53 -06:00
|
|
|
videoFileMetadataGetValidator,
|
2022-07-29 07:50:41 -05:00
|
|
|
videoFilesDeleteHLSFileValidator,
|
2021-11-17 09:04:53 -06:00
|
|
|
videoFilesDeleteHLSValidator,
|
2022-07-29 07:50:41 -05:00
|
|
|
videoFilesDeleteWebTorrentFileValidator,
|
2022-01-06 06:27:29 -06:00
|
|
|
videoFilesDeleteWebTorrentValidator,
|
|
|
|
videosGetValidator
|
2021-11-17 09:04:53 -06:00
|
|
|
} from '../../../middlewares'
|
2022-07-29 07:50:41 -05:00
|
|
|
import { updatePlaylistAfterFileChange } from '@server/lib/hls'
|
2021-11-17 09:04:53 -06:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('api', 'video')
|
|
|
|
const filesRouter = express.Router()
|
|
|
|
|
|
|
|
filesRouter.get('/:id/metadata/:videoFileId',
|
2022-01-06 06:27:29 -06:00
|
|
|
asyncMiddleware(videosGetValidator),
|
2021-11-17 09:04:53 -06:00
|
|
|
asyncMiddleware(videoFileMetadataGetValidator),
|
|
|
|
asyncMiddleware(getVideoFileMetadata)
|
|
|
|
)
|
|
|
|
|
|
|
|
filesRouter.delete('/:id/hls',
|
|
|
|
authenticate,
|
2021-11-18 07:35:08 -06:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
2021-11-17 09:04:53 -06:00
|
|
|
asyncMiddleware(videoFilesDeleteHLSValidator),
|
2022-07-29 07:50:41 -05:00
|
|
|
asyncMiddleware(removeHLSPlaylistController)
|
|
|
|
)
|
|
|
|
filesRouter.delete('/:id/hls/:videoFileId',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
|
|
|
asyncMiddleware(videoFilesDeleteHLSFileValidator),
|
|
|
|
asyncMiddleware(removeHLSFileController)
|
2021-11-17 09:04:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
filesRouter.delete('/:id/webtorrent',
|
|
|
|
authenticate,
|
2021-11-18 07:35:08 -06:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
2021-11-17 09:04:53 -06:00
|
|
|
asyncMiddleware(videoFilesDeleteWebTorrentValidator),
|
2022-07-29 07:50:41 -05:00
|
|
|
asyncMiddleware(removeAllWebTorrentFilesController)
|
|
|
|
)
|
|
|
|
filesRouter.delete('/:id/webtorrent/:videoFileId',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
|
|
|
asyncMiddleware(videoFilesDeleteWebTorrentFileValidator),
|
|
|
|
asyncMiddleware(removeWebTorrentFileController)
|
2021-11-17 09:04:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
filesRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function getVideoFileMetadata (req: express.Request, res: express.Response) {
|
|
|
|
const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId))
|
|
|
|
|
|
|
|
return res.json(videoFile.metadata)
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function removeHLSPlaylistController (req: express.Request, res: express.Response) {
|
2021-11-17 09:04:53 -06:00
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
logger.info('Deleting HLS playlist of %s.', video.url, lTags(video.uuid))
|
2022-07-29 07:50:41 -05:00
|
|
|
await removeHLSPlaylist(video)
|
|
|
|
|
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeHLSFileController (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
const videoFileId = +req.params.videoFileId
|
2021-11-17 09:04:53 -06:00
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
logger.info('Deleting HLS file %d of %s.', videoFileId, video.url, lTags(video.uuid))
|
2021-11-17 09:04:53 -06:00
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
const playlist = await removeHLSFile(video, videoFileId)
|
|
|
|
if (playlist) await updatePlaylistAfterFileChange(video, playlist)
|
2021-11-17 09:04:53 -06:00
|
|
|
|
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function removeAllWebTorrentFilesController (req: express.Request, res: express.Response) {
|
2021-11-17 09:04:53 -06:00
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
logger.info('Deleting WebTorrent files of %s.', video.url, lTags(video.uuid))
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
await removeAllWebTorrentFiles(video)
|
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeWebTorrentFileController (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
const videoFileId = +req.params.videoFileId
|
|
|
|
logger.info('Deleting WebTorrent file %d of %s.', videoFileId, video.url, lTags(video.uuid))
|
2021-11-17 09:04:53 -06:00
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
await removeWebTorrentFile(video, videoFileId)
|
2021-11-17 09:04:53 -06:00
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|