]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-files.ts
35b0ac757254def1112fc59ce65e9ac08d8b366c
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-files.ts
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
7 const videoFilesDeleteWebTorrentValidator = [
8 isValidVideoIdParam('id'),
9
10 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
11 logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })
12
13 if (areValidationErrors(req, res)) return
14 if (!await doesVideoExist(req.params.id, res)) return
15
16 const video = res.locals.videoAll
17
18 if (!checkLocalVideo(video, res)) return
19
20 if (!video.hasWebTorrentFiles()) {
21 return res.fail({
22 status: HttpStatusCode.BAD_REQUEST_400,
23 message: 'This video does not have WebTorrent files'
24 })
25 }
26
27 if (!video.getHLSPlaylist()) {
28 return res.fail({
29 status: HttpStatusCode.BAD_REQUEST_400,
30 message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
31 })
32 }
33
34 return next()
35 }
36 ]
37
38 const videoFilesDeleteHLSValidator = [
39 isValidVideoIdParam('id'),
40
41 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
42 logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })
43
44 if (areValidationErrors(req, res)) return
45 if (!await doesVideoExist(req.params.id, res)) return
46
47 const video = res.locals.videoAll
48
49 if (!checkLocalVideo(video, res)) return
50
51 if (!video.getHLSPlaylist()) {
52 return res.fail({
53 status: HttpStatusCode.BAD_REQUEST_400,
54 message: 'This video does not have HLS files'
55 })
56 }
57
58 if (!video.hasWebTorrentFiles()) {
59 return res.fail({
60 status: HttpStatusCode.BAD_REQUEST_400,
61 message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
62 })
63 }
64
65 return next()
66 }
67 ]
68
69 export {
70 videoFilesDeleteWebTorrentValidator,
71 videoFilesDeleteHLSValidator
72 }
73
74 // ---------------------------------------------------------------------------
75
76 function checkLocalVideo (video: MVideo, res: express.Response) {
77 if (video.remote) {
78 res.fail({
79 status: HttpStatusCode.BAD_REQUEST_400,
80 message: 'Cannot delete files of remote video'
81 })
82
83 return false
84 }
85
86 return true
87 }