]>
Commit | Line | Data |
---|---|---|
3545e72c C |
1 | import LRUCache from 'lru-cache' |
2 | import { LRU_CACHE } from '@server/initializers/constants' | |
3 | import { buildUUID } from '@shared/extra-utils' | |
4 | ||
5 | // --------------------------------------------------------------------------- | |
6 | // Create temporary tokens that can be used as URL query parameters to access video static files | |
7 | // --------------------------------------------------------------------------- | |
8 | ||
9 | class VideoTokensManager { | |
10 | ||
11 | private static instance: VideoTokensManager | |
12 | ||
13 | private readonly lruCache = new LRUCache<string, string>({ | |
14 | max: LRU_CACHE.VIDEO_TOKENS.MAX_SIZE, | |
15 | ttl: LRU_CACHE.VIDEO_TOKENS.TTL | |
16 | }) | |
17 | ||
18 | private constructor () {} | |
19 | ||
20 | create (videoUUID: string) { | |
21 | const token = buildUUID() | |
22 | ||
23 | const expires = new Date(new Date().getTime() + LRU_CACHE.VIDEO_TOKENS.TTL) | |
24 | ||
25 | this.lruCache.set(token, videoUUID) | |
26 | ||
27 | return { token, expires } | |
28 | } | |
29 | ||
30 | hasToken (options: { | |
31 | token: string | |
32 | videoUUID: string | |
33 | }) { | |
34 | const value = this.lruCache.get(options.token) | |
35 | if (!value) return false | |
36 | ||
37 | return value === options.videoUUID | |
38 | } | |
39 | ||
40 | static get Instance () { | |
41 | return this.instance || (this.instance = new this()) | |
42 | } | |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | VideoTokensManager | |
49 | } |