]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
Implement video transcoding on server side
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
1 import * as express from 'express'
2 import * as Promise from 'bluebird'
3
4 import { pseudoRandomBytesPromise } from './core-utils'
5 import { CONFIG, database as db } from '../initializers'
6 import { ResultList } from '../../shared'
7 import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
8
9 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
10 res.type('json').status(400).end()
11 }
12
13 function generateRandomString (size: number) {
14 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
15 }
16
17 interface FormattableToJSON {
18 toFormattedJSON ()
19 }
20
21 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
22 const formattedObjects: U[] = []
23
24 objects.forEach(object => {
25 formattedObjects.push(object.toFormattedJSON())
26 })
27
28 const res: ResultList<U> = {
29 total: objectsTotal,
30 data: formattedObjects
31 }
32
33 return res
34 }
35
36 function 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
51 function 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) {
64 if (configResolutions[resolution.toString()] === true && videoFileHeight >= resolution) {
65 resolutionsEnabled.push(resolution)
66 }
67 }
68
69 return resolutionsEnabled
70 }
71
72 type SortType = { sortModel: any, sortValue: string }
73
74 // ---------------------------------------------------------------------------
75
76 export {
77 badRequest,
78 generateRandomString,
79 getFormattedObjects,
80 isSignupAllowed,
81 computeResolutionsToTranscode,
82 SortType
83 }