]>
Commit | Line | Data |
---|---|---|
0626e7af C |
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' | |
2769e297 | 7 | import { extname } from 'path' |
0626e7af | 8 | |
0b18f4aa C |
9 | function buildNSFWFilter (res: express.Response, paramNSFW?: string) { |
10 | if (paramNSFW === 'true') return true | |
11 | if (paramNSFW === 'false') return false | |
12 | if (paramNSFW === 'both') return undefined | |
d525fc39 | 13 | |
0626e7af C |
14 | if (res.locals.oauth) { |
15 | const user: User = res.locals.oauth.token.User | |
d525fc39 C |
16 | // User does not want NSFW videos |
17 | if (user && user.nsfwPolicy === 'do_not_list') return false | |
0626e7af C |
18 | } |
19 | ||
d525fc39 C |
20 | if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false |
21 | ||
22 | // Display all | |
23 | return null | |
0626e7af C |
24 | } |
25 | ||
26 | function getHostWithPort (host: string) { | |
27 | const splitted = host.split(':') | |
28 | ||
29 | // The port was not specified | |
30 | if (splitted.length === 1) { | |
31 | if (REMOTE_SCHEME.HTTP === 'https') return host + ':443' | |
32 | ||
33 | return host + ':80' | |
34 | } | |
35 | ||
36 | return host | |
37 | } | |
38 | ||
39 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | |
40 | return res.type('json').status(400).end() | |
41 | } | |
42 | ||
43 | function createReqFiles ( | |
44 | fieldNames: string[], | |
45 | mimeTypes: { [ id: string ]: string }, | |
46 | destinations: { [ fieldName: string ]: string } | |
47 | ) { | |
48 | const storage = multer.diskStorage({ | |
49 | destination: (req, file, cb) => { | |
50 | cb(null, destinations[ file.fieldname ]) | |
51 | }, | |
52 | ||
53 | filename: async (req, file, cb) => { | |
2769e297 | 54 | const extension = mimeTypes[ file.mimetype ] || extname(file.originalname) |
0626e7af C |
55 | let randomString = '' |
56 | ||
57 | try { | |
58 | randomString = await generateRandomString(16) | |
59 | } catch (err) { | |
60 | logger.error('Cannot generate random string for file name.', { err }) | |
61 | randomString = 'fake-random-string' | |
62 | } | |
63 | ||
64 | cb(null, randomString + extension) | |
65 | } | |
66 | }) | |
67 | ||
c1e791ba | 68 | let fields: { name: string, maxCount: number }[] = [] |
0626e7af C |
69 | for (const fieldName of fieldNames) { |
70 | fields.push({ | |
71 | name: fieldName, | |
72 | maxCount: 1 | |
73 | }) | |
74 | } | |
75 | ||
76 | return multer({ storage }).fields(fields) | |
77 | } | |
78 | ||
79 | // --------------------------------------------------------------------------- | |
80 | ||
81 | export { | |
d525fc39 | 82 | buildNSFWFilter, |
0626e7af C |
83 | getHostWithPort, |
84 | badRequest, | |
85 | createReqFiles | |
86 | } |