1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { REMOTE_SCHEME } from '../initializers/constants'
4 import { logger } from './logger'
5 import { deleteFileAsync, generateRandomString } from './utils'
6 import { extname } from 'path'
7 import { isArray } from './custom-validators/misc'
8 import { CONFIG } from '../initializers/config'
9 import { getExtFromMimetype } from './video'
11 function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
12 if (paramNSFW === 'true') return true
13 if (paramNSFW === 'false') return false
14 if (paramNSFW === 'both') return undefined
16 if (res?.locals.oauth) {
17 const user = res.locals.oauth.token.User
19 // User does not want NSFW videos
20 if (user.nsfwPolicy === 'do_not_list') return false
26 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
32 function cleanUpReqFiles (req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] }) {
33 const files = req.files
38 (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
42 for (const key of Object.keys(files)) {
43 const file = files[key]
45 if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
46 else deleteFileAsync(file.path)
50 function getHostWithPort (host: string) {
51 const splitted = host.split(':')
53 // The port was not specified
54 if (splitted.length === 1) {
55 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
63 function badRequest (req: express.Request, res: express.Response) {
64 return res.type('json').status(400).end()
67 function createReqFiles (
69 mimeTypes: { [id: string]: string | string[] },
70 destinations: { [fieldName: string]: string }
72 const storage = multer.diskStorage({
73 destination: (req, file, cb) => {
74 cb(null, destinations[file.fieldname])
77 filename: async (req, file, cb) => {
79 const fileExtension = extname(file.originalname)
80 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
82 // Take the file extension if we don't understand the mime type
83 if (!extensionFromMimetype) {
84 extension = fileExtension
86 // Take the first available extension for this mimetype
87 extension = extensionFromMimetype
93 randomString = await generateRandomString(16)
95 logger.error('Cannot generate random string for file name.', { err })
96 randomString = 'fake-random-string'
99 cb(null, randomString + extension)
103 const fields: { name: string, maxCount: number }[] = []
104 for (const fieldName of fieldNames) {
111 return multer({ storage }).fields(fields)
114 function isUserAbleToSearchRemoteURI (res: express.Response) {
115 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
117 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
118 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
121 function getCountVideos (req: express.Request) {
122 return req.query.skipCount !== true
125 // ---------------------------------------------------------------------------
130 isUserAbleToSearchRemoteURI,