1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { exists, isBooleanValid, isIdOrUUIDValid, 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'
11 const videoFileRedundancyGetValidator = [
12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
14 .customSanitizer(toIntOrNull)
15 .custom(exists).withMessage('Should have a valid resolution'),
18 .customSanitizer(toIntOrNull)
19 .custom(exists).withMessage('Should have a valid fps'),
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
24 if (areValidationErrors(req, res)) return
25 if (!await doesVideoExist(req.params.videoId, res)) return
27 const video = res.locals.videoAll
28 const videoFile = video.VideoFiles.find(f => {
29 return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
32 if (!videoFile) return res.status(404).json({ error: 'Video file not found.' })
33 res.locals.videoFile = videoFile
35 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
36 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
37 res.locals.videoRedundancy = videoRedundancy
43 const videoPlaylistRedundancyGetValidator = [
44 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
45 param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'),
47 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
48 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
50 if (areValidationErrors(req, res)) return
51 if (!await doesVideoExist(req.params.videoId, res)) return
53 const video = res.locals.videoAll
54 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType)
56 if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
57 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
59 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
60 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
61 res.locals.videoRedundancy = videoRedundancy
67 const updateServerRedundancyValidator = [
68 param('host').custom(isHostValid).withMessage('Should have a valid host'),
69 body('redundancyAllowed')
70 .customSanitizer(toBooleanOrNull)
71 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
76 if (areValidationErrors(req, res)) return
78 const server = await ServerModel.loadByHost(req.params.host)
84 error: `Server ${req.params.host} not found.`
89 res.locals.server = server
94 // ---------------------------------------------------------------------------
97 videoFileRedundancyGetValidator,
98 videoPlaylistRedundancyGetValidator,
99 updateServerRedundancyValidator