]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { JobQueue } from '@server/lib/job-queue' | |
3 | import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy' | |
4 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' | |
5 | import { UserRight } from '../../../../shared/models/users' | |
6 | import { logger } from '../../../helpers/logger' | |
7 | import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy' | |
8 | import { | |
9 | asyncMiddleware, | |
10 | authenticate, | |
11 | ensureUserHasRight, | |
12 | paginationValidator, | |
13 | setDefaultPagination, | |
14 | setDefaultVideoRedundanciesSort, | |
15 | videoRedundanciesSortValidator | |
16 | } from '../../../middlewares' | |
17 | import { | |
18 | addVideoRedundancyValidator, | |
19 | listVideoRedundanciesValidator, | |
20 | removeVideoRedundancyValidator, | |
21 | updateServerRedundancyValidator | |
22 | } from '../../../middlewares/validators/redundancy' | |
23 | ||
24 | const serverRedundancyRouter = express.Router() | |
25 | ||
26 | serverRedundancyRouter.put('/redundancy/:host', | |
27 | authenticate, | |
28 | ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), | |
29 | asyncMiddleware(updateServerRedundancyValidator), | |
30 | asyncMiddleware(updateRedundancy) | |
31 | ) | |
32 | ||
33 | serverRedundancyRouter.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 | ||
44 | serverRedundancyRouter.post('/redundancy/videos', | |
45 | authenticate, | |
46 | ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES), | |
47 | addVideoRedundancyValidator, | |
48 | asyncMiddleware(addVideoRedundancy) | |
49 | ) | |
50 | ||
51 | serverRedundancyRouter.delete('/redundancy/videos/:redundancyId', | |
52 | authenticate, | |
53 | ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES), | |
54 | removeVideoRedundancyValidator, | |
55 | asyncMiddleware(removeVideoRedundancyController) | |
56 | ) | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | export { | |
61 | serverRedundancyRouter | |
62 | } | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
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, | |
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 | ||
83 | async function addVideoRedundancy (req: express.Request, res: express.Response) { | |
84 | const payload = { | |
85 | videoId: res.locals.onlyVideo.id | |
86 | } | |
87 | ||
88 | await JobQueue.Instance.createJob({ | |
89 | type: 'video-redundancy', | |
90 | payload | |
91 | }) | |
92 | ||
93 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
94 | } | |
95 | ||
96 | async function removeVideoRedundancyController (req: express.Request, res: express.Response) { | |
97 | await removeVideoRedundancy(res.locals.videoRedundancy) | |
98 | ||
99 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
100 | } | |
101 | ||
102 | async function updateRedundancy (req: express.Request, res: express.Response) { | |
103 | const server = res.locals.server | |
104 | ||
105 | server.redundancyAllowed = req.body.redundancyAllowed | |
106 | ||
107 | await server.save() | |
108 | ||
109 | if (server.redundancyAllowed !== true) { | |
110 | // Async, could be long | |
111 | removeRedundanciesOfServer(server.id) | |
112 | .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err })) | |
113 | } | |
114 | ||
115 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
116 | } |