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 import { extname } from 'path'
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
14 if (res.locals.oauth) {
15 const user: User = res.locals.oauth.token.User
16 // User does not want NSFW videos
17 if (user && user.nsfwPolicy === 'do_not_list') return false
20 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
26 function getHostWithPort (host: string) {
27 const splitted = host.split(':')
29 // The port was not specified
30 if (splitted.length === 1) {
31 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
39 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
40 return res.type('json').status(400).end()
43 function createReqFiles (
45 mimeTypes: { [ id: string ]: string },
46 destinations: { [ fieldName: string ]: string }
48 const storage = multer.diskStorage({
49 destination: (req, file, cb) => {
50 cb(null, destinations[ file.fieldname ])
53 filename: async (req, file, cb) => {
54 const extension = mimeTypes[ file.mimetype ] || extname(file.originalname)
58 randomString = await generateRandomString(16)
60 logger.error('Cannot generate random string for file name.', { err })
61 randomString = 'fake-random-string'
64 cb(null, randomString + extension)
68 let fields: { name: string, maxCount: number }[] = []
69 for (const fieldName of fieldNames) {
76 return multer({ storage }).fields(fields)
79 // ---------------------------------------------------------------------------