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'
|
2023-07-11 02:21:13 -05:00
|
|
|
import { updatePlaylistAfterFileChange } from '@server/lib/hls'
|
|
|
|
import { removeAllWebVideoFiles, removeHLSFile, removeHLSPlaylist, removeWebVideoFile } 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,
|
2023-07-11 02:21:13 -05:00
|
|
|
videoFilesDeleteWebVideoFileValidator,
|
|
|
|
videoFilesDeleteWebVideoValidator,
|
2022-01-06 06:27:29 -06:00
|
|
|
videosGetValidator
|
2021-11-17 09:04:53 -06:00
|
|
|
} from '../../../middlewares'
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
filesRouter.delete(
|
|
|
|
[ '/:id/webtorrent', '/:id/web-videos' ], // TODO: remove webtorrent in V7
|
2021-11-17 09:04:53 -06:00
|
|
|
authenticate,
|
2021-11-18 07:35:08 -06:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
2023-07-11 02:21:13 -05:00
|
|
|
asyncMiddleware(videoFilesDeleteWebVideoValidator),
|
|
|
|
asyncMiddleware(removeAllWebVideoFilesController)
|
2022-07-29 07:50:41 -05:00
|
|
|
)
|
2023-07-11 02:21:13 -05:00
|
|
|
filesRouter.delete(
|
|
|
|
[ '/:id/webtorrent/:videoFileId', '/:id/web-videos/:videoFileId' ], // TODO: remove webtorrent in V7
|
2022-07-29 07:50:41 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
2023-07-11 02:21:13 -05:00
|
|
|
asyncMiddleware(videoFilesDeleteWebVideoFileValidator),
|
|
|
|
asyncMiddleware(removeWebVideoFileController)
|
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
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
async function removeAllWebVideoFilesController (req: express.Request, res: express.Response) {
|
2021-11-17 09:04:53 -06:00
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
logger.info('Deleting Web Video files of %s.', video.url, lTags(video.uuid))
|
2021-11-17 09:04:53 -06:00
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
await removeAllWebVideoFiles(video)
|
2022-07-29 07:50:41 -05:00
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
async function removeWebVideoFileController (req: express.Request, res: express.Response) {
|
2022-07-29 07:50:41 -05:00
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
const videoFileId = +req.params.videoFileId
|
2023-07-11 02:21:13 -05:00
|
|
|
logger.info('Deleting Web Video file %d of %s.', videoFileId, video.url, lTags(video.uuid))
|
2021-11-17 09:04:53 -06:00
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
await removeWebVideoFile(video, videoFileId)
|
2021-11-17 09:04:53 -06:00
|
|
|
await federateVideoIfNeeded(video, false, undefined)
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|