aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r--server/helpers/utils.ts62
1 files changed, 2 insertions, 60 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index c58117219..058c3211e 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,68 +1,13 @@
1import * as express from 'express'
2import * as multer from 'multer'
3import { Model } from 'sequelize-typescript' 1import { Model } from 'sequelize-typescript'
4import { ResultList } from '../../shared' 2import { ResultList } from '../../shared'
5import { VideoResolution } from '../../shared/models/videos' 3import { VideoResolution } from '../../shared/models/videos'
6import { CONFIG, REMOTE_SCHEME } from '../initializers' 4import { CONFIG } from '../initializers'
7import { UserModel } from '../models/account/user' 5import { UserModel } from '../models/account/user'
8import { ActorModel } from '../models/activitypub/actor' 6import { ActorModel } from '../models/activitypub/actor'
9import { ApplicationModel } from '../models/application/application' 7import { ApplicationModel } from '../models/application/application'
10import { pseudoRandomBytesPromise } from './core-utils' 8import { pseudoRandomBytesPromise } from './core-utils'
11import { logger } from './logger' 9import { logger } from './logger'
12 10
13function 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
26function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
27 return res.type('json').status(400).end()
28}
29
30function createReqFiles (
31 fieldNames: string[],
32 mimeTypes: { [ id: string ]: string },
33 destinations: { [ fieldName: string ]: string }
34) {
35 const storage = multer.diskStorage({
36 destination: (req, file, cb) => {
37 cb(null, destinations[file.fieldname])
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
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)
64}
65
66async function generateRandomString (size: number) { 11async function generateRandomString (size: number) {
67 const raw = await pseudoRandomBytesPromise(size) 12 const raw = await pseudoRandomBytesPromise(size)
68 13
@@ -151,14 +96,11 @@ type SortType = { sortModel: any, sortValue: string }
151// --------------------------------------------------------------------------- 96// ---------------------------------------------------------------------------
152 97
153export { 98export {
154 badRequest,
155 generateRandomString, 99 generateRandomString,
156 getFormattedObjects, 100 getFormattedObjects,
157 isSignupAllowed, 101 isSignupAllowed,
158 computeResolutionsToTranscode, 102 computeResolutionsToTranscode,
159 resetSequelizeInstance, 103 resetSequelizeInstance,
160 getServerActor, 104 getServerActor,
161 SortType, 105 SortType
162 getHostWithPort,
163 createReqFiles
164} 106}