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)
40 status: HttpStatusCode.NOT_FOUND_404,
41 message: 'Video file not found.'
44 res.locals.videoFile = videoFile
46 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
47 if (!videoRedundancy) {
49 status: HttpStatusCode.NOT_FOUND_404,
50 message: 'Video redundancy not found.'
53 res.locals.videoRedundancy = videoRedundancy
59 const videoPlaylistRedundancyGetValidator = [
61 .custom(isIdOrUUIDValid)
62 .not().isEmpty().withMessage('Should have a valid video id'),
63 param('streamingPlaylistType')
64 .customSanitizer(toIntOrNull)
65 .custom(exists).withMessage('Should have a valid streaming playlist type'),
67 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
68 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
70 if (areValidationErrors(req, res)) return
71 if (!await doesVideoExist(req.params.videoId, res)) return
73 const video = res.locals.videoAll
75 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
76 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
78 if (!videoStreamingPlaylist) {
80 status: HttpStatusCode.NOT_FOUND_404,
81 message: 'Video playlist not found.'
84 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
86 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
87 if (!videoRedundancy) {
89 status: HttpStatusCode.NOT_FOUND_404,
90 message: 'Video redundancy not found.'
93 res.locals.videoRedundancy = videoRedundancy
99 const updateServerRedundancyValidator = [
100 param('host').custom(isHostValid).withMessage('Should have a valid host'),
101 body('redundancyAllowed')
102 .customSanitizer(toBooleanOrNull)
103 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
105 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
106 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
108 if (areValidationErrors(req, res)) return
110 const server = await ServerModel.loadByHost(req.params.host)
114 status: HttpStatusCode.NOT_FOUND_404,
115 message: `Server ${req.params.host} not found.`
119 res.locals.server = server
124 const listVideoRedundanciesValidator = [
126 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
128 (req: express.Request, res: express.Response, next: express.NextFunction) => {
129 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
131 if (areValidationErrors(req, res)) return
137 const addVideoRedundancyValidator = [
140 .withMessage('Should have a valid video id'),
142 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
143 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
145 if (areValidationErrors(req, res)) return
147 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
149 if (res.locals.onlyVideo.remote === false) {
150 return res.fail({ message: 'Cannot create a redundancy on a local video' })
153 if (res.locals.onlyVideo.isLive) {
154 return res.fail({ message: 'Cannot create a redundancy of a live video' })
157 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
160 status: HttpStatusCode.CONFLICT_409,
161 message: 'This video is already duplicated by your instance.'
169 const removeVideoRedundancyValidator = [
170 param('redundancyId')
172 .withMessage('Should have a valid redundancy id'),
174 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
175 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
177 if (areValidationErrors(req, res)) return
179 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
182 status: HttpStatusCode.NOT_FOUND_404,
183 message: 'Video redundancy not found'
187 res.locals.videoRedundancy = redundancy
193 // ---------------------------------------------------------------------------
196 videoFileRedundancyGetValidator,
197 videoPlaylistRedundancyGetValidator,
198 updateServerRedundancyValidator,
199 listVideoRedundanciesValidator,
200 addVideoRedundancyValidator,
201 removeVideoRedundancyValidator