2022-10-12 16:09:02 +02:00
|
|
|
import express from 'express'
|
|
|
|
import { VideoTokensManager } from '@server/lib/video-tokens-manager'
|
2023-06-29 09:48:55 +02:00
|
|
|
import { VideoPrivacy, VideoToken } from '@shared/models'
|
|
|
|
import { asyncMiddleware, optionalAuthenticate, videoFileTokenValidator, videosCustomGetValidator } from '../../../middlewares'
|
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,
|
2022-10-12 16:09:02 +02:00
|
|
|
asyncMiddleware(videosCustomGetValidator('only-video')),
|
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)
|
|
|
|
}
|