1 import LRUCache from 'lru-cache'
2 import { LRU_CACHE } from '@server/initializers/constants'
3 import { buildUUID } from '@shared/extra-utils'
5 // ---------------------------------------------------------------------------
6 // Create temporary tokens that can be used as URL query parameters to access video static files
7 // ---------------------------------------------------------------------------
9 class VideoTokensManager {
11 private static instance: VideoTokensManager
13 private readonly lruCache = new LRUCache<string, string>({
14 max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE,
15 ttl: LRU_CACHE.VIDEO_TOKENS.TTL
18 private constructor () {}
20 create (videoUUID: string) {
21 const token = buildUUID()
23 const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL)
25 this.lruCache.set(token, videoUUID)
27 return { token, expires }
34 const value = this.lruCache.get(options.token)
35 if (!value) return false
37 return value === options.videoUUID
40 static get Instance () {
41 return this.instance || (this.instance = new this())
45 // ---------------------------------------------------------------------------