]>
Commit | Line | Data |
---|---|---|
69818c93 | 1 | import * as express from 'express' |
291e8d3e | 2 | import * as Promise from 'bluebird' |
69818c93 | 3 | |
6fcd19ba | 4 | import { pseudoRandomBytesPromise } from './core-utils' |
291e8d3e | 5 | import { CONFIG, database as db } from '../initializers' |
6fcd19ba | 6 | import { ResultList } from '../../shared' |
40298b02 | 7 | import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' |
cbe2f7c3 | 8 | |
69818c93 | 9 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { |
a6fd2b30 C |
10 | res.type('json').status(400).end() |
11 | } | |
12 | ||
6fcd19ba C |
13 | function generateRandomString (size: number) { |
14 | return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex')) | |
e4c87ec2 C |
15 | } |
16 | ||
40298b02 | 17 | interface FormattableToJSON { |
0aef76c4 | 18 | toFormattedJSON () |
154898b0 C |
19 | } |
20 | ||
40298b02 | 21 | function 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 |
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 | ||
40298b02 C |
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) { | |
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 |
72 | type SortType = { sortModel: any, sortValue: string } |
73 | ||
9f10b292 | 74 | // --------------------------------------------------------------------------- |
c45f7f84 | 75 | |
65fcc311 C |
76 | export { |
77 | badRequest, | |
65fcc311 | 78 | generateRandomString, |
0aef76c4 | 79 | getFormattedObjects, |
792dbaf0 | 80 | isSignupAllowed, |
40298b02 | 81 | computeResolutionsToTranscode, |
792dbaf0 | 82 | SortType |
65fcc311 | 83 | } |