]>
Commit | Line | Data |
---|---|---|
69818c93 | 1 | import * as express from 'express' |
3fd3ab2d | 2 | import { Model } from 'sequelize-typescript' |
6fcd19ba | 3 | import { ResultList } from '../../shared' |
3fd3ab2d | 4 | import { VideoResolution } from '../../shared/models/videos' |
0405ab52 | 5 | import { CONFIG, REMOTE_SCHEME } from '../initializers' |
3fd3ab2d | 6 | import { UserModel } from '../models/account/user' |
50d6de9c C |
7 | import { ActorModel } from '../models/activitypub/actor' |
8 | import { ApplicationModel } from '../models/application/application' | |
8d468a16 | 9 | import { pseudoRandomBytesPromise } from './core-utils' |
efc32059 | 10 | import { logger } from './logger' |
cbe2f7c3 | 11 | |
0405ab52 C |
12 | function getHostWithPort (host: string) { |
13 | const splitted = host.split(':') | |
14 | ||
15 | // The port was not specified | |
16 | if (splitted.length === 1) { | |
17 | if (REMOTE_SCHEME.HTTP === 'https') return host + ':443' | |
18 | ||
19 | return host + ':80' | |
20 | } | |
21 | ||
22 | return host | |
23 | } | |
24 | ||
69818c93 | 25 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { |
f5028693 | 26 | return res.type('json').status(400).end() |
a6fd2b30 C |
27 | } |
28 | ||
f5028693 C |
29 | async function generateRandomString (size: number) { |
30 | const raw = await pseudoRandomBytesPromise(size) | |
31 | ||
32 | return raw.toString('hex') | |
e4c87ec2 C |
33 | } |
34 | ||
40298b02 | 35 | interface FormattableToJSON { |
0aef76c4 | 36 | toFormattedJSON () |
154898b0 C |
37 | } |
38 | ||
40298b02 | 39 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { |
0aef76c4 | 40 | const formattedObjects: U[] = [] |
55fa55a9 | 41 | |
075f16ca | 42 | objects.forEach(object => { |
0aef76c4 | 43 | formattedObjects.push(object.toFormattedJSON()) |
55fa55a9 C |
44 | }) |
45 | ||
6fcd19ba | 46 | const res: ResultList<U> = { |
55fa55a9 | 47 | total: objectsTotal, |
0aef76c4 | 48 | data: formattedObjects |
55fa55a9 | 49 | } |
6fcd19ba C |
50 | |
51 | return res | |
55fa55a9 C |
52 | } |
53 | ||
f5028693 | 54 | async function isSignupAllowed () { |
291e8d3e | 55 | if (CONFIG.SIGNUP.ENABLED === false) { |
f5028693 | 56 | return false |
291e8d3e C |
57 | } |
58 | ||
59 | // No limit and signup is enabled | |
60 | if (CONFIG.SIGNUP.LIMIT === -1) { | |
f5028693 | 61 | return true |
291e8d3e C |
62 | } |
63 | ||
3fd3ab2d | 64 | const totalUsers = await UserModel.countTotal() |
f5028693 C |
65 | |
66 | return totalUsers < CONFIG.SIGNUP.LIMIT | |
291e8d3e C |
67 | } |
68 | ||
40298b02 C |
69 | function computeResolutionsToTranscode (videoFileHeight: number) { |
70 | const resolutionsEnabled: number[] = [] | |
71 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
72 | ||
73 | const resolutions = [ | |
74 | VideoResolution.H_240P, | |
75 | VideoResolution.H_360P, | |
76 | VideoResolution.H_480P, | |
77 | VideoResolution.H_720P, | |
78 | VideoResolution.H_1080P | |
79 | ] | |
80 | ||
81 | for (const resolution of resolutions) { | |
14d3270f | 82 | if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) { |
40298b02 C |
83 | resolutionsEnabled.push(resolution) |
84 | } | |
85 | } | |
86 | ||
87 | return resolutionsEnabled | |
88 | } | |
89 | ||
3fd3ab2d | 90 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { |
eb080476 C |
91 | Object.keys(savedFields).forEach(key => { |
92 | const value = savedFields[key] | |
93 | instance.set(key, value) | |
94 | }) | |
95 | } | |
96 | ||
50d6de9c C |
97 | let serverActor: ActorModel |
98 | async function getServerActor () { | |
99 | if (serverActor === undefined) { | |
100 | const application = await ApplicationModel.load() | |
101 | serverActor = application.Account.Actor | |
7a7724e6 C |
102 | } |
103 | ||
50d6de9c C |
104 | if (!serverActor) { |
105 | logger.error('Cannot load server actor.') | |
efc32059 C |
106 | process.exit(0) |
107 | } | |
108 | ||
50d6de9c | 109 | return Promise.resolve(serverActor) |
7a7724e6 C |
110 | } |
111 | ||
792dbaf0 GS |
112 | type SortType = { sortModel: any, sortValue: string } |
113 | ||
9f10b292 | 114 | // --------------------------------------------------------------------------- |
c45f7f84 | 115 | |
65fcc311 C |
116 | export { |
117 | badRequest, | |
65fcc311 | 118 | generateRandomString, |
0aef76c4 | 119 | getFormattedObjects, |
792dbaf0 | 120 | isSignupAllowed, |
40298b02 | 121 | computeResolutionsToTranscode, |
eb080476 | 122 | resetSequelizeInstance, |
50d6de9c | 123 | getServerActor, |
0405ab52 C |
124 | SortType, |
125 | getHostWithPort | |
65fcc311 | 126 | } |