diff options
author | Chocobozzz <me@florianbigard.com> | 2018-04-24 15:10:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-04-24 15:13:19 +0200 |
commit | 0626e7af82e02f8a5bd1e74a7d4d8c916d073ceb (patch) | |
tree | 79b5befbc6a04c007e5919805f1514d065b30e11 /server/helpers | |
parent | b4d1af3dd8cdab2d58927e671d62194ca383cd75 (diff) | |
download | PeerTube-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.ts | 77 | ||||
-rw-r--r-- | server/helpers/utils.ts | 62 |
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 @@ | |||
1 | import * as express from 'express' | ||
2 | import * as multer from 'multer' | ||
3 | import { CONFIG, REMOTE_SCHEME } from '../initializers' | ||
4 | import { logger } from './logger' | ||
5 | import { User } from '../../shared/models/users' | ||
6 | import { generateRandomString } from './utils' | ||
7 | |||
8 | function 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 | |||
17 | function 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 | |||
30 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
31 | return res.type('json').status(400).end() | ||
32 | } | ||
33 | |||
34 | function 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 | |||
72 | export { | ||
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 @@ | |||
1 | import * as express from 'express' | ||
2 | import * as multer from 'multer' | ||
3 | import { Model } from 'sequelize-typescript' | 1 | import { Model } from 'sequelize-typescript' |
4 | import { ResultList } from '../../shared' | 2 | import { ResultList } from '../../shared' |
5 | import { VideoResolution } from '../../shared/models/videos' | 3 | import { VideoResolution } from '../../shared/models/videos' |
6 | import { CONFIG, REMOTE_SCHEME } from '../initializers' | 4 | import { CONFIG } from '../initializers' |
7 | import { UserModel } from '../models/account/user' | 5 | import { UserModel } from '../models/account/user' |
8 | import { ActorModel } from '../models/activitypub/actor' | 6 | import { ActorModel } from '../models/activitypub/actor' |
9 | import { ApplicationModel } from '../models/application/application' | 7 | import { ApplicationModel } from '../models/application/application' |
10 | import { pseudoRandomBytesPromise } from './core-utils' | 8 | import { pseudoRandomBytesPromise } from './core-utils' |
11 | import { logger } from './logger' | 9 | import { logger } from './logger' |
12 | 10 | ||
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 | |||
26 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
27 | return res.type('json').status(400).end() | ||
28 | } | ||
29 | |||
30 | function 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 | |||
66 | async function generateRandomString (size: number) { | 11 | async 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 | ||
153 | export { | 98 | export { |
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 | } |