aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-tokens-manager.ts
blob: c43085d167631d5bf08956a31c20aceb7dda922f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import LRUCache from 'lru-cache'
import { LRU_CACHE } from '@server/initializers/constants'
import { buildUUID } from '@shared/extra-utils'

// ---------------------------------------------------------------------------
// Create temporary tokens that can be used as URL query parameters to access video static files
// ---------------------------------------------------------------------------

class VideoTokensManager {

  private static instance: VideoTokensManager

  private readonly lruCache = new LRUCache<string, string>({
    max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE,
    ttl: LRU_CACHE.VIDEO_TOKENS.TTL
  })

  private constructor () {}

  create (videoUUID: string) {
    const token = buildUUID()

    const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL)

    this.lruCache.set(token, videoUUID)

    return { token, expires }
  }

  hasToken (options: {
    token: string
    videoUUID: string
  }) {
    const value = this.lruCache.get(options.token)
    if (!value) return false

    return value === options.videoUUID
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}

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

export {
  VideoTokensManager
}