]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
Video blacklist refractoring
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
69818c93 1import * as express from 'express'
291e8d3e 2import * as Promise from 'bluebird'
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) {
a6fd2b30
C
10 res.type('json').status(400).end()
11}
12
6fcd19ba
C
13function generateRandomString (size: number) {
14 return pseudoRandomBytesPromise(size).then(raw => 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
291e8d3e
C
36function isSignupAllowed () {
37 if (CONFIG.SIGNUP.ENABLED === false) {
38 return Promise.resolve(false)
39 }
40
41 // No limit and signup is enabled
42 if (CONFIG.SIGNUP.LIMIT === -1) {
43 return Promise.resolve(true)
44 }
45
46 return db.User.countTotal().then(totalUsers => {
47 return totalUsers < CONFIG.SIGNUP.LIMIT
48 })
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) {
14d3270f 64 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
40298b02
C
65 resolutionsEnabled.push(resolution)
66 }
67 }
68
69 return resolutionsEnabled
70}
71
792dbaf0
GS
72type SortType = { sortModel: any, sortValue: string }
73
9f10b292 74// ---------------------------------------------------------------------------
c45f7f84 75
65fcc311
C
76export {
77 badRequest,
65fcc311 78 generateRandomString,
0aef76c4 79 getFormattedObjects,
792dbaf0 80 isSignupAllowed,
40298b02 81 computeResolutionsToTranscode,
792dbaf0 82 SortType
65fcc311 83}