2021-08-27 14:32:44 +02:00
|
|
|
import express from 'express'
|
2021-07-16 10:42:24 +02:00
|
|
|
import { JobQueue } from '@server/lib/job-queue'
|
|
|
|
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
|
|
|
|
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
|
2018-09-11 16:27:07 +02:00
|
|
|
import { UserRight } from '../../../../shared/models/users'
|
2021-07-16 10:42:24 +02:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy'
|
2020-01-10 10:11:28 +01:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultVideoRedundanciesSort,
|
|
|
|
videoRedundanciesSortValidator
|
|
|
|
} from '../../../middlewares'
|
|
|
|
import {
|
|
|
|
addVideoRedundancyValidator,
|
2021-07-16 10:42:24 +02:00
|
|
|
listVideoRedundanciesValidator,
|
|
|
|
removeVideoRedundancyValidator,
|
|
|
|
updateServerRedundancyValidator
|
2020-01-10 10:11:28 +01:00
|
|
|
} from '../../../middlewares/validators/redundancy'
|
2018-09-11 16:27:07 +02:00
|
|
|
|
|
|
|
const serverRedundancyRouter = express.Router()
|
|
|
|
|
|
|
|
serverRedundancyRouter.put('/redundancy/:host',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
|
|
|
|
asyncMiddleware(updateServerRedundancyValidator),
|
|
|
|
asyncMiddleware(updateRedundancy)
|
|
|
|
)
|
|
|
|
|
2020-01-10 10:11:28 +01:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2018-09-11 16:27:07 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
serverRedundancyRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-01-10 10:11:28 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
await JobQueue.Instance.createJobWithPromise({
|
2020-01-10 10:11:28 +01:00
|
|
|
type: 'video-redundancy',
|
|
|
|
payload
|
|
|
|
})
|
|
|
|
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2020-01-10 10:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoRedundancyController (req: express.Request, res: express.Response) {
|
|
|
|
await removeVideoRedundancy(res.locals.videoRedundancy)
|
|
|
|
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2020-01-10 10:11:28 +01:00
|
|
|
}
|
|
|
|
|
2019-03-19 10:35:15 +01:00
|
|
|
async function updateRedundancy (req: express.Request, res: express.Response) {
|
|
|
|
const server = res.locals.server
|
2018-09-11 16:27:07 +02:00
|
|
|
|
|
|
|
server.redundancyAllowed = req.body.redundancyAllowed
|
|
|
|
|
|
|
|
await server.save()
|
|
|
|
|
2021-08-26 10:00:12 +02:00
|
|
|
if (server.redundancyAllowed !== true) {
|
|
|
|
// Async, could be long
|
|
|
|
removeRedundanciesOfServer(server.id)
|
|
|
|
.catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err }))
|
|
|
|
}
|
2018-09-28 10:07:05 +02:00
|
|
|
|
2021-06-01 01:36:53 +02:00
|
|
|
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
2018-09-11 16:27:07 +02:00
|
|
|
}
|