Fix last commit
[oweals/peertube.git] / server / helpers / utils.ts
1 import { Model } from 'sequelize-typescript'
2 import * as ipaddr from 'ipaddr.js'
3 import { ResultList } from '../../shared'
4 import { VideoResolution } from '../../shared/models/videos'
5 import { CONFIG } from '../initializers'
6 import { UserModel } from '../models/account/user'
7 import { ActorModel } from '../models/activitypub/actor'
8 import { ApplicationModel } from '../models/application/application'
9 import { pseudoRandomBytesPromise } from './core-utils'
10 import { logger } from './logger'
11
12 const isCidr = require('is-cidr')
13
14 async function generateRandomString (size: number) {
15   const raw = await pseudoRandomBytesPromise(size)
16
17   return raw.toString('hex')
18 }
19
20 interface FormattableToJSON {
21   toFormattedJSON (args?: any)
22 }
23
24 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
25   const formattedObjects: U[] = []
26
27   objects.forEach(object => {
28     formattedObjects.push(object.toFormattedJSON(formattedArg))
29   })
30
31   return {
32     total: objectsTotal,
33     data: formattedObjects
34   } as ResultList<U>
35 }
36
37 async function isSignupAllowed () {
38   if (CONFIG.SIGNUP.ENABLED === false) {
39     return false
40   }
41
42   // No limit and signup is enabled
43   if (CONFIG.SIGNUP.LIMIT === -1) {
44     return true
45   }
46
47   const totalUsers = await UserModel.countTotal()
48
49   return totalUsers < CONFIG.SIGNUP.LIMIT
50 }
51
52 function isSignupAllowedForCurrentIP (ip: string) {
53   const addr = ipaddr.parse(ip)
54   let excludeList = [ 'blacklist' ]
55   let matched = ''
56
57   // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
58   if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
59     excludeList.push('unknown')
60   }
61
62   if (addr.kind() === 'ipv4') {
63     const addrV4 = ipaddr.IPv4.parse(ip)
64     const rangeList = {
65       whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
66                                                 .map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
67       blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
68                                                 .map(cidr => ipaddr.IPv4.parseCIDR(cidr))
69     }
70     matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
71   } else if (addr.kind() === 'ipv6') {
72     const addrV6 = ipaddr.IPv6.parse(ip)
73     const rangeList = {
74       whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
75                                                 .map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
76       blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
77                                                 .map(cidr => ipaddr.IPv6.parseCIDR(cidr))
78     }
79     matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
80   }
81
82   return !excludeList.includes(matched)
83 }
84
85 function computeResolutionsToTranscode (videoFileHeight: number) {
86   const resolutionsEnabled: number[] = []
87   const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
88
89   // Put in the order we want to proceed jobs
90   const resolutions = [
91     VideoResolution.H_480P,
92     VideoResolution.H_360P,
93     VideoResolution.H_720P,
94     VideoResolution.H_240P,
95     VideoResolution.H_1080P
96   ]
97
98   for (const resolution of resolutions) {
99     if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
100       resolutionsEnabled.push(resolution)
101     }
102   }
103
104   return resolutionsEnabled
105 }
106
107 const timeTable = {
108   ms:           1,
109   second:       1000,
110   minute:       60000,
111   hour:         3600000,
112   day:          3600000 * 24,
113   week:         3600000 * 24 * 7,
114   month:        3600000 * 24 * 30
115 }
116 export function parseDuration (duration: number | string): number {
117   if (typeof duration === 'number') return duration
118
119   if (typeof duration === 'string') {
120     const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
121
122     if (split.length === 3) {
123       const len = parseFloat(split[1])
124       let unit = split[2].replace(/s$/i,'').toLowerCase()
125       if (unit === 'm') {
126         unit = 'ms'
127       }
128
129       return (len || 1) * (timeTable[unit] || 0)
130     }
131   }
132
133   throw new Error('Duration could not be properly parsed')
134 }
135
136 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
137   Object.keys(savedFields).forEach(key => {
138     const value = savedFields[key]
139     instance.set(key, value)
140   })
141 }
142
143 let serverActor: ActorModel
144 async function getServerActor () {
145   if (serverActor === undefined) {
146     const application = await ApplicationModel.load()
147     if (!application) throw Error('Could not load Application from database.')
148
149     serverActor = application.Account.Actor
150   }
151
152   if (!serverActor) {
153     logger.error('Cannot load server actor.')
154     process.exit(0)
155   }
156
157   return Promise.resolve(serverActor)
158 }
159
160 type SortType = { sortModel: any, sortValue: string }
161
162 // ---------------------------------------------------------------------------
163
164 export {
165   generateRandomString,
166   getFormattedObjects,
167   isSignupAllowed,
168   isSignupAllowedForCurrentIP,
169   computeResolutionsToTranscode,
170   resetSequelizeInstance,
171   getServerActor,
172   SortType
173 }