]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Fix announce activities
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
c5911fd3 2import * as multer from 'multer'
3fd3ab2d 3import { Model } from 'sequelize-typescript'
6fcd19ba 4import { ResultList } from '../../shared'
3fd3ab2d 5import { VideoResolution } from '../../shared/models/videos'
47564bbe 6import { CONFIG, REMOTE_SCHEME } from '../initializers'
3fd3ab2d 7import { UserModel } from '../models/account/user'
50d6de9c
C
8import { ActorModel } from '../models/activitypub/actor'
9import { ApplicationModel } from '../models/application/application'
8d468a16 10import { pseudoRandomBytesPromise } from './core-utils'
efc32059 11import { logger } from './logger'
cbe2f7c3 12
0405ab52
C
13function getHostWithPort (host: string) {
14 const splitted = host.split(':')
15
16 // The port was not specified
17 if (splitted.length === 1) {
18 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
19
20 return host + ':80'
21 }
22
23 return host
24}
25
69818c93 26function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
f5028693 27 return res.type('json').status(400).end()
a6fd2b30
C
28}
29
c5911fd3
C
30function createReqFiles (fieldName: string, storageDir: string, mimeTypes: { [ id: string ]: string }) {
31 const storage = multer.diskStorage({
32 destination: (req, file, cb) => {
33 cb(null, storageDir)
34 },
35
36 filename: async (req, file, cb) => {
37 const extension = mimeTypes[file.mimetype]
38 let randomString = ''
39
40 try {
41 randomString = await generateRandomString(16)
42 } catch (err) {
43 logger.error('Cannot generate random string for file name.', err)
44 randomString = 'fake-random-string'
45 }
46
47 cb(null, randomString + extension)
48 }
49 })
50
51 return multer({ storage }).fields([{ name: fieldName, maxCount: 1 }])
52}
53
f5028693
C
54async function generateRandomString (size: number) {
55 const raw = await pseudoRandomBytesPromise(size)
56
57 return raw.toString('hex')
e4c87ec2
C
58}
59
40298b02 60interface FormattableToJSON {
0aef76c4 61 toFormattedJSON ()
154898b0
C
62}
63
40298b02 64function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
0aef76c4 65 const formattedObjects: U[] = []
55fa55a9 66
075f16ca 67 objects.forEach(object => {
0aef76c4 68 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
69 })
70
6fcd19ba 71 const res: ResultList<U> = {
55fa55a9 72 total: objectsTotal,
0aef76c4 73 data: formattedObjects
55fa55a9 74 }
6fcd19ba
C
75
76 return res
55fa55a9
C
77}
78
f5028693 79async function isSignupAllowed () {
291e8d3e 80 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 81 return false
291e8d3e
C
82 }
83
84 // No limit and signup is enabled
85 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 86 return true
291e8d3e
C
87 }
88
3fd3ab2d 89 const totalUsers = await UserModel.countTotal()
f5028693
C
90
91 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
92}
93
40298b02
C
94function computeResolutionsToTranscode (videoFileHeight: number) {
95 const resolutionsEnabled: number[] = []
96 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
97
98 const resolutions = [
99 VideoResolution.H_240P,
100 VideoResolution.H_360P,
101 VideoResolution.H_480P,
102 VideoResolution.H_720P,
103 VideoResolution.H_1080P
104 ]
105
106 for (const resolution of resolutions) {
fd206f0b 107 if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) {
40298b02
C
108 resolutionsEnabled.push(resolution)
109 }
110 }
111
112 return resolutionsEnabled
113}
114
3fd3ab2d 115function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
eb080476
C
116 Object.keys(savedFields).forEach(key => {
117 const value = savedFields[key]
118 instance.set(key, value)
119 })
120}
121
50d6de9c
C
122let serverActor: ActorModel
123async function getServerActor () {
124 if (serverActor === undefined) {
125 const application = await ApplicationModel.load()
126 serverActor = application.Account.Actor
7a7724e6
C
127 }
128
50d6de9c
C
129 if (!serverActor) {
130 logger.error('Cannot load server actor.')
efc32059
C
131 process.exit(0)
132 }
133
50d6de9c 134 return Promise.resolve(serverActor)
7a7724e6
C
135}
136
792dbaf0
GS
137type SortType = { sortModel: any, sortValue: string }
138
9f10b292 139// ---------------------------------------------------------------------------
c45f7f84 140
65fcc311
C
141export {
142 badRequest,
65fcc311 143 generateRandomString,
0aef76c4 144 getFormattedObjects,
792dbaf0 145 isSignupAllowed,
40298b02 146 computeResolutionsToTranscode,
eb080476 147 resetSequelizeInstance,
50d6de9c 148 getServerActor,
0405ab52 149 SortType,
c5911fd3
C
150 getHostWithPort,
151 createReqFiles
65fcc311 152}