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'
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'
14 return CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
17 function getHostWithPort (host: string) {
18 const splitted = host.split(':')
20 // The port was not specified
21 if (splitted.length === 1) {
22 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
30 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
31 return res.type('json').status(400).end()
34 function createReqFiles (
36 mimeTypes: { [ id: string ]: string },
37 destinations: { [ fieldName: string ]: string }
39 const storage = multer.diskStorage({
40 destination: (req, file, cb) => {
41 cb(null, destinations[ file.fieldname ])
44 filename: async (req, file, cb) => {
45 const extension = mimeTypes[ file.mimetype ]
49 randomString = await generateRandomString(16)
51 logger.error('Cannot generate random string for file name.', { err })
52 randomString = 'fake-random-string'
55 cb(null, randomString + extension)
60 for (const fieldName of fieldNames) {
67 return multer({ storage }).fields(fields)
70 // ---------------------------------------------------------------------------