2021-11-17 09:04:53 -06:00
|
|
|
import express from 'express'
|
2022-08-17 07:58:40 -05:00
|
|
|
import { param } from 'express-validator'
|
|
|
|
import { isIdValid } from '@server/helpers/custom-validators/misc'
|
2021-11-18 07:35:08 -06:00
|
|
|
import { MVideo } from '@server/types/models'
|
2021-12-24 03:14:47 -06:00
|
|
|
import { HttpStatusCode } from '@shared/models'
|
2021-11-17 09:04:53 -06:00
|
|
|
import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
const videoFilesDeleteWebVideoValidator = [
|
2021-11-17 09:04:53 -06:00
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
if (!checkLocalVideo(video, res)) return
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
if (!video.hasWebVideoFiles()) {
|
2021-11-17 09:04:53 -06:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'This video does not have Web Video files'
|
2021-11-17 09:04:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!video.getHLSPlaylist()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'Cannot delete Web Video files since this video does not have HLS playlist'
|
2021-11-17 09:04:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
const videoFilesDeleteWebVideoFileValidator = [
|
2022-07-29 07:50:41 -05:00
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
param('videoFileId')
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await doesVideoExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
if (!checkLocalVideo(video, res)) return
|
|
|
|
|
|
|
|
const files = video.VideoFiles
|
|
|
|
if (!files.find(f => f.id === +req.params.videoFileId)) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'This video does not have this Web Video file id'
|
2022-07-29 07:50:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length === 1 && !video.getHLSPlaylist()) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'Cannot delete Web Video files since this video does not have HLS playlist'
|
2022-07-29 07:50:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-11-17 09:04:53 -06:00
|
|
|
const videoFilesDeleteHLSValidator = [
|
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-11 02:21:13 -05:00
|
|
|
if (!video.hasWebVideoFiles()) {
|
2021-11-17 09:04:53 -06:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'Cannot delete HLS playlist since this video does not have Web Video files'
|
2021-11-17 09:04:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
const videoFilesDeleteHLSFileValidator = [
|
|
|
|
isValidVideoIdParam('id'),
|
|
|
|
|
|
|
|
param('videoFileId')
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const hlsFiles = video.getHLSPlaylist().VideoFiles
|
|
|
|
if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) {
|
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'This HLS playlist does not have this file id'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last file to delete
|
2023-07-11 02:21:13 -05:00
|
|
|
if (hlsFiles.length === 1 && !video.hasWebVideoFiles()) {
|
2022-07-29 07:50:41 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
2023-07-11 02:21:13 -05:00
|
|
|
message: 'Cannot delete last HLS playlist file since this video does not have Web Video files'
|
2022-07-29 07:50:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2021-11-17 09:04:53 -06:00
|
|
|
export {
|
2023-07-11 02:21:13 -05:00
|
|
|
videoFilesDeleteWebVideoValidator,
|
|
|
|
videoFilesDeleteWebVideoFileValidator,
|
2022-07-29 07:50:41 -05:00
|
|
|
|
|
|
|
videoFilesDeleteHLSValidator,
|
|
|
|
videoFilesDeleteHLSFileValidator
|
2021-11-17 09:04:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|