1 import * as express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
5 import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
15 private static instance: Redis
16 private initialized = false
17 private client: RedisClient
18 private prefix: string
20 private constructor () {}
23 // Already initialized
24 if (this.initialized === true) return
25 this.initialized = true
27 this.client = createClient(Redis.getRedisClient())
29 this.client.on('error', err => {
30 logger.error('Error in Redis client.', { err })
34 if (CONFIG.REDIS.AUTH) {
35 this.client.auth(CONFIG.REDIS.AUTH)
38 this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
41 static getRedisClient () {
42 return Object.assign({},
43 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
44 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
45 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ?
46 { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } :
47 { path: CONFIG.REDIS.SOCKET }
51 async setResetPasswordVerificationString (userId: number) {
52 const generatedString = await generateRandomString(32)
54 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
56 return generatedString
59 async getResetPasswordLink (userId: number) {
60 return this.getValue(this.generateResetPasswordKey(userId))
63 setView (ip: string, videoUUID: string) {
64 return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
67 async isViewExists (ip: string, videoUUID: string) {
68 return this.exists(this.buildViewKey(ip, videoUUID))
71 async getCachedRoute (req: express.Request) {
72 const cached = await this.getObject(this.buildCachedRouteKey(req))
74 return cached as CachedRoute
77 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
78 const cached: CachedRoute = Object.assign({}, {
81 (contentType) ? { contentType } : null,
82 (statusCode) ? { statusCode: statusCode.toString() } : null
85 return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
88 generateResetPasswordKey (userId: number) {
89 return 'reset-password-' + userId
92 buildViewKey (ip: string, videoUUID: string) {
93 return videoUUID + '-' + ip
96 buildCachedRouteKey (req: express.Request) {
97 return req.method + '-' + req.originalUrl
100 private getValue (key: string) {
101 return new Promise<string>((res, rej) => {
102 this.client.get(this.prefix + key, (err, value) => {
103 if (err) return rej(err)
110 private setValue (key: string, value: string, expirationMilliseconds: number) {
111 return new Promise<void>((res, rej) => {
112 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
113 if (err) return rej(err)
115 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
122 private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) {
123 return new Promise<void>((res, rej) => {
124 this.client.hmset(this.prefix + key, obj, (err, ok) => {
125 if (err) return rej(err)
126 if (!ok) return rej(new Error('Redis mset result is not OK.'))
128 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
129 if (err) return rej(err)
130 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
138 private getObject (key: string) {
139 return new Promise<{ [ id: string ]: string }>((res, rej) => {
140 this.client.hgetall(this.prefix + key, (err, value) => {
141 if (err) return rej(err)
148 private exists (key: string) {
149 return new Promise<boolean>((res, rej) => {
150 this.client.exists(this.prefix + key, (err, existsNumber) => {
151 if (err) return rej(err)
153 return res(existsNumber === 1)
158 static get Instance () {
159 return this.instance || (this.instance = new this())
163 // ---------------------------------------------------------------------------