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