]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { Model } from 'sequelize-typescript' |
ff2c1fe8 RK |
2 | import * as ipaddr from 'ipaddr.js' |
3 | const isCidr = require('is-cidr') | |
6fcd19ba | 4 | import { ResultList } from '../../shared' |
3fd3ab2d | 5 | import { VideoResolution } from '../../shared/models/videos' |
0626e7af | 6 | import { CONFIG } from '../initializers' |
3fd3ab2d | 7 | import { UserModel } from '../models/account/user' |
50d6de9c C |
8 | import { ActorModel } from '../models/activitypub/actor' |
9 | import { ApplicationModel } from '../models/application/application' | |
8d468a16 | 10 | import { pseudoRandomBytesPromise } from './core-utils' |
efc32059 | 11 | import { logger } from './logger' |
cbe2f7c3 | 12 | |
f5028693 C |
13 | async function generateRandomString (size: number) { |
14 | const raw = await pseudoRandomBytesPromise(size) | |
15 | ||
16 | return raw.toString('hex') | |
e4c87ec2 C |
17 | } |
18 | ||
40298b02 | 19 | interface FormattableToJSON { |
0aef76c4 | 20 | toFormattedJSON () |
154898b0 C |
21 | } |
22 | ||
40298b02 | 23 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { |
0aef76c4 | 24 | const formattedObjects: U[] = [] |
55fa55a9 | 25 | |
075f16ca | 26 | objects.forEach(object => { |
0aef76c4 | 27 | formattedObjects.push(object.toFormattedJSON()) |
55fa55a9 C |
28 | }) |
29 | ||
6fcd19ba | 30 | const res: ResultList<U> = { |
55fa55a9 | 31 | total: objectsTotal, |
0aef76c4 | 32 | data: formattedObjects |
55fa55a9 | 33 | } |
6fcd19ba C |
34 | |
35 | return res | |
55fa55a9 C |
36 | } |
37 | ||
f5028693 | 38 | async function isSignupAllowed () { |
291e8d3e | 39 | if (CONFIG.SIGNUP.ENABLED === false) { |
f5028693 | 40 | return false |
291e8d3e C |
41 | } |
42 | ||
43 | // No limit and signup is enabled | |
44 | if (CONFIG.SIGNUP.LIMIT === -1) { | |
f5028693 | 45 | return true |
291e8d3e C |
46 | } |
47 | ||
3fd3ab2d | 48 | const totalUsers = await UserModel.countTotal() |
f5028693 C |
49 | |
50 | return totalUsers < CONFIG.SIGNUP.LIMIT | |
291e8d3e C |
51 | } |
52 | ||
ff2c1fe8 RK |
53 | function isSignupAllowedForCurrentIP (ip: string) { |
54 | const addr = ipaddr.parse(ip) | |
55 | let excludeList = [ 'blacklist' ] | |
56 | let matched: string | |
57 | ||
58 | // if there is a valid, non-empty whitelist, we exclude all unknown adresses too | |
59 | if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) { | |
60 | excludeList.push('unknown') | |
61 | } | |
62 | ||
63 | if (addr.kind() === 'ipv4') { | |
64 | const addrV4 = ipaddr.IPv4.parse(ip) | |
65 | const rangeList = { | |
66 | whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr)) | |
67 | .map(cidr => ipaddr.IPv4.parseCIDR(cidr)), | |
68 | blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr)) | |
69 | .map(cidr => ipaddr.IPv4.parseCIDR(cidr)) | |
70 | } | |
71 | matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown') | |
72 | } else if (addr.kind() === 'ipv6') { | |
73 | const addrV6 = ipaddr.IPv6.parse(ip) | |
74 | const rangeList = { | |
75 | whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr)) | |
76 | .map(cidr => ipaddr.IPv6.parseCIDR(cidr)), | |
77 | blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr)) | |
78 | .map(cidr => ipaddr.IPv6.parseCIDR(cidr)) | |
79 | } | |
80 | matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown') | |
81 | } | |
82 | ||
83 | return !excludeList.includes(matched) | |
84 | } | |
85 | ||
40298b02 C |
86 | function computeResolutionsToTranscode (videoFileHeight: number) { |
87 | const resolutionsEnabled: number[] = [] | |
88 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
89 | ||
90 | const resolutions = [ | |
91 | VideoResolution.H_240P, | |
92 | VideoResolution.H_360P, | |
93 | VideoResolution.H_480P, | |
94 | VideoResolution.H_720P, | |
95 | VideoResolution.H_1080P | |
96 | ] | |
97 | ||
98 | for (const resolution of resolutions) { | |
fd206f0b | 99 | if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) { |
40298b02 C |
100 | resolutionsEnabled.push(resolution) |
101 | } | |
102 | } | |
103 | ||
104 | return resolutionsEnabled | |
105 | } | |
106 | ||
3fd3ab2d | 107 | function 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 |
114 | let serverActor: ActorModel |
115 | async 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 |
129 | type SortType = { sortModel: any, sortValue: string } |
130 | ||
9f10b292 | 131 | // --------------------------------------------------------------------------- |
c45f7f84 | 132 | |
65fcc311 | 133 | export { |
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 | } |