1 import express from 'express'
2 import { param } from 'express-validator'
3 import { isIdValid } from '@server/helpers/custom-validators/misc'
4 import { MVideo } from '@server/types/models'
5 import { HttpStatusCode } from '@shared/models'
6 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
8 const videoFilesDeleteWebTorrentValidator = [
9 isValidVideoIdParam('id'),
11 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 if (areValidationErrors(req, res)) return
13 if (!await doesVideoExist(req.params.id, res)) return
15 const video = res.locals.videoAll
17 if (!checkLocalVideo(video, res)) return
19 if (!video.hasWebTorrentFiles()) {
21 status: HttpStatusCode.BAD_REQUEST_400,
22 message: 'This video does not have WebTorrent files'
26 if (!video.getHLSPlaylist()) {
28 status: HttpStatusCode.BAD_REQUEST_400,
29 message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
37 const videoFilesDeleteWebTorrentFileValidator = [
38 isValidVideoIdParam('id'),
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 if (areValidationErrors(req, res)) return
45 if (!await doesVideoExist(req.params.id, res)) return
47 const video = res.locals.videoAll
49 if (!checkLocalVideo(video, res)) return
51 const files = video.VideoFiles
52 if (!files.find(f => f.id === +req.params.videoFileId)) {
54 status: HttpStatusCode.NOT_FOUND_404,
55 message: 'This video does not have this WebTorrent file id'
59 if (files.length === 1 && !video.getHLSPlaylist()) {
61 status: HttpStatusCode.BAD_REQUEST_400,
62 message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
70 // ---------------------------------------------------------------------------
72 const videoFilesDeleteHLSValidator = [
73 isValidVideoIdParam('id'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 if (areValidationErrors(req, res)) return
77 if (!await doesVideoExist(req.params.id, res)) return
79 const video = res.locals.videoAll
81 if (!checkLocalVideo(video, res)) return
83 if (!video.getHLSPlaylist()) {
85 status: HttpStatusCode.BAD_REQUEST_400,
86 message: 'This video does not have HLS files'
90 if (!video.hasWebTorrentFiles()) {
92 status: HttpStatusCode.BAD_REQUEST_400,
93 message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
101 const videoFilesDeleteHLSFileValidator = [
102 isValidVideoIdParam('id'),
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 if (areValidationErrors(req, res)) return
109 if (!await doesVideoExist(req.params.id, res)) return
111 const video = res.locals.videoAll
113 if (!checkLocalVideo(video, res)) return
115 if (!video.getHLSPlaylist()) {
117 status: HttpStatusCode.BAD_REQUEST_400,
118 message: 'This video does not have HLS files'
122 const hlsFiles = video.getHLSPlaylist().VideoFiles
123 if (!hlsFiles.find(f => f.id === +req.params.videoFileId)) {
125 status: HttpStatusCode.NOT_FOUND_404,
126 message: 'This HLS playlist does not have this file id'
130 // Last file to delete
131 if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) {
133 status: HttpStatusCode.BAD_REQUEST_400,
134 message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files'
143 videoFilesDeleteWebTorrentValidator,
144 videoFilesDeleteWebTorrentFileValidator,
146 videoFilesDeleteHLSValidator,
147 videoFilesDeleteHLSFileValidator
150 // ---------------------------------------------------------------------------
152 function checkLocalVideo (video: MVideo, res: express.Response) {
155 status: HttpStatusCode.BAD_REQUEST_400,
156 message: 'Cannot delete files of remote video'