2021-11-17 16:04:53 +01:00
|
|
|
import express from 'express'
|
2021-11-18 14:35:08 +01:00
|
|
|
import { MVideo } from '@server/types/models'
|
2021-12-24 10:14:47 +01:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-11-17 16:04:53 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
|
|
|
|
|
|
|
|
const videoFilesDeleteWebTorrentValidator = [
|
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
if (!checkLocalVideo(video, res)) return
|
|
|
|
|
|
|
|
if (!video.hasWebTorrentFiles()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: 'This video does not have WebTorrent files'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!video.getHLSPlaylist()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const videoFilesDeleteHLSValidator = [
|
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
if (!checkLocalVideo(video, res)) return
|
|
|
|
|
|
|
|
if (!video.getHLSPlaylist()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: 'This video does not have HLS files'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!video.hasWebTorrentFiles()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoFilesDeleteWebTorrentValidator,
|
|
|
|
videoFilesDeleteHLSValidator
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function checkLocalVideo (video: MVideo, res: express.Response) {
|
|
|
|
if (video.remote) {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message: 'Cannot delete files of remote video'
|
|
|
|
})
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|