1 import * as express from 'express'
2 import 'express-validator'
3 import { param, body } from 'express-validator/check'
4 import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc'
5 import { isVideoExist } from '../../helpers/custom-validators/videos'
6 import { logger } from '../../helpers/logger'
7 import { areValidationErrors } from './utils'
8 import { VideoModel } from '../../models/video/video'
9 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
10 import { isHostValid } from '../../helpers/custom-validators/servers'
11 import { getServerActor } from '../../helpers/utils'
12 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
13 import { SERVER_ACTOR_NAME } from '../../initializers'
14 import { ServerModel } from '../../models/server/server'
16 const videoFileRedundancyGetValidator = [
17 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
19 .customSanitizer(toIntOrNull)
20 .custom(exists).withMessage('Should have a valid resolution'),
23 .customSanitizer(toIntOrNull)
24 .custom(exists).withMessage('Should have a valid fps'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
29 if (areValidationErrors(req, res)) return
30 if (!await isVideoExist(req.params.videoId, res)) return
32 const video: VideoModel = res.locals.video
33 const videoFile = video.VideoFiles.find(f => {
34 return f.resolution === req.params.resolution && (!req.params.fps || f.fps === req.params.fps)
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 = [
49 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
50 param('streamingPlaylistType').custom(exists).withMessage('Should have a valid streaming playlist type'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
55 if (areValidationErrors(req, res)) return
56 if (!await isVideoExist(req.params.videoId, res)) return
58 const video: VideoModel = res.locals.video
59 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p === req.params.streamingPlaylistType)
61 if (!videoStreamingPlaylist) return res.status(404).json({ error: 'Video playlist not found.' })
62 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
64 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
65 if (!videoRedundancy) return res.status(404).json({ error: 'Video redundancy not found.' })
66 res.locals.videoRedundancy = videoRedundancy
72 const updateServerRedundancyValidator = [
73 param('host').custom(isHostValid).withMessage('Should have a valid host'),
74 body('redundancyAllowed')
76 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
78 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
81 if (areValidationErrors(req, res)) return
83 const server = await ServerModel.loadByHost(req.params.host)
89 error: `Server ${req.params.host} not found.`
94 res.locals.server = server
99 // ---------------------------------------------------------------------------
102 videoFileRedundancyGetValidator,
103 videoPlaylistRedundancyGetValidator,
104 updateServerRedundancyValidator