]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Add follow tests
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
eb080476 2import * as Sequelize from 'sequelize'
69818c93 3
6fcd19ba 4import { pseudoRandomBytesPromise } from './core-utils'
291e8d3e 5import { CONFIG, database as db } from '../initializers'
6fcd19ba 6import { ResultList } from '../../shared'
40298b02 7import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
7a7724e6 8import { AccountInstance } from '../models/account/account-interface'
efc32059 9import { logger } from './logger'
cbe2f7c3 10
69818c93 11function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
f5028693 12 return res.type('json').status(400).end()
a6fd2b30
C
13}
14
f5028693
C
15async function generateRandomString (size: number) {
16 const raw = await pseudoRandomBytesPromise(size)
17
18 return raw.toString('hex')
e4c87ec2
C
19}
20
40298b02 21interface FormattableToJSON {
0aef76c4 22 toFormattedJSON ()
154898b0
C
23}
24
40298b02 25function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
0aef76c4 26 const formattedObjects: U[] = []
55fa55a9 27
075f16ca 28 objects.forEach(object => {
0aef76c4 29 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
30 })
31
6fcd19ba 32 const res: ResultList<U> = {
55fa55a9 33 total: objectsTotal,
0aef76c4 34 data: formattedObjects
55fa55a9 35 }
6fcd19ba
C
36
37 return res
55fa55a9
C
38}
39
f5028693 40async function isSignupAllowed () {
291e8d3e 41 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 42 return false
291e8d3e
C
43 }
44
45 // No limit and signup is enabled
46 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 47 return true
291e8d3e
C
48 }
49
f5028693
C
50 const totalUsers = await db.User.countTotal()
51
52 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
53}
54
40298b02
C
55function 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) {
14d3270f 68 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
40298b02
C
69 resolutionsEnabled.push(resolution)
70 }
71 }
72
73 return resolutionsEnabled
74}
75
eb080476
C
76function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
77 Object.keys(savedFields).forEach(key => {
78 const value = savedFields[key]
79 instance.set(key, value)
80 })
81}
82
efc32059
C
83let serverAccount: AccountInstance
84async function getServerAccount () {
85 if (serverAccount === undefined) {
86 serverAccount = await db.Account.loadApplication()
7a7724e6
C
87 }
88
efc32059
C
89 if (!serverAccount) {
90 logger.error('Cannot load server account.')
91 process.exit(0)
92 }
93
94 return Promise.resolve(serverAccount)
7a7724e6
C
95}
96
792dbaf0
GS
97type SortType = { sortModel: any, sortValue: string }
98
9f10b292 99// ---------------------------------------------------------------------------
c45f7f84 100
65fcc311
C
101export {
102 badRequest,
65fcc311 103 generateRandomString,
0aef76c4 104 getFormattedObjects,
792dbaf0 105 isSignupAllowed,
40298b02 106 computeResolutionsToTranscode,
eb080476 107 resetSequelizeInstance,
efc32059 108 getServerAccount,
792dbaf0 109 SortType
65fcc311 110}