]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { Model } from 'sequelize-typescript' |
ff2c1fe8 | 2 | import * as ipaddr from 'ipaddr.js' |
6fcd19ba | 3 | import { ResultList } from '../../shared' |
3fd3ab2d | 4 | import { VideoResolution } from '../../shared/models/videos' |
0626e7af | 5 | import { CONFIG } from '../initializers' |
3fd3ab2d | 6 | import { UserModel } from '../models/account/user' |
50d6de9c C |
7 | import { ActorModel } from '../models/activitypub/actor' |
8 | import { ApplicationModel } from '../models/application/application' | |
8d468a16 | 9 | import { pseudoRandomBytesPromise } from './core-utils' |
efc32059 | 10 | import { logger } from './logger' |
cbe2f7c3 | 11 | |
2186386c C |
12 | const isCidr = require('is-cidr') |
13 | ||
f5028693 C |
14 | async function generateRandomString (size: number) { |
15 | const raw = await pseudoRandomBytesPromise(size) | |
16 | ||
17 | return raw.toString('hex') | |
e4c87ec2 C |
18 | } |
19 | ||
40298b02 | 20 | interface FormattableToJSON { |
2186386c | 21 | toFormattedJSON (args?: any) |
154898b0 C |
22 | } |
23 | ||
2186386c | 24 | function 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 | 37 | async 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 |
52 | function isSignupAllowedForCurrentIP (ip: string) { |
53 | const addr = ipaddr.parse(ip) | |
54 | let excludeList = [ 'blacklist' ] | |
c1e791ba | 55 | let matched = '' |
ff2c1fe8 RK |
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 |
85 | function 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 |
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 | } | |
98d3324d | 116 | export 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 | 136 | function 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 |
143 | let serverActor: ActorModel |
144 | async function getServerActor () { | |
145 | if (serverActor === undefined) { | |
146 | const application = await ApplicationModel.load() | |
c1e791ba | 147 | if (!application) throw Error('Could not application.') |
50d6de9c | 148 | serverActor = application.Account.Actor |
7a7724e6 C |
149 | } |
150 | ||
50d6de9c C |
151 | if (!serverActor) { |
152 | logger.error('Cannot load server actor.') | |
efc32059 C |
153 | process.exit(0) |
154 | } | |
155 | ||
50d6de9c | 156 | return Promise.resolve(serverActor) |
7a7724e6 C |
157 | } |
158 | ||
792dbaf0 GS |
159 | type SortType = { sortModel: any, sortValue: string } |
160 | ||
9f10b292 | 161 | // --------------------------------------------------------------------------- |
c45f7f84 | 162 | |
65fcc311 | 163 | export { |
65fcc311 | 164 | generateRandomString, |
0aef76c4 | 165 | getFormattedObjects, |
792dbaf0 | 166 | isSignupAllowed, |
ff2c1fe8 | 167 | isSignupAllowedForCurrentIP, |
40298b02 | 168 | computeResolutionsToTranscode, |
eb080476 | 169 | resetSequelizeInstance, |
50d6de9c | 170 | getServerActor, |
0626e7af | 171 | SortType |
65fcc311 | 172 | } |