]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Add ability to click on category/licence/language/tags in watch page
[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
3fd3ab2d 107function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
eb080476
C
108 Object.keys(savedFields).forEach(key => {
109 const value = savedFields[key]
110 instance.set(key, value)
111 })
112}
113
50d6de9c
C
114let serverActor: ActorModel
115async function getServerActor () {
116 if (serverActor === undefined) {
117 const application = await ApplicationModel.load()
118 serverActor = application.Account.Actor
7a7724e6
C
119 }
120
50d6de9c
C
121 if (!serverActor) {
122 logger.error('Cannot load server actor.')
efc32059
C
123 process.exit(0)
124 }
125
50d6de9c 126 return Promise.resolve(serverActor)
7a7724e6
C
127}
128
792dbaf0
GS
129type SortType = { sortModel: any, sortValue: string }
130
9f10b292 131// ---------------------------------------------------------------------------
c45f7f84 132
65fcc311 133export {
65fcc311 134 generateRandomString,
0aef76c4 135 getFormattedObjects,
792dbaf0 136 isSignupAllowed,
ff2c1fe8 137 isSignupAllowedForCurrentIP,
40298b02 138 computeResolutionsToTranscode,
eb080476 139 resetSequelizeInstance,
50d6de9c 140 getServerActor,
0626e7af 141 SortType
65fcc311 142}