1 import express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
7 USER_EMAIL_VERIFY_LIFETIME,
8 USER_PASSWORD_RESET_LIFETIME,
9 USER_PASSWORD_CREATE_LIFETIME,
13 RESUMABLE_UPLOAD_SESSION_LIFETIME
14 } from '../initializers/constants'
15 import { CONFIG } from '../initializers/config'
25 private static instance: Redis
26 private initialized = false
27 private client: RedisClient
28 private prefix: string
30 private constructor () {
34 // Already initialized
35 if (this.initialized === true) return
36 this.initialized = true
38 this.client = createClient(Redis.getRedisClientOptions())
40 this.client.on('error', err => {
41 logger.error('Error in Redis client.', { err })
45 if (CONFIG.REDIS.AUTH) {
46 this.client.auth(CONFIG.REDIS.AUTH)
49 this.prefix = 'redis-' + WEBSERVER.HOST + '-'
52 static getRedisClientOptions () {
53 return Object.assign({},
54 (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {},
55 (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {},
56 (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT)
57 ? { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT }
58 : { path: CONFIG.REDIS.SOCKET }
70 /* ************ Forgot password ************ */
72 async setResetPasswordVerificationString (userId: number) {
73 const generatedString = await generateRandomString(32)
75 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
77 return generatedString
80 async setCreatePasswordVerificationString (userId: number) {
81 const generatedString = await generateRandomString(32)
83 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_CREATE_LIFETIME)
85 return generatedString
88 async removePasswordVerificationString (userId: number) {
89 return this.removeValue(this.generateResetPasswordKey(userId))
92 async getResetPasswordLink (userId: number) {
93 return this.getValue(this.generateResetPasswordKey(userId))
96 /* ************ Email verification ************ */
98 async setVerifyEmailVerificationString (userId: number) {
99 const generatedString = await generateRandomString(32)
101 await this.setValue(this.generateVerifyEmailKey(userId), generatedString, USER_EMAIL_VERIFY_LIFETIME)
103 return generatedString
106 async getVerifyEmailLink (userId: number) {
107 return this.getValue(this.generateVerifyEmailKey(userId))
110 /* ************ Contact form per IP ************ */
112 async setContactFormIp (ip: string) {
113 return this.setValue(this.generateContactFormKey(ip), '1', CONTACT_FORM_LIFETIME)
116 async doesContactFormIpExist (ip: string) {
117 return this.exists(this.generateContactFormKey(ip))
120 /* ************ Views per IP ************ */
122 setIPVideoView (ip: string, videoUUID: string, isLive: boolean) {
123 const lifetime = isLive
125 : VIEW_LIFETIME.VIDEO
127 return this.setValue(this.generateViewKey(ip, videoUUID), '1', lifetime)
130 async doesVideoIPViewExist (ip: string, videoUUID: string) {
131 return this.exists(this.generateViewKey(ip, videoUUID))
134 /* ************ Tracker IP block ************ */
136 setTrackerBlockIP (ip: string) {
137 return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
140 async doesTrackerBlockIPExist (ip: string) {
141 return this.exists(this.generateTrackerBlockIPKey(ip))
144 /* ************ API cache ************ */
146 async getCachedRoute (req: express.Request) {
147 const cached = await this.getObject(this.generateCachedRouteKey(req))
149 return cached as CachedRoute
152 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
153 const cached: CachedRoute = Object.assign(
155 { body: body.toString() },
156 (contentType) ? { contentType } : null,
157 (statusCode) ? { statusCode: statusCode.toString() } : null
160 return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
163 /* ************ Video views ************ */
165 addVideoView (videoId: number) {
166 const keyIncr = this.generateVideoViewKey(videoId)
167 const keySet = this.generateVideosViewKey()
170 this.addToSet(keySet, videoId.toString()),
171 this.increment(keyIncr)
175 async getVideoViews (videoId: number, hour: number) {
176 const key = this.generateVideoViewKey(videoId, hour)
178 const valueString = await this.getValue(key)
179 const valueInt = parseInt(valueString, 10)
181 if (isNaN(valueInt)) {
182 logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
189 async getVideosIdViewed (hour: number) {
190 const key = this.generateVideosViewKey(hour)
192 const stringIds = await this.getSet(key)
193 return stringIds.map(s => parseInt(s, 10))
196 deleteVideoViews (videoId: number, hour: number) {
197 const keySet = this.generateVideosViewKey(hour)
198 const keyIncr = this.generateVideoViewKey(videoId, hour)
201 this.deleteFromSet(keySet, videoId.toString()),
202 this.deleteKey(keyIncr)
206 /* ************ Resumable uploads final responses ************ */
208 setUploadSession (uploadId: string, response?: { video: { id: number, shortUUID: string, uuid: string } }) {
209 return this.setValue(
210 'resumable-upload-' + uploadId,
212 ? JSON.stringify(response)
214 RESUMABLE_UPLOAD_SESSION_LIFETIME
218 doesUploadSessionExist (uploadId: string) {
219 return this.exists('resumable-upload-' + uploadId)
222 async getUploadSession (uploadId: string) {
223 const value = await this.getValue('resumable-upload-' + uploadId)
230 /* ************ Keys generation ************ */
232 generateCachedRouteKey (req: express.Request) {
233 return req.method + '-' + req.originalUrl
236 private generateVideosViewKey (hour?: number) {
237 if (!hour) hour = new Date().getHours()
239 return `videos-view-h${hour}`
242 private generateVideoViewKey (videoId: number, hour?: number) {
243 if (hour === undefined || hour === null) hour = new Date().getHours()
245 return `video-view-${videoId}-h${hour}`
248 private generateResetPasswordKey (userId: number) {
249 return 'reset-password-' + userId
252 private generateVerifyEmailKey (userId: number) {
253 return 'verify-email-' + userId
256 private generateViewKey (ip: string, videoUUID: string) {
257 return `views-${videoUUID}-${ip}`
260 private generateTrackerBlockIPKey (ip: string) {
261 return `tracker-block-ip-${ip}`
264 private generateContactFormKey (ip: string) {
265 return 'contact-form-' + ip
268 /* ************ Redis helpers ************ */
270 private getValue (key: string) {
271 return new Promise<string>((res, rej) => {
272 this.client.get(this.prefix + key, (err, value) => {
273 if (err) return rej(err)
280 private getSet (key: string) {
281 return new Promise<string[]>((res, rej) => {
282 this.client.smembers(this.prefix + key, (err, value) => {
283 if (err) return rej(err)
290 private addToSet (key: string, value: string) {
291 return new Promise<void>((res, rej) => {
292 this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res())
296 private deleteFromSet (key: string, value: string) {
297 return new Promise<void>((res, rej) => {
298 this.client.srem(this.prefix + key, value, err => err ? rej(err) : res())
302 private deleteKey (key: string) {
303 return new Promise<void>((res, rej) => {
304 this.client.del(this.prefix + key, err => err ? rej(err) : res())
308 private deleteFieldInHash (key: string, field: string) {
309 return new Promise<void>((res, rej) => {
310 this.client.hdel(this.prefix + key, field, err => err ? rej(err) : res())
314 private setValue (key: string, value: string, expirationMilliseconds: number) {
315 return new Promise<void>((res, rej) => {
316 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
317 if (err) return rej(err)
319 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
326 private removeValue (key: string) {
327 return new Promise<void>((res, rej) => {
328 this.client.del(this.prefix + key, err => {
329 if (err) return rej(err)
336 private setObject (key: string, obj: { [id: string]: string }, expirationMilliseconds: number) {
337 return new Promise<void>((res, rej) => {
338 this.client.hmset(this.prefix + key, obj, (err, ok) => {
339 if (err) return rej(err)
340 if (!ok) return rej(new Error('Redis mset result is not OK.'))
342 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
343 if (err) return rej(err)
344 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
352 private getObject (key: string) {
353 return new Promise<{ [id: string]: string }>((res, rej) => {
354 this.client.hgetall(this.prefix + key, (err, value) => {
355 if (err) return rej(err)
362 private setValueInHash (key: string, field: string, value: string) {
363 return new Promise<void>((res, rej) => {
364 this.client.hset(this.prefix + key, field, value, (err) => {
365 if (err) return rej(err)
372 private increment (key: string) {
373 return new Promise<number>((res, rej) => {
374 this.client.incr(this.prefix + key, (err, value) => {
375 if (err) return rej(err)
382 private exists (key: string) {
383 return new Promise<boolean>((res, rej) => {
384 this.client.exists(this.prefix + key, (err, existsNumber) => {
385 if (err) return rej(err)
387 return res(existsNumber === 1)
392 static get Instance () {
393 return this.instance || (this.instance = new this())
397 // ---------------------------------------------------------------------------