]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import * as Sequelize from 'sequelize' | |
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 | import { AccountInstance } from '../models/account/account-interface' | |
9 | import { logger } from './logger' | |
10 | ||
11 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | |
12 | return res.type('json').status(400).end() | |
13 | } | |
14 | ||
15 | async function generateRandomString (size: number) { | |
16 | const raw = await pseudoRandomBytesPromise(size) | |
17 | ||
18 | return raw.toString('hex') | |
19 | } | |
20 | ||
21 | interface FormattableToJSON { | |
22 | toFormattedJSON () | |
23 | } | |
24 | ||
25 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { | |
26 | const formattedObjects: U[] = [] | |
27 | ||
28 | objects.forEach(object => { | |
29 | formattedObjects.push(object.toFormattedJSON()) | |
30 | }) | |
31 | ||
32 | const res: ResultList<U> = { | |
33 | total: objectsTotal, | |
34 | data: formattedObjects | |
35 | } | |
36 | ||
37 | return res | |
38 | } | |
39 | ||
40 | async function isSignupAllowed () { | |
41 | if (CONFIG.SIGNUP.ENABLED === false) { | |
42 | return false | |
43 | } | |
44 | ||
45 | // No limit and signup is enabled | |
46 | if (CONFIG.SIGNUP.LIMIT === -1) { | |
47 | return true | |
48 | } | |
49 | ||
50 | const totalUsers = await db.User.countTotal() | |
51 | ||
52 | return totalUsers < CONFIG.SIGNUP.LIMIT | |
53 | } | |
54 | ||
55 | function computeResolutionsToTranscode (videoFileHeight: number) { | |
56 | const resolutionsEnabled: number[] = [] | |
57 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
58 | ||
59 | const resolutions = [ | |
60 | VideoResolution.H_240P, | |
61 | VideoResolution.H_360P, | |
62 | VideoResolution.H_480P, | |
63 | VideoResolution.H_720P, | |
64 | VideoResolution.H_1080P | |
65 | ] | |
66 | ||
67 | for (const resolution of resolutions) { | |
68 | if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) { | |
69 | resolutionsEnabled.push(resolution) | |
70 | } | |
71 | } | |
72 | ||
73 | return resolutionsEnabled | |
74 | } | |
75 | ||
76 | function resetSequelizeInstance (instance: Sequelize.Instance<any>, savedFields: object) { | |
77 | Object.keys(savedFields).forEach(key => { | |
78 | const value = savedFields[key] | |
79 | instance.set(key, value) | |
80 | }) | |
81 | } | |
82 | ||
83 | let serverAccount: AccountInstance | |
84 | async function getServerAccount () { | |
85 | if (serverAccount === undefined) { | |
86 | serverAccount = await db.Account.loadApplication() | |
87 | } | |
88 | ||
89 | if (!serverAccount) { | |
90 | logger.error('Cannot load server account.') | |
91 | process.exit(0) | |
92 | } | |
93 | ||
94 | return Promise.resolve(serverAccount) | |
95 | } | |
96 | ||
97 | type SortType = { sortModel: any, sortValue: string } | |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
101 | export { | |
102 | badRequest, | |
103 | generateRandomString, | |
104 | getFormattedObjects, | |
105 | isSignupAllowed, | |
106 | computeResolutionsToTranscode, | |
107 | resetSequelizeInstance, | |
108 | getServerAccount, | |
109 | SortType | |
110 | } |