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 } 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 badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
13 return res.type('json').status(400).end()
16 async function generateRandomString (size: number) {
17 const raw = await pseudoRandomBytesPromise(size)
19 return raw.toString('hex')
22 interface FormattableToJSON {
26 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
27 const formattedObjects: U[] = []
29 objects.forEach(object => {
30 formattedObjects.push(object.toFormattedJSON())
33 const res: ResultList<U> = {
35 data: formattedObjects
41 async function isSignupAllowed () {
42 if (CONFIG.SIGNUP.ENABLED === false) {
46 // No limit and signup is enabled
47 if (CONFIG.SIGNUP.LIMIT === -1) {
51 const totalUsers = await UserModel.countTotal()
53 return totalUsers < CONFIG.SIGNUP.LIMIT
56 function computeResolutionsToTranscode (videoFileHeight: number) {
57 const resolutionsEnabled: number[] = []
58 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
61 VideoResolution.H_240P,
62 VideoResolution.H_360P,
63 VideoResolution.H_480P,
64 VideoResolution.H_720P,
65 VideoResolution.H_1080P
68 for (const resolution of resolutions) {
69 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
70 resolutionsEnabled.push(resolution)
74 return resolutionsEnabled
77 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
78 Object.keys(savedFields).forEach(key => {
79 const value = savedFields[key]
80 instance.set(key, value)
84 let serverActor: ActorModel
85 async function getServerActor () {
86 if (serverActor === undefined) {
87 const application = await ApplicationModel.load()
88 serverActor = application.Account.Actor
92 logger.error('Cannot load server actor.')
96 return Promise.resolve(serverActor)
99 type SortType = { sortModel: any, sortValue: string }
101 // ---------------------------------------------------------------------------
105 generateRandomString,
108 computeResolutionsToTranscode,
109 resetSequelizeInstance,