1 import 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/models/http/http-error-codes'
13 } from '../../helpers/custom-validators/misc'
14 import { isHostValid } from '../../helpers/custom-validators/servers'
15 import { logger } from '../../helpers/logger'
16 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
17 import { ServerModel } from '../../models/server/server'
18 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared'
20 const videoFileRedundancyGetValidator = [
21 isValidVideoIdParam('videoId'),
24 .customSanitizer(toIntOrNull)
25 .custom(exists).withMessage('Should have a valid resolution'),
28 .customSanitizer(toIntOrNull)
29 .custom(exists).withMessage('Should have a valid fps'),
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 logger.debug('Checking videoFileRedundancyGetValidator parameters', { parameters: req.params })
34 if (areValidationErrors(req, res)) return
35 if (!await doesVideoExist(req.params.videoId, res)) return
37 const video = res.locals.videoAll
39 const paramResolution = req.params.resolution as unknown as number // We casted to int above
40 const paramFPS = req.params.fps as unknown as number // We casted to int above
42 const videoFile = video.VideoFiles.find(f => {
43 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Video file not found.'
52 res.locals.videoFile = videoFile
54 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
55 if (!videoRedundancy) {
57 status: HttpStatusCode.NOT_FOUND_404,
58 message: 'Video redundancy not found.'
61 res.locals.videoRedundancy = videoRedundancy
67 const videoPlaylistRedundancyGetValidator = [
68 isValidVideoIdParam('videoId'),
70 param('streamingPlaylistType')
71 .customSanitizer(toIntOrNull)
72 .custom(exists).withMessage('Should have a valid streaming playlist type'),
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoPlaylistRedundancyGetValidator parameters', { parameters: req.params })
77 if (areValidationErrors(req, res)) return
78 if (!await doesVideoExist(req.params.videoId, res)) return
80 const video = res.locals.videoAll
82 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
83 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
85 if (!videoStreamingPlaylist) {
87 status: HttpStatusCode.NOT_FOUND_404,
88 message: 'Video playlist not found.'
91 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
93 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
94 if (!videoRedundancy) {
96 status: HttpStatusCode.NOT_FOUND_404,
97 message: 'Video redundancy not found.'
100 res.locals.videoRedundancy = videoRedundancy
106 const updateServerRedundancyValidator = [
107 param('host').custom(isHostValid).withMessage('Should have a valid host'),
108 body('redundancyAllowed')
109 .customSanitizer(toBooleanOrNull)
110 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'),
112 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking updateServerRedundancy parameters', { parameters: req.params })
115 if (areValidationErrors(req, res)) return
117 const server = await ServerModel.loadByHost(req.params.host)
121 status: HttpStatusCode.NOT_FOUND_404,
122 message: `Server ${req.params.host} not found.`
126 res.locals.server = server
131 const listVideoRedundanciesValidator = [
133 .custom(isVideoRedundancyTarget).withMessage('Should have a valid video redundancies target'),
135 (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 logger.debug('Checking listVideoRedundanciesValidator parameters', { parameters: req.query })
138 if (areValidationErrors(req, res)) return
144 const addVideoRedundancyValidator = [
146 .customSanitizer(toCompleteUUID)
147 .custom(isIdOrUUIDValid)
148 .withMessage('Should have a valid video id'),
150 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
151 logger.debug('Checking addVideoRedundancyValidator parameters', { parameters: req.query })
153 if (areValidationErrors(req, res)) return
155 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
157 if (res.locals.onlyVideo.remote === false) {
158 return res.fail({ message: 'Cannot create a redundancy on a local video' })
161 if (res.locals.onlyVideo.isLive) {
162 return res.fail({ message: 'Cannot create a redundancy of a live video' })
165 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
168 status: HttpStatusCode.CONFLICT_409,
169 message: 'This video is already duplicated by your instance.'
177 const removeVideoRedundancyValidator = [
178 param('redundancyId')
180 .withMessage('Should have a valid redundancy id'),
182 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
183 logger.debug('Checking removeVideoRedundancyValidator parameters', { parameters: req.query })
185 if (areValidationErrors(req, res)) return
187 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
190 status: HttpStatusCode.NOT_FOUND_404,
191 message: 'Video redundancy not found'
195 res.locals.videoRedundancy = redundancy
201 // ---------------------------------------------------------------------------
204 videoFileRedundancyGetValidator,
205 videoPlaylistRedundancyGetValidator,
206 updateServerRedundancyValidator,
207 listVideoRedundanciesValidator,
208 addVideoRedundancyValidator,
209 removeVideoRedundancyValidator