1 import * as express from 'express'
2 import * as Sequelize from 'sequelize'
4 import { pseudoRandomBytesPromise } from './core-utils'
5 import { CONFIG, database as db } from '../initializers'
6 import { ResultList } from '../../shared'
7 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
8 import { AccountInstance } from '../models/account/account-interface'
9 import { logger } from './logger'
11 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
12 return res.type('json').status(400).end()
15 async function generateRandomString (size: number) {
16 const raw = await pseudoRandomBytesPromise(size)
18 return raw.toString('hex')
21 interface FormattableToJSON {
25 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
26 const formattedObjects: U[] = []
28 objects.forEach(object => {
29 formattedObjects.push(object.toFormattedJSON())
32 const res: ResultList<U> = {
34 data: formattedObjects
40 async function isSignupAllowed () {
41 if (CONFIG.SIGNUP.ENABLED === false) {
45 // No limit and signup is enabled
46 if (CONFIG.SIGNUP.LIMIT === -1) {
50 const totalUsers = await db.User.countTotal()
52 return totalUsers < CONFIG.SIGNUP.LIMIT
55 function computeResolutionsToTranscode (videoFileHeight: number) {
56 const resolutionsEnabled: number[] = []
57 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
60 VideoResolution.H_240P,
61 VideoResolution.H_360P,
62 VideoResolution.H_480P,
63 VideoResolution.H_720P,
64 VideoResolution.H_1080P
67 for (const resolution of resolutions) {
68 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
69 resolutionsEnabled.push(resolution)
73 return resolutionsEnabled
76 function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
77 Object.keys(savedFields).forEach(key => {
78 const value = savedFields[key]
79 instance.set(key, value)
83 let serverAccount: AccountInstance
84 async function getServerAccount () {
85 if (serverAccount === undefined) {
86 serverAccount = await db.Account.loadApplication()
90 logger.error('Cannot load server account.')
94 return Promise.resolve(serverAccount)
97 type SortType = { sortModel: any, sortValue: string }
99 // ---------------------------------------------------------------------------
103 generateRandomString,
106 computeResolutionsToTranscode,
107 resetSequelizeInstance,