1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies'
4 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5 import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
6 import { isHostValid } from '../../helpers/custom-validators/servers'
7 import { logger } from '../../helpers/logger'
8 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
9 import { ServerModel } from '../../models/server/server'
10 import { areValidationErrors, doesVideoExist } from './shared'
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)
39 status: HttpStatusCode.NOT_FOUND_404,
40 message: 'Video file not found.'
43 res.locals.videoFile = videoFile
45 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
46 if (!videoRedundancy) {
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Video redundancy not found.'
52 res.locals.videoRedundancy = videoRedundancy
58 const videoPlaylistRedundancyGetValidator = [
60 .custom(isIdOrUUIDValid)
61 .not().isEmpty().withMessage('Should have a valid video id'),
62 param('streamingPlaylistType')
63 .customSanitizer(toIntOrNull)
64 .custom(exists).withMessage('Should have a valid streaming playlist type'),
66 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
69 if (areValidationErrors(req, res)) return
70 if (!await doesVideoExist(req.params.videoId, res)) return
72 const video = res.locals.videoAll
74 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
75 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
77 if (!videoStreamingPlaylist) {
79 status: HttpStatusCode.NOT_FOUND_404,
80 message: 'Video playlist not found.'
83 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
85 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
86 if (!videoRedundancy) {
88 status: HttpStatusCode.NOT_FOUND_404,
89 message: 'Video redundancy not found.'
92 res.locals.videoRedundancy = videoRedundancy
98 const updateServerRedundancyValidator = [
99 param('host').custom(isHostValid).withMessage('Should have a valid host'),
100 body('redundancyAllowed')
101 .customSanitizer(toBooleanOrNull)
102 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
107 if (areValidationErrors(req, res)) return
109 const server = await ServerModel.loadByHost(req.params.host)
113 status: HttpStatusCode.NOT_FOUND_404,
114 message: `Server ${req.params.host} not found.`
118 res.locals.server = server
123 const listVideoRedundanciesValidator = [
125 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
127 (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
130 if (areValidationErrors(req, res)) return
136 const addVideoRedundancyValidator = [
139 .withMessage('Should have a valid video id'),
141 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
142 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
144 if (areValidationErrors(req, res)) return
146 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
148 if (res.locals.onlyVideo.remote === false) {
149 return res.fail({ message: 'Cannot create a redundancy on a local video' })
152 if (res.locals.onlyVideo.isLive) {
153 return res.fail({ message: 'Cannot create a redundancy of a live video' })
156 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
159 status: HttpStatusCode.CONFLICT_409,
160 message: 'This video is already duplicated by your instance.'
168 const removeVideoRedundancyValidator = [
169 param('redundancyId')
171 .withMessage('Should have a valid redundancy id'),
173 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
174 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
176 if (areValidationErrors(req, res)) return
178 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
181 status: HttpStatusCode.NOT_FOUND_404,
182 message: 'Video redundancy not found'
186 res.locals.videoRedundancy = redundancy
192 // ---------------------------------------------------------------------------
195 videoFileRedundancyGetValidator,
196 videoPlaylistRedundancyGetValidator,
197 updateServerRedundancyValidator,
198 listVideoRedundanciesValidator,
199 addVideoRedundancyValidator,
200 removeVideoRedundancyValidator