1 import * as express from 'express'
2 import * as Sequelize from 'sequelize'
3 import * as Promise from 'bluebird'
5 import { pseudoRandomBytesPromise } from './core-utils'
6 import { CONFIG, database as db } from '../initializers'
7 import { ResultList } from '../../shared'
8 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
10 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
11 return res.type('json').status(400).end()
14 async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size)
17 return raw.toString('hex')
20 interface FormattableToJSON {
24 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
25 const formattedObjects: U[] = []
27 objects.forEach(object => {
28 formattedObjects.push(object.toFormattedJSON())
31 const res: ResultList<U> = {
33 data: formattedObjects
39 async function isSignupAllowed () {
40 if (CONFIG.SIGNUP.ENABLED === false) {
44 // No limit and signup is enabled
45 if (CONFIG.SIGNUP.LIMIT === -1) {
49 const totalUsers = await db.User.countTotal()
51 return totalUsers < CONFIG.SIGNUP.LIMIT
54 function computeResolutionsToTranscode (videoFileHeight: number) {
55 const resolutionsEnabled: number[] = []
56 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
59 VideoResolution.H_240P,
60 VideoResolution.H_360P,
61 VideoResolution.H_480P,
62 VideoResolution.H_720P,
63 VideoResolution.H_1080P
66 for (const resolution of resolutions) {
67 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
68 resolutionsEnabled.push(resolution)
72 return resolutionsEnabled
75 function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
76 Object.keys(savedFields).forEach(key => {
77 const value = savedFields[key]
78 instance.set(key, value)
82 type SortType = { sortModel: any, sortValue: string }
84 // ---------------------------------------------------------------------------
91 computeResolutionsToTranscode,
92 resetSequelizeInstance,