]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-files.ts
Fix shared imports
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-files.ts
CommitLineData
b46cf4b9 1import express from 'express'
ad5db104 2import { MVideo } from '@server/types/models'
d17c7b4e 3import { HttpStatusCode } from '@shared/models'
b46cf4b9
C
4import { logger } from '../../../helpers/logger'
5import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
6
7const 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
b46cf4b9 17
b46cf4b9
C
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
38const 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
b46cf4b9 48
b46cf4b9
C
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
69export {
70 videoFilesDeleteWebTorrentValidator,
71 videoFilesDeleteHLSValidator
72}
73
74// ---------------------------------------------------------------------------
75
76function 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}