]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
Cleanup helpers
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
1 import * as express from 'express'
2 import * as Sequelize from 'sequelize'
3 import { ResultList } from '../../shared'
4 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
5 import { CONFIG, database as db } from '../initializers'
6 import { AccountInstance } from '../models/account/account-interface'
7 import { pseudoRandomBytesPromise } from './core-utils'
8 import { logger } from './logger'
9
10 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
11 return res.type('json').status(400).end()
12 }
13
14 async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size)
16
17 return raw.toString('hex')
18 }
19
20 interface FormattableToJSON {
21 toFormattedJSON ()
22 }
23
24 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
25 const formattedObjects: U[] = []
26
27 objects.forEach(object => {
28 formattedObjects.push(object.toFormattedJSON())
29 })
30
31 const res: ResultList<U> = {
32 total: objectsTotal,
33 data: formattedObjects
34 }
35
36 return res
37 }
38
39 async function isSignupAllowed () {
40 if (CONFIG.SIGNUP.ENABLED === false) {
41 return false
42 }
43
44 // No limit and signup is enabled
45 if (CONFIG.SIGNUP.LIMIT === -1) {
46 return true
47 }
48
49 const totalUsers = await db.User.countTotal()
50
51 return totalUsers < CONFIG.SIGNUP.LIMIT
52 }
53
54 function computeResolutionsToTranscode (videoFileHeight: number) {
55 const resolutionsEnabled: number[] = []
56 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
57
58 const resolutions = [
59 VideoResolution.H_240P,
60 VideoResolution.H_360P,
61 VideoResolution.H_480P,
62 VideoResolution.H_720P,
63 VideoResolution.H_1080P
64 ]
65
66 for (const resolution of resolutions) {
67 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
68 resolutionsEnabled.push(resolution)
69 }
70 }
71
72 return resolutionsEnabled
73 }
74
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)
79 })
80 }
81
82 let serverAccount: AccountInstance
83 async function getServerAccount () {
84 if (serverAccount === undefined) {
85 serverAccount = await db.Account.loadApplication()
86 }
87
88 if (!serverAccount) {
89 logger.error('Cannot load server account.')
90 process.exit(0)
91 }
92
93 return Promise.resolve(serverAccount)
94 }
95
96 type SortType = { sortModel: any, sortValue: string }
97
98 // ---------------------------------------------------------------------------
99
100 export {
101 badRequest,
102 generateRandomString,
103 getFormattedObjects,
104 isSignupAllowed,
105 computeResolutionsToTranscode,
106 resetSequelizeInstance,
107 getServerAccount,
108 SortType
109 }