aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/controllers/api/videos/token.ts
blob: e961ffd9ec897db85d164b17240a8ec61d451ff9 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                                                     

                                                                                                                               



                                    
                       
                                                          
                          













                                                                              


                                                                                                                 

                   
         

                  
import express from 'express'
import { VideoTokensManager } from '@server/lib/video-tokens-manager'
import { VideoPrivacy, VideoToken } from '@shared/models'
import { asyncMiddleware, optionalAuthenticate, videoFileTokenValidator, videosCustomGetValidator } from '../../../middlewares'

const tokenRouter = express.Router()

tokenRouter.post('/:id/token',
  optionalAuthenticate,
  asyncMiddleware(videosCustomGetValidator('only-video')),
  videoFileTokenValidator,
  generateToken
)

// ---------------------------------------------------------------------------

export {
  tokenRouter
}

// ---------------------------------------------------------------------------

function generateToken (req: express.Request, res: express.Response) {
  const video = res.locals.onlyVideo

  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 })

  return res.json({
    files
  } as VideoToken)
}