X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Futils.ts;h=3af14a68a51182ba16d81564d8ec2ffdc94d175a;hb=0f91ae62df8a37194fea84ce1efa9e733d9c1fd8;hp=5b8d21f70d1cdfe0d9db79f7d625fae730184903;hpb=154898b0b7bc1af41fc5a94974e338a3590c90f3;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 5b8d21f70..3af14a68a 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -1,49 +1,110 @@ import * as express from 'express' +import * as Sequelize from 'sequelize' -import { pseudoRandomBytes } from 'crypto' - +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, callback: (err: Error, randomString?: string) => void) { - pseudoRandomBytes(size, function (err, raw) { - if (err) return callback(err) +async function generateRandomString (size: number) { + const raw = await pseudoRandomBytesPromise(size) - callback(null, raw.toString('hex')) - }) + return raw.toString('hex') +} + +interface FormattableToJSON { + toFormattedJSON () } -function createEmptyCallback () { - return function (err) { - if (err) logger.error('Error in empty callback.', { error: err }) +function getFormattedObjects (objects: T[], objectsTotal: number) { + const formattedObjects: U[] = [] + + objects.forEach(object => { + formattedObjects.push(object.toFormattedJSON()) + }) + + const res: ResultList = { + total: objectsTotal, + data: formattedObjects } + + return res } -interface FormatableToJSON { - toFormatedJSON() +async function isSignupAllowed () { + if (CONFIG.SIGNUP.ENABLED === false) { + return false + } + + // No limit and signup is enabled + if (CONFIG.SIGNUP.LIMIT === -1) { + return true + } + + const totalUsers = await db.User.countTotal() + + return totalUsers < CONFIG.SIGNUP.LIMIT } -function getFormatedObjects (objects: T[], objectsTotal: number) { - const formatedObjects: U[] = [] +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 +} - objects.forEach(function (object) { - formatedObjects.push(object.toFormatedJSON()) +function resetSequelizeInstance (instance: Sequelize.Instance, savedFields: object) { + Object.keys(savedFields).forEach(key => { + const value = savedFields[key] + instance.set(key, value) }) +} - return { - total: objectsTotal, - data: formatedObjects +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, - createEmptyCallback, generateRandomString, - getFormatedObjects + getFormattedObjects, + isSignupAllowed, + computeResolutionsToTranscode, + resetSequelizeInstance, + getServerAccount, + SortType }