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 { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
16 import { ServerModel } from '../../models/server/server'
17 import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from './shared'
19 const videoFileRedundancyGetValidator = [
20 isValidVideoIdParam('videoId'),
23 .customSanitizer(toIntOrNull)
27 .customSanitizer(toIntOrNull)
30 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 if (areValidationErrors(req, res)) return
32 if (!await doesVideoExist(req.params.videoId, res)) return
34 const video = res.locals.videoAll
36 const paramResolution = req.params.resolution as unknown as number // We casted to int above
37 const paramFPS = req.params.fps as unknown as number // We casted to int above
39 const videoFile = video.VideoFiles.find(f => {
40 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
45 status: HttpStatusCode.NOT_FOUND_404,
46 message: 'Video file not found.'
49 res.locals.videoFile = videoFile
51 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
52 if (!videoRedundancy) {
54 status: HttpStatusCode.NOT_FOUND_404,
55 message: 'Video redundancy not found.'
58 res.locals.videoRedundancy = videoRedundancy
64 const videoPlaylistRedundancyGetValidator = [
65 isValidVideoIdParam('videoId'),
67 param('streamingPlaylistType')
68 .customSanitizer(toIntOrNull)
71 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72 if (areValidationErrors(req, res)) return
73 if (!await doesVideoExist(req.params.videoId, res)) return
75 const video = res.locals.videoAll
77 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
78 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
80 if (!videoStreamingPlaylist) {
82 status: HttpStatusCode.NOT_FOUND_404,
83 message: 'Video playlist not found.'
86 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
88 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
89 if (!videoRedundancy) {
91 status: HttpStatusCode.NOT_FOUND_404,
92 message: 'Video redundancy not found.'
95 res.locals.videoRedundancy = videoRedundancy
101 const updateServerRedundancyValidator = [
103 .custom(isHostValid),
105 body('redundancyAllowed')
106 .customSanitizer(toBooleanOrNull)
107 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'),
109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 if (areValidationErrors(req, res)) return
112 const server = await ServerModel.loadByHost(req.params.host)
116 status: HttpStatusCode.NOT_FOUND_404,
117 message: `Server ${req.params.host} not found.`
121 res.locals.server = server
126 const listVideoRedundanciesValidator = [
128 .custom(isVideoRedundancyTarget),
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
131 if (areValidationErrors(req, res)) return
137 const addVideoRedundancyValidator = [
139 .customSanitizer(toCompleteUUID)
140 .custom(isIdOrUUIDValid),
142 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
143 if (areValidationErrors(req, res)) return
145 if (!await doesVideoExist(req.body.videoId, res, 'only-video')) return
147 if (res.locals.onlyVideo.remote === false) {
148 return res.fail({ message: 'Cannot create a redundancy on a local video' })
151 if (res.locals.onlyVideo.isLive) {
152 return res.fail({ message: 'Cannot create a redundancy of a live video' })
155 const alreadyExists = await VideoRedundancyModel.isLocalByVideoUUIDExists(res.locals.onlyVideo.uuid)
158 status: HttpStatusCode.CONFLICT_409,
159 message: 'This video is already duplicated by your instance.'
167 const removeVideoRedundancyValidator = [
168 param('redundancyId')
171 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 if (areValidationErrors(req, res)) return
174 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(parseInt(req.params.redundancyId, 10))
177 status: HttpStatusCode.NOT_FOUND_404,
178 message: 'Video redundancy not found'
182 res.locals.videoRedundancy = redundancy
188 // ---------------------------------------------------------------------------
191 videoFileRedundancyGetValidator,
192 videoPlaylistRedundancyGetValidator,
193 updateServerRedundancyValidator,
194 listVideoRedundanciesValidator,
195 addVideoRedundancyValidator,
196 removeVideoRedundancyValidator