1 import express from 'express'
2 import { MVideo } from '@server/types/models'
3 import { HttpStatusCode } from '@shared/models'
4 import { logger } from '../../../helpers/logger'
5 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
6 import { isIdValid } from '@server/helpers/custom-validators/misc'
7 import { param } from 'express-validator'
9 const videoFilesDeleteWebTorrentValidator = [
10 isValidVideoIdParam('id'),
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })
15 if (areValidationErrors(req, res)) return
16 if (!await doesVideoExist(req.params.id, res)) return
18 const video = res.locals.videoAll
20 if (!checkLocalVideo(video, res)) return
22 if (!video.hasWebTorrentFiles()) {
24 status: HttpStatusCode.BAD_REQUEST_400,
25 message: 'This video does not have WebTorrent files'
29 if (!video.getHLSPlaylist()) {
31 status: HttpStatusCode.BAD_REQUEST_400,
32 message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
40 const videoFilesDeleteWebTorrentFileValidator = [
41 isValidVideoIdParam('id'),
46 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking videoFilesDeleteWebTorrentFile parameters', { parameters: req.params })
49 if (areValidationErrors(req, res)) return
50 if (!await doesVideoExist(req.params.id, res)) return
52 const video = res.locals.videoAll
54 if (!checkLocalVideo(video, res)) return
56 const files = video.VideoFiles
57 if (!files.find(f => f.id === +req.params.videoFileId)) {
59 status: HttpStatusCode.NOT_FOUND_404,
60 message: 'This video does not have this WebTorrent file id'
64 if (files.length === 1 && !video.getHLSPlaylist()) {
66 status: HttpStatusCode.BAD_REQUEST_400,
67 message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
75 // ---------------------------------------------------------------------------
77 const videoFilesDeleteHLSValidator = [
78 isValidVideoIdParam('id'),
80 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })
83 if (areValidationErrors(req, res)) return
84 if (!await doesVideoExist(req.params.id, res)) return
86 const video = res.locals.videoAll
88 if (!checkLocalVideo(video, res)) return
90 if (!video.getHLSPlaylist()) {
92 status: HttpStatusCode.BAD_REQUEST_400,
93 message: 'This video does not have HLS files'
97 if (!video.hasWebTorrentFiles()) {
99 status: HttpStatusCode.BAD_REQUEST_400,
100 message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
108 const videoFilesDeleteHLSFileValidator = [
109 isValidVideoIdParam('id'),
114 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking videoFilesDeleteHLSFile parameters', { parameters: req.params })
117 if (areValidationErrors(req, res)) return
118 if (!await doesVideoExist(req.params.id, res)) return
120 const video = res.locals.videoAll
122 if (!checkLocalVideo(video, res)) return
124 if (!video.getHLSPlaylist()) {
126 status: HttpStatusCode.BAD_REQUEST_400,
127 message: 'This video does not have HLS files'
131 const hlsFiles = video.getHLSPlaylist().VideoFiles
132 if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) {
134 status: HttpStatusCode.NOT_FOUND_404,
135 message: 'This HLS playlist does not have this file id'
139 // Last file to delete
140 if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) {
142 status: HttpStatusCode.BAD_REQUEST_400,
143 message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files'
152 videoFilesDeleteWebTorrentValidator,
153 videoFilesDeleteWebTorrentFileValidator,
155 videoFilesDeleteHLSValidator,
156 videoFilesDeleteHLSFileValidator
159 // ---------------------------------------------------------------------------
161 function checkLocalVideo (video: MVideo, res: express.Response) {
164 status: HttpStatusCode.BAD_REQUEST_400,
165 message: 'Cannot delete files of remote video'