1 import * as express from 'express'
2 import { UserRight } from '../../../../shared/models/users'
9 setDefaultVideoRedundanciesSort,
10 videoRedundanciesSortValidator
11 } from '../../../middlewares'
13 listVideoRedundanciesValidator,
14 updateServerRedundancyValidator,
15 addVideoRedundancyValidator,
16 removeVideoRedundancyValidator
17 } from '../../../middlewares/validators/redundancy'
18 import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy'
19 import { logger } from '../../../helpers/logger'
20 import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
21 import { JobQueue } from '@server/lib/job-queue'
22 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
24 const serverRedundancyRouter = express.Router()
26 serverRedundancyRouter.put('/redundancy/:host',
28 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
29 asyncMiddleware(updateServerRedundancyValidator),
30 asyncMiddleware(updateRedundancy)
33 serverRedundancyRouter.get('/redundancy/videos',
35 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
36 listVideoRedundanciesValidator,
38 videoRedundanciesSortValidator,
39 setDefaultVideoRedundanciesSort,
41 asyncMiddleware(listVideoRedundancies)
44 serverRedundancyRouter.post('/redundancy/videos',
46 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
47 addVideoRedundancyValidator,
48 asyncMiddleware(addVideoRedundancy)
51 serverRedundancyRouter.delete('/redundancy/videos/:redundancyId',
53 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
54 removeVideoRedundancyValidator,
55 asyncMiddleware(removeVideoRedundancyController)
58 // ---------------------------------------------------------------------------
61 serverRedundancyRouter
64 // ---------------------------------------------------------------------------
66 async function listVideoRedundancies (req: express.Request, res: express.Response) {
67 const resultList = await VideoRedundancyModel.listForApi({
68 start: req.query.start,
69 count: req.query.count,
71 target: req.query.target,
72 strategy: req.query.strategy
76 total: resultList.total,
77 data: resultList.data.map(r => VideoRedundancyModel.toFormattedJSONStatic(r))
80 return res.json(result)
83 async function addVideoRedundancy (req: express.Request, res: express.Response) {
85 videoId: res.locals.onlyVideo.id
88 await JobQueue.Instance.createJobWithPromise({
89 type: 'video-redundancy',
93 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
96 async function removeVideoRedundancyController (req: express.Request, res: express.Response) {
97 await removeVideoRedundancy(res.locals.videoRedundancy)
99 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
102 async function updateRedundancy (req: express.Request, res: express.Response) {
103 const server = res.locals.server
105 server.redundancyAllowed = req.body.redundancyAllowed
109 // Async, could be long
110 removeRedundanciesOfServer(server.id)
111 .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err }))
113 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)