]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/server/redundancy.ts
Add new plugin/peertube version notifs
[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'
2d53be02 22import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
c48e82b5
C
23
24const serverRedundancyRouter = express.Router()
25
26serverRedundancyRouter.put('/redundancy/:host',
27 authenticate,
28 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
29 asyncMiddleware(updateServerRedundancyValidator),
30 asyncMiddleware(updateRedundancy)
31)
32
b764380a
C
33serverRedundancyRouter.get('/redundancy/videos',
34 authenticate,
35 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
36 listVideoRedundanciesValidator,
37 paginationValidator,
38 videoRedundanciesSortValidator,
39 setDefaultVideoRedundanciesSort,
40 setDefaultPagination,
41 asyncMiddleware(listVideoRedundancies)
42)
43
44serverRedundancyRouter.post('/redundancy/videos',
45 authenticate,
46 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
47 addVideoRedundancyValidator,
48 asyncMiddleware(addVideoRedundancy)
49)
50
51serverRedundancyRouter.delete('/redundancy/videos/:redundancyId',
52 authenticate,
53 ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
54 removeVideoRedundancyValidator,
55 asyncMiddleware(removeVideoRedundancyController)
56)
57
c48e82b5
C
58// ---------------------------------------------------------------------------
59
60export {
61 serverRedundancyRouter
62}
63
64// ---------------------------------------------------------------------------
65
b764380a
C
66async function listVideoRedundancies (req: express.Request, res: express.Response) {
67 const resultList = await VideoRedundancyModel.listForApi({
68 start: req.query.start,
69 count: req.query.count,
70 sort: req.query.sort,
71 target: req.query.target,
72 strategy: req.query.strategy
73 })
74
75 const result = {
76 total: resultList.total,
77 data: resultList.data.map(r => VideoRedundancyModel.toFormattedJSONStatic(r))
78 }
79
80 return res.json(result)
81}
82
83async function addVideoRedundancy (req: express.Request, res: express.Response) {
84 const payload = {
85 videoId: res.locals.onlyVideo.id
86 }
87
a1587156 88 await JobQueue.Instance.createJobWithPromise({
b764380a
C
89 type: 'video-redundancy',
90 payload
91 })
92
2d53be02 93 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
b764380a
C
94}
95
96async function removeVideoRedundancyController (req: express.Request, res: express.Response) {
97 await removeVideoRedundancy(res.locals.videoRedundancy)
98
2d53be02 99 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
b764380a
C
100}
101
dae86118
C
102async function updateRedundancy (req: express.Request, res: express.Response) {
103 const server = res.locals.server
c48e82b5
C
104
105 server.redundancyAllowed = req.body.redundancyAllowed
106
107 await server.save()
108
161b061d 109 // Async, could be long
b764380a 110 removeRedundanciesOfServer(server.id)
a18bb55e 111 .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err }))
161b061d 112
2d53be02 113 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
c48e82b5 114}