From 3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Oct 2022 16:09:02 +0200 Subject: Put private videos under a specific subdirectory --- server/lib/video-tokens-manager.ts | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 server/lib/video-tokens-manager.ts (limited to 'server/lib/video-tokens-manager.ts') diff --git a/server/lib/video-tokens-manager.ts b/server/lib/video-tokens-manager.ts new file mode 100644 index 000000000..c43085d16 --- /dev/null +++ b/server/lib/video-tokens-manager.ts @@ -0,0 +1,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({ + 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 +} -- cgit v1.2.3