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