1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
4 import { logger } from '../../helpers/logger'
5 import { areValidationErrors } from './utils'
6 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
7 import { isHostValid } from '../../helpers/custom-validators/servers'
8 import { ServerModel } from '../../models/server/server'
9 import { doesVideoExist } from '../../helpers/middlewares'
10 import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
12 const videoFileRedundancyGetValidator = [
13 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
15 .customSanitizer(toIntOrNull)
16 .custom(exists).withMessage('Should have a valid resolution'),
19 .customSanitizer(toIntOrNull)
20 .custom(exists).withMessage('Should have a valid fps'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
25 if (areValidationErrors(req, res)) return
26 if (!await doesVideoExist(req.params.videoId, res)) return
28 const video = res.locals.videoAll
30 const paramResolution = req.params.resolution as unknown as number // We casted to int above
31 const paramFPS = req.params.fps as unknown as number // We casted to int above
33 const videoFile = video.VideoFiles.find(f => {
34 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
37 if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
38 res.locals.videoFile = videoFile
40 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
41 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
42 res.locals.videoRedundancy = videoRedundancy
48 const videoPlaylistRedundancyGetValidator = [
50 .custom(isIdOrUUIDValid)
51 .not().isEmpty().withMessage('Should have a valid video id'),
52 param('streamingPlaylistType')
53 .customSanitizer(toIntOrNull)
54 .custom(exists).withMessage('Should have a valid streaming playlist type'),
56 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
57 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
59 if (areValidationErrors(req, res)) return
60 if (!await doesVideoExist(req.params.videoId, res)) return
62 const video = res.locals.videoAll
64 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
65 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
67 if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
68 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
70 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
71 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
72 res.locals.videoRedundancy = videoRedundancy
78 const updateServerRedundancyValidator = [
79 param('host').custom(isHostValid).withMessage('Should have a valid host'),
80 body('redundancyAllowed')
81 .customSanitizer(toBooleanOrNull)
82 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
87 if (areValidationErrors(req, res)) return
89 const server = await ServerModel.loadByHost(req.params.host)
95 error: `Server ${req.params.host} not found.`
100 res.locals.server = server
105 const listVideoRedundanciesValidator = [
107 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
109 (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
112 if (areValidationErrors(req, res)) return
118 const addVideoRedundancyValidator = [
121 .withMessage('Should have a valid video id'),
123 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
124 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
126 if (areValidationErrors(req, res)) return
128 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
130 if (res.locals.onlyVideo.remote === false) {
131 return res.status(400)
132 .json({ error: 'Cannot create a redundancy on a local video' })
136 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
138 return res.status(409)
139 .json({ error: 'This video is already duplicated by your instance.' })
146 const removeVideoRedundancyValidator = [
147 param('redundancyId')
149 .withMessage('Should have a valid redundancy id'),
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
154 if (areValidationErrors(req, res)) return
156 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
158 return res.status(404)
159 .json({ error: 'Video redundancy not found' })
163 res.locals.videoRedundancy = redundancy
169 // ---------------------------------------------------------------------------
172 videoFileRedundancyGetValidator,
173 videoPlaylistRedundancyGetValidator,
174 updateServerRedundancyValidator,
175 listVideoRedundanciesValidator,
176 addVideoRedundancyValidator,
177 removeVideoRedundancyValidator