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'
9 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
10 return res.type('json').status(400).end()
13 async function generateRandomString (size: number) {
14 const raw = await pseudoRandomBytesPromise(size)
16 return raw.toString('hex')
19 interface FormattableToJSON {
23 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
24 const formattedObjects: U[] = []
26 objects.forEach(object => {
27 formattedObjects.push(object.toFormattedJSON())
30 const res: ResultList<U> = {
32 data: formattedObjects
38 async function isSignupAllowed () {
39 if (CONFIG.SIGNUP.ENABLED === false) {
43 // No limit and signup is enabled
44 if (CONFIG.SIGNUP.LIMIT === -1) {
48 const totalUsers = await db.User.countTotal()
50 return totalUsers < CONFIG.SIGNUP.LIMIT
53 function computeResolutionsToTranscode (videoFileHeight: number) {
54 const resolutionsEnabled: number[] = []
55 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
58 VideoResolution.H_240P,
59 VideoResolution.H_360P,
60 VideoResolution.H_480P,
61 VideoResolution.H_720P,
62 VideoResolution.H_1080P
65 for (const resolution of resolutions) {
66 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
67 resolutionsEnabled.push(resolution)
71 return resolutionsEnabled
74 function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
75 Object.keys(savedFields).forEach(key => {
76 const value = savedFields[key]
77 instance.set(key, value)
81 type SortType = { sortModel: any, sortValue: string }
83 // ---------------------------------------------------------------------------
90 computeResolutionsToTranscode,
91 resetSequelizeInstance,