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