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'
12 const isCidr = require('is-cidr')
14 async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size)
17 return raw.toString('hex')
20 interface FormattableToJSON {
21 toFormattedJSON (args?: any)
24 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
25 const formattedObjects: U[] = []
27 objects.forEach(object => {
28 formattedObjects.push(object.toFormattedJSON(formattedArg))
33 data: formattedObjects
37 async function isSignupAllowed () {
38 if (CONFIG.SIGNUP.ENABLED === false) {
42 // No limit and signup is enabled
43 if (CONFIG.SIGNUP.LIMIT === -1) {
47 const totalUsers = await UserModel.countTotal()
49 return totalUsers < CONFIG.SIGNUP.LIMIT
52 function isSignupAllowedForCurrentIP (ip: string) {
53 const addr = ipaddr.parse(ip)
54 let excludeList = [ 'blacklist' ]
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')
62 if (addr.kind() === 'ipv4') {
63 const addrV4 = ipaddr.IPv4.parse(ip)
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))
70 matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
71 } else if (addr.kind() === 'ipv6') {
72 const addrV6 = ipaddr.IPv6.parse(ip)
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))
79 matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
82 return !excludeList.includes(matched)
85 function computeResolutionsToTranscode (videoFileHeight: number) {
86 const resolutionsEnabled: number[] = []
87 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
89 // Put in the order we want to proceed jobs
91 VideoResolution.H_480P,
92 VideoResolution.H_360P,
93 VideoResolution.H_720P,
94 VideoResolution.H_240P,
95 VideoResolution.H_1080P
98 for (const resolution of resolutions) {
99 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
100 resolutionsEnabled.push(resolution)
104 return resolutionsEnabled
113 week: 3600000 * 24 * 7,
114 month: 3600000 * 24 * 30
116 export function parseDuration (duration: number | string): number {
117 if (typeof duration === 'number') return duration
119 if (typeof duration === 'string') {
120 const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
122 if (split.length === 3) {
123 const len = parseFloat(split[1])
124 let unit = split[2].replace(/s$/i,'').toLowerCase()
129 return (len || 1) * (timeTable[unit] || 0)
133 throw new Error('Duration could not be properly parsed')
136 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
137 Object.keys(savedFields).forEach(key => {
138 const value = savedFields[key]
139 instance.set(key, value)
143 let serverActor: ActorModel
144 async function getServerActor () {
145 if (serverActor === undefined) {
146 const application = await ApplicationModel.load()
147 serverActor = application.Account.Actor
151 logger.error('Cannot load server actor.')
155 return Promise.resolve(serverActor)
158 type SortType = { sortModel: any, sortValue: string }
160 // ---------------------------------------------------------------------------
163 generateRandomString,
166 isSignupAllowedForCurrentIP,
167 computeResolutionsToTranscode,
168 resetSequelizeInstance,