X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Futils.ts;h=3af14a68a51182ba16d81564d8ec2ffdc94d175a;hb=efc32059d980c51793e8e9ac0fb6a885a8026f94;hp=f326210f32886fd5136ee1511820cffeaed5bdd3;hpb=291e8d3eed88fe714fb74ad897ac2c67347a85ff;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index f326210f3..3af14a68a 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -1,57 +1,110 @@ import * as express from 'express' -import * as Promise from 'bluebird' +import * as Sequelize from 'sequelize' import { pseudoRandomBytesPromise } from './core-utils' import { CONFIG, database as db } from '../initializers' import { ResultList } from '../../shared' +import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' +import { AccountInstance } from '../models/account/account-interface' +import { logger } from './logger' function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { - res.type('json').status(400).end() + return res.type('json').status(400).end() } -function generateRandomString (size: number) { - return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex')) +async function generateRandomString (size: number) { + const raw = await pseudoRandomBytesPromise(size) + + return raw.toString('hex') } -interface FormatableToJSON { - toFormatedJSON () +interface FormattableToJSON { + toFormattedJSON () } -function getFormatedObjects (objects: T[], objectsTotal: number) { - const formatedObjects: U[] = [] +function getFormattedObjects (objects: T[], objectsTotal: number) { + const formattedObjects: U[] = [] objects.forEach(object => { - formatedObjects.push(object.toFormatedJSON()) + formattedObjects.push(object.toFormattedJSON()) }) const res: ResultList = { total: objectsTotal, - data: formatedObjects + data: formattedObjects } return res } -function isSignupAllowed () { +async function isSignupAllowed () { if (CONFIG.SIGNUP.ENABLED === false) { - return Promise.resolve(false) + return false } // No limit and signup is enabled if (CONFIG.SIGNUP.LIMIT === -1) { - return Promise.resolve(true) + return true } - return db.User.countTotal().then(totalUsers => { - return totalUsers < CONFIG.SIGNUP.LIMIT + const totalUsers = await db.User.countTotal() + + return totalUsers < CONFIG.SIGNUP.LIMIT +} + +function computeResolutionsToTranscode (videoFileHeight: number) { + const resolutionsEnabled: number[] = [] + const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS + + const resolutions = [ + VideoResolution.H_240P, + VideoResolution.H_360P, + VideoResolution.H_480P, + VideoResolution.H_720P, + VideoResolution.H_1080P + ] + + for (const resolution of resolutions) { + if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) { + resolutionsEnabled.push(resolution) + } + } + + return resolutionsEnabled +} + +function resetSequelizeInstance (instance: Sequelize.Instance, savedFields: object) { + Object.keys(savedFields).forEach(key => { + const value = savedFields[key] + instance.set(key, value) }) } +let serverAccount: AccountInstance +async function getServerAccount () { + if (serverAccount === undefined) { + serverAccount = await db.Account.loadApplication() + } + + if (!serverAccount) { + logger.error('Cannot load server account.') + process.exit(0) + } + + return Promise.resolve(serverAccount) +} + +type SortType = { sortModel: any, sortValue: string } + // --------------------------------------------------------------------------- export { badRequest, generateRandomString, - getFormatedObjects, - isSignupAllowed + getFormattedObjects, + isSignupAllowed, + computeResolutionsToTranscode, + resetSequelizeInstance, + getServerAccount, + SortType }