1 import * as express from 'express'
2 import * as Promise from 'bluebird'
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'
9 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
10 res.type('json').status(400).end()
13 function generateRandomString (size: number) {
14 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
17 interface FormattableToJSON {
21 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
22 const formattedObjects: U[] = []
24 objects.forEach(object => {
25 formattedObjects.push(object.toFormattedJSON())
28 const res: ResultList<U> = {
30 data: formattedObjects
36 function isSignupAllowed () {
37 if (CONFIG.SIGNUP.ENABLED === false) {
38 return Promise.resolve(false)
41 // No limit and signup is enabled
42 if (CONFIG.SIGNUP.LIMIT === -1) {
43 return Promise.resolve(true)
46 return db.User.countTotal().then(totalUsers => {
47 return totalUsers < CONFIG.SIGNUP.LIMIT
51 function computeResolutionsToTranscode (videoFileHeight: number) {
52 const resolutionsEnabled: number[] = []
53 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
56 VideoResolution.H_240P,
57 VideoResolution.H_360P,
58 VideoResolution.H_480P,
59 VideoResolution.H_720P,
60 VideoResolution.H_1080P
63 for (const resolution of resolutions) {
64 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
65 resolutionsEnabled.push(resolution)
69 return resolutionsEnabled
72 type SortType = { sortModel: any, sortValue: string }
74 // ---------------------------------------------------------------------------
81 computeResolutionsToTranscode,