]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/server/redundancy.ts
Merge branch 'release/2.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / redundancy.ts
CommitLineData
c48e82b5
C
1import * as express from 'express'
2import { UserRight } from '../../../../shared/models/users'
b764380a
C
3import {
4 asyncMiddleware,
5 authenticate,
6 ensureUserHasRight,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultVideoRedundanciesSort,
10 videoRedundanciesSortValidator
11} from '../../../middlewares'
12import {
13 listVideoRedundanciesValidator,
14 updateServerRedundancyValidator,
15 addVideoRedundancyValidator,
16 removeVideoRedundancyValidator
17} from '../../../middlewares/validators/redundancy'
18import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy'
161b061d 19import { logger } from '../../../helpers/logger'
b764380a
C
20import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
21import { JobQueue } from '@server/lib/job-queue'
c48e82b5
C
22
23const serverRedundancyRouter = express.Router()
24
25serverRedundancyRouter.put('/redundancy/:host',
26 authenticate,
27 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
28 asyncMiddleware(updateServerRedundancyValidator),
29 asyncMiddleware(updateRedundancy)
30)
31
b764380a
C
32serverRedundancyRouter.get('/redundancy/videos',
33 authenticate,
34 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
35 listVideoRedundanciesValidator,
36 paginationValidator,
37 videoRedundanciesSortValidator,
38 setDefaultVideoRedundanciesSort,
39 setDefaultPagination,
40 asyncMiddleware(listVideoRedundancies)
41)
42
43serverRedundancyRouter.post('/redundancy/videos',
44 authenticate,
45 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
46 addVideoRedundancyValidator,
47 asyncMiddleware(addVideoRedundancy)
48)
49
50serverRedundancyRouter.delete('/redundancy/videos/:redundancyId',
51 authenticate,
52 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
53 removeVideoRedundancyValidator,
54 asyncMiddleware(removeVideoRedundancyController)
55)
56
c48e82b5
C
57// ---------------------------------------------------------------------------
58
59export {
60 serverRedundancyRouter
61}
62
63// ---------------------------------------------------------------------------
64
b764380a
C
65async function listVideoRedundancies (req: express.Request, res: express.Response) {
66 const resultList = await VideoRedundancyModel.listForApi({
67 start: req.query.start,
68 count: req.query.count,
69 sort: req.query.sort,
70 target: req.query.target,
71 strategy: req.query.strategy
72 })
73
74 const result = {
75 total: resultList.total,
76 data: resultList.data.map(r => VideoRedundancyModel.toFormattedJSONStatic(r))
77 }
78
79 return res.json(result)
80}
81
82async function addVideoRedundancy (req: express.Request, res: express.Response) {
83 const payload = {
84 videoId: res.locals.onlyVideo.id
85 }
86
a1587156 87 await JobQueue.Instance.createJobWithPromise({
b764380a
C
88 type: 'video-redundancy',
89 payload
90 })
91
92 return res.sendStatus(204)
93}
94
95async function removeVideoRedundancyController (req: express.Request, res: express.Response) {
96 await removeVideoRedundancy(res.locals.videoRedundancy)
97
98 return res.sendStatus(204)
99}
100
dae86118
C
101async function updateRedundancy (req: express.Request, res: express.Response) {
102 const server = res.locals.server
c48e82b5
C
103
104 server.redundancyAllowed = req.body.redundancyAllowed
105
106 await server.save()
107
161b061d 108 // Async, could be long
b764380a 109 removeRedundanciesOfServer(server.id)
a18bb55e 110 .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err }))
161b061d 111
c48e82b5
C
112 return res.sendStatus(204)
113}