X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fserver%2Fredundancy.ts;h=e36c8d2db44fbfa748f36833e863709a715ab946;hb=7b6cf83e33c1cf9719f77338288b9145b8cdcf60;hp=4ea6164a38c0342a839d5207ef9c44c1dd428f6b;hpb=a18bb55e9b72664dff4043785842d6d406dd15a3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/server/redundancy.ts b/server/controllers/api/server/redundancy.ts index 4ea6164a3..e36c8d2db 100644 --- a/server/controllers/api/server/redundancy.ts +++ b/server/controllers/api/server/redundancy.ts @@ -1,9 +1,25 @@ import * as express from 'express' +import { JobQueue } from '@server/lib/job-queue' +import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy' +import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' import { UserRight } from '../../../../shared/models/users' -import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' -import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy' -import { removeRedundancyOf } from '../../../lib/redundancy' import { logger } from '../../../helpers/logger' +import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy' +import { + asyncMiddleware, + authenticate, + ensureUserHasRight, + paginationValidator, + setDefaultPagination, + setDefaultVideoRedundanciesSort, + videoRedundanciesSortValidator +} from '../../../middlewares' +import { + addVideoRedundancyValidator, + listVideoRedundanciesValidator, + removeVideoRedundancyValidator, + updateServerRedundancyValidator +} from '../../../middlewares/validators/redundancy' const serverRedundancyRouter = express.Router() @@ -14,6 +30,31 @@ serverRedundancyRouter.put('/redundancy/:host', asyncMiddleware(updateRedundancy) ) +serverRedundancyRouter.get('/redundancy/videos', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES), + listVideoRedundanciesValidator, + paginationValidator, + videoRedundanciesSortValidator, + setDefaultVideoRedundanciesSort, + setDefaultPagination, + asyncMiddleware(listVideoRedundancies) +) + +serverRedundancyRouter.post('/redundancy/videos', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES), + addVideoRedundancyValidator, + asyncMiddleware(addVideoRedundancy) +) + +serverRedundancyRouter.delete('/redundancy/videos/:redundancyId', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES), + removeVideoRedundancyValidator, + asyncMiddleware(removeVideoRedundancyController) +) + // --------------------------------------------------------------------------- export { @@ -22,6 +63,42 @@ export { // --------------------------------------------------------------------------- +async function listVideoRedundancies (req: express.Request, res: express.Response) { + const resultList = await VideoRedundancyModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + target: req.query.target, + strategy: req.query.strategy + }) + + const result = { + total: resultList.total, + data: resultList.data.map(r => VideoRedundancyModel.toFormattedJSONStatic(r)) + } + + return res.json(result) +} + +async function addVideoRedundancy (req: express.Request, res: express.Response) { + const payload = { + videoId: res.locals.onlyVideo.id + } + + await JobQueue.Instance.createJobWithPromise({ + type: 'video-redundancy', + payload + }) + + return res.status(HttpStatusCode.NO_CONTENT_204).end() +} + +async function removeVideoRedundancyController (req: express.Request, res: express.Response) { + await removeVideoRedundancy(res.locals.videoRedundancy) + + return res.status(HttpStatusCode.NO_CONTENT_204).end() +} + async function updateRedundancy (req: express.Request, res: express.Response) { const server = res.locals.server @@ -29,9 +106,11 @@ async function updateRedundancy (req: express.Request, res: express.Response) { await server.save() - // Async, could be long - removeRedundancyOf(server.id) - .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err })) + if (server.redundancyAllowed !== true) { + // Async, could be long + removeRedundanciesOfServer(server.id) + .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err })) + } - return res.sendStatus(204) + return res.status(HttpStatusCode.NO_CONTENT_204).end() }