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 buildNSFWFilter (res: express.Response, paramNSFW?: boolean) {
9 if (paramNSFW === true || paramNSFW === false) return paramNSFW
11 if (res.locals.oauth) {
12 const user: User = res.locals.oauth.token.User
13 // User does not want NSFW videos
14 if (user && user.nsfwPolicy === 'do_not_list') return false
17 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
23 function getHostWithPort (host: string) {
24 const splitted = host.split(':')
26 // The port was not specified
27 if (splitted.length === 1) {
28 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
36 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
37 return res.type('json').status(400).end()
40 function createReqFiles (
42 mimeTypes: { [ id: string ]: string },
43 destinations: { [ fieldName: string ]: string }
45 const storage = multer.diskStorage({
46 destination: (req, file, cb) => {
47 cb(null, destinations[ file.fieldname ])
50 filename: async (req, file, cb) => {
51 const extension = mimeTypes[ file.mimetype ]
55 randomString = await generateRandomString(16)
57 logger.error('Cannot generate random string for file name.', { err })
58 randomString = 'fake-random-string'
61 cb(null, randomString + extension)
66 for (const fieldName of fieldNames) {
73 return multer({ storage }).fields(fields)
76 // ---------------------------------------------------------------------------