]>
Commit | Line | Data |
---|---|---|
69818c93 | 1 | import * as express from 'express' |
3fd3ab2d | 2 | import { Model } from 'sequelize-typescript' |
6fcd19ba | 3 | import { ResultList } from '../../shared' |
3fd3ab2d C |
4 | import { VideoResolution } from '../../shared/models/videos' |
5 | import { CONFIG } from '../initializers' | |
6 | import { AccountModel } from '../models/account/account' | |
7 | import { UserModel } from '../models/account/user' | |
8d468a16 | 8 | import { pseudoRandomBytesPromise } from './core-utils' |
efc32059 | 9 | import { logger } from './logger' |
cbe2f7c3 | 10 | |
69818c93 | 11 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { |
f5028693 | 12 | return res.type('json').status(400).end() |
a6fd2b30 C |
13 | } |
14 | ||
f5028693 C |
15 | async function generateRandomString (size: number) { |
16 | const raw = await pseudoRandomBytesPromise(size) | |
17 | ||
18 | return raw.toString('hex') | |
e4c87ec2 C |
19 | } |
20 | ||
40298b02 | 21 | interface FormattableToJSON { |
0aef76c4 | 22 | toFormattedJSON () |
154898b0 C |
23 | } |
24 | ||
40298b02 | 25 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { |
0aef76c4 | 26 | const formattedObjects: U[] = [] |
55fa55a9 | 27 | |
075f16ca | 28 | objects.forEach(object => { |
0aef76c4 | 29 | formattedObjects.push(object.toFormattedJSON()) |
55fa55a9 C |
30 | }) |
31 | ||
6fcd19ba | 32 | const res: ResultList<U> = { |
55fa55a9 | 33 | total: objectsTotal, |
0aef76c4 | 34 | data: formattedObjects |
55fa55a9 | 35 | } |
6fcd19ba C |
36 | |
37 | return res | |
55fa55a9 C |
38 | } |
39 | ||
f5028693 | 40 | async function isSignupAllowed () { |
291e8d3e | 41 | if (CONFIG.SIGNUP.ENABLED === false) { |
f5028693 | 42 | return false |
291e8d3e C |
43 | } |
44 | ||
45 | // No limit and signup is enabled | |
46 | if (CONFIG.SIGNUP.LIMIT === -1) { | |
f5028693 | 47 | return true |
291e8d3e C |
48 | } |
49 | ||
3fd3ab2d | 50 | const totalUsers = await UserModel.countTotal() |
f5028693 C |
51 | |
52 | return totalUsers < CONFIG.SIGNUP.LIMIT | |
291e8d3e C |
53 | } |
54 | ||
40298b02 C |
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) { | |
14d3270f | 68 | if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) { |
40298b02 C |
69 | resolutionsEnabled.push(resolution) |
70 | } | |
71 | } | |
72 | ||
73 | return resolutionsEnabled | |
74 | } | |
75 | ||
3fd3ab2d | 76 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { |
eb080476 C |
77 | Object.keys(savedFields).forEach(key => { |
78 | const value = savedFields[key] | |
79 | instance.set(key, value) | |
80 | }) | |
81 | } | |
82 | ||
3fd3ab2d | 83 | let serverAccount: AccountModel |
efc32059 C |
84 | async function getServerAccount () { |
85 | if (serverAccount === undefined) { | |
3fd3ab2d | 86 | serverAccount = await AccountModel.loadApplication() |
7a7724e6 C |
87 | } |
88 | ||
efc32059 C |
89 | if (!serverAccount) { |
90 | logger.error('Cannot load server account.') | |
91 | process.exit(0) | |
92 | } | |
93 | ||
94 | return Promise.resolve(serverAccount) | |
7a7724e6 C |
95 | } |
96 | ||
792dbaf0 GS |
97 | type SortType = { sortModel: any, sortValue: string } |
98 | ||
9f10b292 | 99 | // --------------------------------------------------------------------------- |
c45f7f84 | 100 | |
65fcc311 C |
101 | export { |
102 | badRequest, | |
65fcc311 | 103 | generateRandomString, |
0aef76c4 | 104 | getFormattedObjects, |
792dbaf0 | 105 | isSignupAllowed, |
40298b02 | 106 | computeResolutionsToTranscode, |
eb080476 | 107 | resetSequelizeInstance, |
efc32059 | 108 | getServerAccount, |
792dbaf0 | 109 | SortType |
65fcc311 | 110 | } |