1 import * as express from 'express'
2 import { Model } from 'sequelize-typescript'
3 import { ResultList } from '../../shared'
4 import { VideoResolution } from '../../shared/models/videos'
5 import { CONFIG, REMOTE_SCHEME } 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 function getHostWithPort (host: string) {
13 const splitted = host.split(':')
15 // The port was not specified
16 if (splitted.length === 1) {
17 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
25 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
26 return res.type('json').status(400).end()
29 async function generateRandomString (size: number) {
30 const raw = await pseudoRandomBytesPromise(size)
32 return raw.toString('hex')
35 interface FormattableToJSON {
39 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
40 const formattedObjects: U[] = []
42 objects.forEach(object => {
43 formattedObjects.push(object.toFormattedJSON())
46 const res: ResultList<U> = {
48 data: formattedObjects
54 async function isSignupAllowed () {
55 if (CONFIG.SIGNUP.ENABLED === false) {
59 // No limit and signup is enabled
60 if (CONFIG.SIGNUP.LIMIT === -1) {
64 const totalUsers = await UserModel.countTotal()
66 return totalUsers < CONFIG.SIGNUP.LIMIT
69 function computeResolutionsToTranscode (videoFileHeight: number) {
70 const resolutionsEnabled: number[] = []
71 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
74 VideoResolution.H_240P,
75 VideoResolution.H_360P,
76 VideoResolution.H_480P,
77 VideoResolution.H_720P,
78 VideoResolution.H_1080P
81 for (const resolution of resolutions) {
82 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
83 resolutionsEnabled.push(resolution)
87 return resolutionsEnabled
90 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
91 Object.keys(savedFields).forEach(key => {
92 const value = savedFields[key]
93 instance.set(key, value)
97 let serverActor: ActorModel
98 async function getServerActor () {
99 if (serverActor === undefined) {
100 const application = await ApplicationModel.load()
101 serverActor = application.Account.Actor
105 logger.error('Cannot load server actor.')
109 return Promise.resolve(serverActor)
112 type SortType = { sortModel: any, sortValue: string }
114 // ---------------------------------------------------------------------------
118 generateRandomString,
121 computeResolutionsToTranscode,
122 resetSequelizeInstance,