]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
769aa83c6a0b94f19b2956bc702e4f3d1852f213
[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, REMOTE_SCHEME } from '../initializers'
6 import { UserModel } from '../models/account/user'
7 import { ActorModel } from '../models/activitypub/actor'
8 import { ApplicationModel } from '../models/application/application'
9 import { pseudoRandomBytesPromise } from './core-utils'
10 import { logger } from './logger'
11
12 function getHostWithPort (host: string) {
13 const splitted = host.split(':')
14
15 // The port was not specified
16 if (splitted.length === 1) {
17 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
18
19 return host + ':80'
20 }
21
22 return host
23 }
24
25 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
26 return res.type('json').status(400).end()
27 }
28
29 async function generateRandomString (size: number) {
30 const raw = await pseudoRandomBytesPromise(size)
31
32 return raw.toString('hex')
33 }
34
35 interface FormattableToJSON {
36 toFormattedJSON ()
37 }
38
39 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
40 const formattedObjects: U[] = []
41
42 objects.forEach(object => {
43 formattedObjects.push(object.toFormattedJSON())
44 })
45
46 const res: ResultList<U> = {
47 total: objectsTotal,
48 data: formattedObjects
49 }
50
51 return res
52 }
53
54 async function isSignupAllowed () {
55 if (CONFIG.SIGNUP.ENABLED === false) {
56 return false
57 }
58
59 // No limit and signup is enabled
60 if (CONFIG.SIGNUP.LIMIT === -1) {
61 return true
62 }
63
64 const totalUsers = await UserModel.countTotal()
65
66 return totalUsers < CONFIG.SIGNUP.LIMIT
67 }
68
69 function computeResolutionsToTranscode (videoFileHeight: number) {
70 const resolutionsEnabled: number[] = []
71 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
72
73 const resolutions = [
74 VideoResolution.H_240P,
75 VideoResolution.H_360P,
76 VideoResolution.H_480P,
77 VideoResolution.H_720P,
78 VideoResolution.H_1080P
79 ]
80
81 for (const resolution of resolutions) {
82 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
83 resolutionsEnabled.push(resolution)
84 }
85 }
86
87 return resolutionsEnabled
88 }
89
90 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
91 Object.keys(savedFields).forEach(key => {
92 const value = savedFields[key]
93 instance.set(key, value)
94 })
95 }
96
97 let serverActor: ActorModel
98 async function getServerActor () {
99 if (serverActor === undefined) {
100 const application = await ApplicationModel.load()
101 serverActor = application.Account.Actor
102 }
103
104 if (!serverActor) {
105 logger.error('Cannot load server actor.')
106 process.exit(0)
107 }
108
109 return Promise.resolve(serverActor)
110 }
111
112 type SortType = { sortModel: any, sortValue: string }
113
114 // ---------------------------------------------------------------------------
115
116 export {
117 badRequest,
118 generateRandomString,
119 getFormattedObjects,
120 isSignupAllowed,
121 computeResolutionsToTranscode,
122 resetSequelizeInstance,
123 getServerActor,
124 SortType,
125 getHostWithPort
126 }