2022-10-12 16:09:02 +02:00
|
|
|
import express from 'express'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { VideoTokensManager } from '@server/lib/video-tokens-manager.js'
|
|
|
|
import { VideoPrivacy, VideoToken } from '@peertube/peertube-models'
|
|
|
|
import { asyncMiddleware, optionalAuthenticate, videoFileTokenValidator, videosCustomGetValidator } from '../../../middlewares/index.js'
|
2022-10-12 16:09:02 +02:00
|
|
|
|
|
|
|
const tokenRouter = express.Router()
|
|
|
|
|
|
|
|
tokenRouter.post('/:id/token',
|
2023-06-29 09:48:55 +02:00
|
|
|
optionalAuthenticate,
|
2024-04-26 08:58:35 +02:00
|
|
|
asyncMiddleware(videosCustomGetValidator('only-video-and-blacklist')),
|
2023-06-29 09:48:55 +02:00
|
|
|
videoFileTokenValidator,
|
2022-10-12 16:09:02 +02:00
|
|
|
generateToken
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
tokenRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function generateToken (req: express.Request, res: express.Response) {
|
|
|
|
const video = res.locals.onlyVideo
|
|
|
|
|
2023-06-29 09:48:55 +02:00
|
|
|
const files = video.privacy === VideoPrivacy.PASSWORD_PROTECTED
|
|
|
|
? VideoTokensManager.Instance.createForPasswordProtectedVideo({ videoUUID: video.uuid })
|
|
|
|
: VideoTokensManager.Instance.createForAuthUser({ videoUUID: video.uuid, user: res.locals.oauth.token.User })
|
2022-10-12 16:09:02 +02:00
|
|
|
|
|
|
|
return res.json({
|
2023-06-29 09:48:55 +02:00
|
|
|
files
|
2022-10-12 16:09:02 +02:00
|
|
|
} as VideoToken)
|
|
|
|
}
|