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?: string) {
9 if (paramNSFW === 'true') return true
10 if (paramNSFW === 'false') return false
11 if (paramNSFW === 'both') return undefined
13 if (res.locals.oauth) {
14 const user: User = res.locals.oauth.token.User
15 // User does not want NSFW videos
16 if (user && user.nsfwPolicy === 'do_not_list') return false
19 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
25 function getHostWithPort (host: string) {
26 const splitted = host.split(':')
28 // The port was not specified
29 if (splitted.length === 1) {
30 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
38 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
39 return res.type('json').status(400).end()
42 function createReqFiles (
44 mimeTypes: { [ id: string ]: string },
45 destinations: { [ fieldName: string ]: string }
47 const storage = multer.diskStorage({
48 destination: (req, file, cb) => {
49 cb(null, destinations[ file.fieldname ])
52 filename: async (req, file, cb) => {
53 const extension = mimeTypes[ file.mimetype ]
57 randomString = await generateRandomString(16)
59 logger.error('Cannot generate random string for file name.', { err })
60 randomString = 'fake-random-string'
63 cb(null, randomString + extension)
68 for (const fieldName of fieldNames) {
75 return multer({ storage }).fields(fields)
78 // ---------------------------------------------------------------------------