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