aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos/video-files.ts
blob: b44c997e3d6acdb590236c409bdfc742f5cef5e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import express from 'express'
import { MVideo } from '@server/types/models'
import { HttpStatusCode } from '@shared/models'
import { logger } from '../../../helpers/logger'
import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared'
import { isIdValid } from '@server/helpers/custom-validators/misc'
import { param } from 'express-validator'

const videoFilesDeleteWebTorrentValidator = [
  isValidVideoIdParam('id'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videoFilesDeleteWebTorrent parameters', { parameters: req.params })

    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.hasWebTorrentFiles()) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'This video does not have WebTorrent files'
      })
    }

    if (!video.getHLSPlaylist()) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
      })
    }

    return next()
  }
]

const videoFilesDeleteWebTorrentFileValidator = [
  isValidVideoIdParam('id'),

  param('videoFileId')
    .custom(isIdValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videoFilesDeleteWebTorrentFile parameters', { parameters: req.params })

    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,
        message: 'This video does not have this WebTorrent file id'
      })
    }

    if (files.length === 1 && !video.getHLSPlaylist()) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot delete WebTorrent files since this video does not have HLS playlist'
      })
    }

    return next()
  }
]

// ---------------------------------------------------------------------------

const videoFilesDeleteHLSValidator = [
  isValidVideoIdParam('id'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videoFilesDeleteHLS parameters', { parameters: req.params })

    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'
      })
    }

    if (!video.hasWebTorrentFiles()) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot delete HLS playlist since this video does not have WebTorrent files'
      })
    }

    return next()
  }
]

const videoFilesDeleteHLSFileValidator = [
  isValidVideoIdParam('id'),

  param('videoFileId')
    .custom(isIdValid),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videoFilesDeleteHLSFile parameters', { parameters: req.params })

    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
    if (hlsFiles.length === 1 && !video.hasWebTorrentFiles()) {
      return res.fail({
        status: HttpStatusCode.BAD_REQUEST_400,
        message: 'Cannot delete last HLS playlist file since this video does not have WebTorrent files'
      })
    }

    return next()
  }
]

export {
  videoFilesDeleteWebTorrentValidator,
  videoFilesDeleteWebTorrentFileValidator,

  videoFilesDeleteHLSValidator,
  videoFilesDeleteHLSFileValidator
}

// ---------------------------------------------------------------------------

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
}