]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Make it compile at least
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
eb080476 2import * as Sequelize from 'sequelize'
69818c93 3
6fcd19ba 4import { pseudoRandomBytesPromise } from './core-utils'
291e8d3e 5import { CONFIG, database as db } from '../initializers'
6fcd19ba 6import { ResultList } from '../../shared'
40298b02 7import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
cbe2f7c3 8
69818c93 9function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
f5028693 10 return res.type('json').status(400).end()
a6fd2b30
C
11}
12
f5028693
C
13async function generateRandomString (size: number) {
14 const raw = await pseudoRandomBytesPromise(size)
15
16 return raw.toString('hex')
e4c87ec2
C
17}
18
40298b02 19interface FormattableToJSON {
0aef76c4 20 toFormattedJSON ()
154898b0
C
21}
22
40298b02 23function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
0aef76c4 24 const formattedObjects: U[] = []
55fa55a9 25
075f16ca 26 objects.forEach(object => {
0aef76c4 27 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
28 })
29
6fcd19ba 30 const res: ResultList<U> = {
55fa55a9 31 total: objectsTotal,
0aef76c4 32 data: formattedObjects
55fa55a9 33 }
6fcd19ba
C
34
35 return res
55fa55a9
C
36}
37
f5028693 38async function isSignupAllowed () {
291e8d3e 39 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 40 return false
291e8d3e
C
41 }
42
43 // No limit and signup is enabled
44 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 45 return true
291e8d3e
C
46 }
47
f5028693
C
48 const totalUsers = await db.User.countTotal()
49
50 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
51}
52
40298b02
C
53function computeResolutionsToTranscode (videoFileHeight: number) {
54 const resolutionsEnabled: number[] = []
55 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
56
57 const resolutions = [
58 VideoResolution.H_240P,
59 VideoResolution.H_360P,
60 VideoResolution.H_480P,
61 VideoResolution.H_720P,
62 VideoResolution.H_1080P
63 ]
64
65 for (const resolution of resolutions) {
14d3270f 66 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
40298b02
C
67 resolutionsEnabled.push(resolution)
68 }
69 }
70
71 return resolutionsEnabled
72}
73
eb080476
C
74function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) {
75 Object.keys(savedFields).forEach(key => {
76 const value = savedFields[key]
77 instance.set(key, value)
78 })
79}
80
792dbaf0
GS
81type SortType = { sortModel: any, sortValue: string }
82
9f10b292 83// ---------------------------------------------------------------------------
c45f7f84 84
65fcc311
C
85export {
86 badRequest,
65fcc311 87 generateRandomString,
0aef76c4 88 getFormattedObjects,
792dbaf0 89 isSignupAllowed,
40298b02 90 computeResolutionsToTranscode,
eb080476 91 resetSequelizeInstance,
792dbaf0 92 SortType
65fcc311 93}