]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/utils.ts
Add ability to remove a video on watch page
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { Model } from 'sequelize-typescript'
3import { ResultList } from '../../shared'
4import { VideoResolution } from '../../shared/models/videos'
5import { CONFIG, REMOTE_SCHEME } from '../initializers'
6import { UserModel } from '../models/account/user'
7import { ActorModel } from '../models/activitypub/actor'
8import { ApplicationModel } from '../models/application/application'
9import { pseudoRandomBytesPromise } from './core-utils'
10import { logger } from './logger'
11
12function 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
25function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
26 return res.type('json').status(400).end()
27}
28
29async function generateRandomString (size: number) {
30 const raw = await pseudoRandomBytesPromise(size)
31
32 return raw.toString('hex')
33}
34
35interface FormattableToJSON {
36 toFormattedJSON ()
37}
38
39function 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
54async 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
69function 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
90function 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
97let serverActor: ActorModel
98async 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
112type SortType = { sortModel: any, sortValue: string }
113
114// ---------------------------------------------------------------------------
115
116export {
117 badRequest,
118 generateRandomString,
119 getFormattedObjects,
120 isSignupAllowed,
121 computeResolutionsToTranscode,
122 resetSequelizeInstance,
123 getServerActor,
124 SortType,
125 getHostWithPort
126}