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'
11 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
13 const videoFileRedundancyGetValidator = [
14 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
16 .customSanitizer(toIntOrNull)
17 .custom(exists).withMessage('Should have a valid resolution'),
20 .customSanitizer(toIntOrNull)
21 .custom(exists).withMessage('Should have a valid fps'),
23 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
24 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
26 if (areValidationErrors(req, res)) return
27 if (!await doesVideoExist(req.params.videoId, res)) return
29 const video = res.locals.videoAll
31 const paramResolution = req.params.resolution as unknown as number // We casted to int above
32 const paramFPS = req.params.fps as unknown as number // We casted to int above
34 const videoFile = video.VideoFiles.find(f => {
35 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
38 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video file not found.' })
39 res.locals.videoFile = videoFile
41 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
42 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' })
43 res.locals.videoRedundancy = videoRedundancy
49 const videoPlaylistRedundancyGetValidator = [
51 .custom(isIdOrUUIDValid)
52 .not().isEmpty().withMessage('Should have a valid video id'),
53 param('streamingPlaylistType')
54 .customSanitizer(toIntOrNull)
55 .custom(exists).withMessage('Should have a valid streaming playlist type'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
60 if (areValidationErrors(req, res)) return
61 if (!await doesVideoExist(req.params.videoId, res)) return
63 const video = res.locals.videoAll
65 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
66 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
68 if (!videoStreamingPlaylist) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video playlist not found.' })
69 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
71 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
72 if (!videoRedundancy) return res.status(HttpStatusCode.NOT_FOUND_404).json({ error: 'Video redundancy not found.' })
73 res.locals.videoRedundancy = videoRedundancy
79 const updateServerRedundancyValidator = [
80 param('host').custom(isHostValid).withMessage('Should have a valid host'),
81 body('redundancyAllowed')
82 .customSanitizer(toBooleanOrNull)
83 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
86 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
88 if (areValidationErrors(req, res)) return
90 const server = await ServerModel.loadByHost(req.params.host)
94 .status(HttpStatusCode.NOT_FOUND_404)
96 error: `Server ${req.params.host} not found.`
101 res.locals.server = server
106 const listVideoRedundanciesValidator = [
108 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
110 (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
113 if (areValidationErrors(req, res)) return
119 const addVideoRedundancyValidator = [
122 .withMessage('Should have a valid video id'),
124 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
125 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
127 if (areValidationErrors(req, res)) return
129 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
131 if (res.locals.onlyVideo.remote === false) {
132 return res.status(HttpStatusCode.BAD_REQUEST_400)
133 .json({ error: 'Cannot create a redundancy on a local video' })
136 if (res.locals.onlyVideo.isLive) {
137 return res.status(HttpStatusCode.BAD_REQUEST_400)
138 .json({ error: 'Cannot create a redundancy of a live video' })
141 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
143 return res.status(HttpStatusCode.CONFLICT_409)
144 .json({ error: 'This video is already duplicated by your instance.' })
151 const removeVideoRedundancyValidator = [
152 param('redundancyId')
154 .withMessage('Should have a valid redundancy id'),
156 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
157 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
159 if (areValidationErrors(req, res)) return
161 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
163 return res.status(HttpStatusCode.NOT_FOUND_404)
164 .json({ error: 'Video redundancy not found' })
168 res.locals.videoRedundancy = redundancy
174 // ---------------------------------------------------------------------------
177 videoFileRedundancyGetValidator,
178 videoPlaylistRedundancyGetValidator,
179 updateServerRedundancyValidator,
180 listVideoRedundanciesValidator,
181 addVideoRedundancyValidator,
182 removeVideoRedundancyValidator