]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Fix nodeinfo endpoint
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
3fd3ab2d 1import { Model } from 'sequelize-typescript'
ff2c1fe8 2import * as ipaddr from 'ipaddr.js'
6fcd19ba 3import { ResultList } from '../../shared'
3fd3ab2d 4import { VideoResolution } from '../../shared/models/videos'
0626e7af 5import { CONFIG } from '../initializers'
3fd3ab2d 6import { UserModel } from '../models/account/user'
50d6de9c
C
7import { ActorModel } from '../models/activitypub/actor'
8import { ApplicationModel } from '../models/application/application'
8d468a16 9import { pseudoRandomBytesPromise } from './core-utils'
efc32059 10import { logger } from './logger'
cbe2f7c3 11
2186386c
C
12const isCidr = require('is-cidr')
13
f5028693
C
14async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size)
16
17 return raw.toString('hex')
e4c87ec2
C
18}
19
40298b02 20interface FormattableToJSON {
2186386c 21 toFormattedJSON (args?: any)
154898b0
C
22}
23
2186386c 24function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
0aef76c4 25 const formattedObjects: U[] = []
55fa55a9 26
075f16ca 27 objects.forEach(object => {
2186386c 28 formattedObjects.push(object.toFormattedJSON(formattedArg))
55fa55a9
C
29 })
30
2186386c 31 return {
55fa55a9 32 total: objectsTotal,
0aef76c4 33 data: formattedObjects
2186386c 34 } as ResultList<U>
55fa55a9
C
35}
36
f5028693 37async function isSignupAllowed () {
291e8d3e 38 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 39 return false
291e8d3e
C
40 }
41
42 // No limit and signup is enabled
43 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 44 return true
291e8d3e
C
45 }
46
3fd3ab2d 47 const totalUsers = await UserModel.countTotal()
f5028693
C
48
49 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
50}
51
ff2c1fe8
RK
52function isSignupAllowedForCurrentIP (ip: string) {
53 const addr = ipaddr.parse(ip)
54 let excludeList = [ 'blacklist' ]
55 let matched: string
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
40298b02
C
85function computeResolutionsToTranscode (videoFileHeight: number) {
86 const resolutionsEnabled: number[] = []
87 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
88
2186386c 89 // Put in the order we want to proceed jobs
40298b02 90 const resolutions = [
40298b02 91 VideoResolution.H_480P,
2186386c 92 VideoResolution.H_360P,
40298b02 93 VideoResolution.H_720P,
2186386c 94 VideoResolution.H_240P,
40298b02
C
95 VideoResolution.H_1080P
96 ]
97
98 for (const resolution of resolutions) {
2186386c 99 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
40298b02
C
100 resolutionsEnabled.push(resolution)
101 }
102 }
103
104 return resolutionsEnabled
105}
106
3f6d68d9
RK
107const 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}
98d3324d 116export function parseDuration (duration: number | string): number {
3f6d68d9
RK
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
98d3324d 133 throw new Error('Duration could not be properly parsed')
3f6d68d9
RK
134}
135
3fd3ab2d 136function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
eb080476
C
137 Object.keys(savedFields).forEach(key => {
138 const value = savedFields[key]
139 instance.set(key, value)
140 })
141}
142
50d6de9c
C
143let serverActor: ActorModel
144async function getServerActor () {
145 if (serverActor === undefined) {
146 const application = await ApplicationModel.load()
147 serverActor = application.Account.Actor
7a7724e6
C
148 }
149
50d6de9c
C
150 if (!serverActor) {
151 logger.error('Cannot load server actor.')
efc32059
C
152 process.exit(0)
153 }
154
50d6de9c 155 return Promise.resolve(serverActor)
7a7724e6
C
156}
157
792dbaf0
GS
158type SortType = { sortModel: any, sortValue: string }
159
9f10b292 160// ---------------------------------------------------------------------------
c45f7f84 161
65fcc311 162export {
65fcc311 163 generateRandomString,
0aef76c4 164 getFormattedObjects,
792dbaf0 165 isSignupAllowed,
ff2c1fe8 166 isSignupAllowedForCurrentIP,
40298b02 167 computeResolutionsToTranscode,
eb080476 168 resetSequelizeInstance,
50d6de9c 169 getServerActor,
0626e7af 170 SortType
65fcc311 171}