1 import { LRUCache } from 'lru-cache'
2 import { LRU_CACHE } from '@server/initializers/constants'
3 import { MUserAccountUrl } from '@server/types/models'
4 import { pick } from '@shared/core-utils'
5 import { buildUUID } from '@shared/extra-utils'
7 // ---------------------------------------------------------------------------
8 // Create temporary tokens that can be used as URL query parameters to access video static files
9 // ---------------------------------------------------------------------------
11 class VideoTokensManager {
13 private static instance: VideoTokensManager
15 private readonly lruCache = new LRUCache<string, { videoUUID: string, user: MUserAccountUrl }>({
16 max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE,
17 ttl: LRU_CACHE.VIDEO_TOKENS.TTL
20 private constructor () {}
26 const token = buildUUID()
28 const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL)
30 this.lruCache.set(token, pick(options, [ 'user', 'videoUUID' ]))
32 return { token, expires }
39 const value = this.lruCache.get(options.token)
40 if (!value) return false
42 return value.videoUUID === options.videoUUID
45 getUserFromToken (options: {
48 const value = this.lruCache.get(options.token)
49 if (!value) return undefined
54 static get Instance () {
55 return this.instance || (this.instance = new this())
59 // ---------------------------------------------------------------------------