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 { forceNumber } from '@shared/core-utils'
5 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
14 } from '../../helpers/custom-validators/misc'
15 import { isHostValid } from '../../helpers/custom-validators/servers'
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)
28 .customSanitizer(toIntOrNull)
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 if (areValidationErrors(req, res)) return
33 if (!await doesVideoExist(req.params.videoId, res)) return
35 const video = res.locals.videoAll
37 const paramResolution = req.params.resolution as unknown as number // We casted to int above
38 const paramFPS = req.params.fps as unknown as number // We casted to int above
40 const videoFile = video.VideoFiles.find(f => {
41 return f.resolution === paramResolution && (!req.params.fps || paramFPS)
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video file not found.'
50 res.locals.videoFile = videoFile
52 const videoRedundancy = await VideoRedundancyModel.loadLocalByFileId(videoFile.id)
53 if (!videoRedundancy) {
55 status: HttpStatusCode.NOT_FOUND_404,
56 message: 'Video redundancy not found.'
59 res.locals.videoRedundancy = videoRedundancy
65 const videoPlaylistRedundancyGetValidator = [
66 isValidVideoIdParam('videoId'),
68 param('streamingPlaylistType')
69 .customSanitizer(toIntOrNull)
72 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
73 if (areValidationErrors(req, res)) return
74 if (!await doesVideoExist(req.params.videoId, res)) return
76 const video = res.locals.videoAll
78 const paramPlaylistType = req.params.streamingPlaylistType as unknown as number // We casted to int above
79 const videoStreamingPlaylist = video.VideoStreamingPlaylists.find(p => p.type === paramPlaylistType)
81 if (!videoStreamingPlaylist) {
83 status: HttpStatusCode.NOT_FOUND_404,
84 message: 'Video playlist not found.'
87 res.locals.videoStreamingPlaylist = videoStreamingPlaylist
89 const videoRedundancy = await VideoRedundancyModel.loadLocalByStreamingPlaylistId(videoStreamingPlaylist.id)
90 if (!videoRedundancy) {
92 status: HttpStatusCode.NOT_FOUND_404,
93 message: 'Video redundancy not found.'
96 res.locals.videoRedundancy = videoRedundancy
102 const updateServerRedundancyValidator = [
104 .custom(isHostValid),
106 body('redundancyAllowed')
107 .customSanitizer(toBooleanOrNull)
108 .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed boolean'),
110 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
111 if (areValidationErrors(req, res)) return
113 const server = await ServerModel.loadByHost(req.params.host)
117 status: HttpStatusCode.NOT_FOUND_404,
118 message: `Server ${req.params.host} not found.`
122 res.locals.server = server
127 const listVideoRedundanciesValidator = [
129 .custom(isVideoRedundancyTarget),
131 (req: express.Request, res: express.Response, next: express.NextFunction) => {
132 if (areValidationErrors(req, res)) return
138 const addVideoRedundancyValidator = [
140 .customSanitizer(toCompleteUUID)
141 .custom(isIdOrUUIDValid),
143 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
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')
172 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
173 if (areValidationErrors(req, res)) return
175 const redundancy = await VideoRedundancyModel.loadByIdWithVideo(forceNumber(req.params.redundancyId))
178 status: HttpStatusCode.NOT_FOUND_404,
179 message: 'Video redundancy not found'
183 res.locals.videoRedundancy = redundancy
189 // ---------------------------------------------------------------------------
192 videoFileRedundancyGetValidator,
193 videoPlaylistRedundancyGetValidator,
194 updateServerRedundancyValidator,
195 listVideoRedundanciesValidator,
196 addVideoRedundancyValidator,
197 removeVideoRedundancyValidator