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