aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-04-24 15:10:54 +0200
committerChocobozzz <me@florianbigard.com>2018-04-24 15:13:19 +0200
commit0626e7af82e02f8a5bd1e74a7d4d8c916d073ceb (patch)
tree79b5befbc6a04c007e5919805f1514d065b30e11 /server/helpers
parentb4d1af3dd8cdab2d58927e671d62194ca383cd75 (diff)
downloadPeerTube-0626e7af82e02f8a5bd1e74a7d4d8c916d073ceb.tar.gz
PeerTube-0626e7af82e02f8a5bd1e74a7d4d8c916d073ceb.tar.zst
PeerTube-0626e7af82e02f8a5bd1e74a7d4d8c916d073ceb.zip
Add account view
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/express-utils.ts77
-rw-r--r--server/helpers/utils.ts62
2 files changed, 79 insertions, 60 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
new file mode 100644
index 000000000..d023117a8
--- /dev/null
+++ b/server/helpers/express-utils.ts
@@ -0,0 +1,77 @@
1import * as express from 'express'
2import * as multer from 'multer'
3import { CONFIG, REMOTE_SCHEME } from '../initializers'
4import { logger } from './logger'
5import { User } from '../../shared/models/users'
6import { generateRandomString } from './utils'
7
8function isNSFWHidden (res: express.Response) {
9 if (res.locals.oauth) {
10 const user: User = res.locals.oauth.token.User
11 if (user) return user.nsfwPolicy === 'do_not_list'
12 }
13
14 return CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
15}
16
17function getHostWithPort (host: string) {
18 const splitted = host.split(':')
19
20 // The port was not specified
21 if (splitted.length === 1) {
22 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
23
24 return host + ':80'
25 }
26
27 return host
28}
29
30function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
31 return res.type('json').status(400).end()
32}
33
34function createReqFiles (
35 fieldNames: string[],
36 mimeTypes: { [ id: string ]: string },
37 destinations: { [ fieldName: string ]: string }
38) {
39 const storage = multer.diskStorage({
40 destination: (req, file, cb) => {
41 cb(null, destinations[ file.fieldname ])
42 },
43
44 filename: async (req, file, cb) => {
45 const extension = mimeTypes[ file.mimetype ]
46 let randomString = ''
47
48 try {
49 randomString = await generateRandomString(16)
50 } catch (err) {
51 logger.error('Cannot generate random string for file name.', { err })
52 randomString = 'fake-random-string'
53 }
54
55 cb(null, randomString + extension)
56 }
57 })
58
59 const fields = []
60 for (const fieldName of fieldNames) {
61 fields.push({
62 name: fieldName,
63 maxCount: 1
64 })
65 }
66
67 return multer({ storage }).fields(fields)
68}
69
70// ---------------------------------------------------------------------------
71
72export {
73 isNSFWHidden,
74 getHostWithPort,
75 badRequest,
76 createReqFiles
77}
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}