1 import express, { RequestHandler } from 'express'
2 import multer, { diskStorage } from 'multer'
3 import { getLowercaseExtension } from '@shared/core-utils'
4 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
5 import { CONFIG } from '../initializers/config'
6 import { REMOTE_SCHEME } from '../initializers/constants'
7 import { isArray } from './custom-validators/misc'
8 import { logger } from './logger'
9 import { deleteFileAndCatch, generateRandomString } from './utils'
10 import { getExtFromMimetype } from './video'
12 function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
13 if (paramNSFW === 'true') return true
14 if (paramNSFW === 'false') return false
15 if (paramNSFW === 'both') return undefined
17 if (res?.locals.oauth) {
18 const user = res.locals.oauth.token.User
20 // User does not want NSFW videos
21 if (user.nsfwPolicy === 'do_not_list') return false
27 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
33 function cleanUpReqFiles (req: express.Request) {
34 const filesObject = req.files
35 if (!filesObject) return
37 if (isArray(filesObject)) {
38 filesObject.forEach(f => deleteFileAndCatch(f.path))
42 for (const key of Object.keys(filesObject)) {
43 const files = filesObject[key]
45 files.forEach(f => deleteFileAndCatch(f.path))
49 function getHostWithPort (host: string) {
50 const splitted = host.split(':')
52 // The port was not specified
53 if (splitted.length === 1) {
54 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
62 function badRequest (_req: express.Request, res: express.Response) {
63 return res.type('json')
64 .status(HttpStatusCode.BAD_REQUEST_400)
68 function createReqFiles (
70 mimeTypes: { [id: string]: string | string[] },
71 destinations: { [fieldName: string]: string }
73 const storage = diskStorage({
74 destination: (req, file, cb) => {
75 cb(null, destinations[file.fieldname])
78 filename: (req, file, cb) => {
79 return generateReqFilename(file, mimeTypes, cb)
83 const fields: { name: string, maxCount: number }[] = []
84 for (const fieldName of fieldNames) {
91 return multer({ storage }).fields(fields)
94 function createAnyReqFiles (
95 mimeTypes: { [id: string]: string | string[] },
96 destinationDirectory: string,
97 fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void
99 const storage = diskStorage({
100 destination: (req, file, cb) => {
101 cb(null, destinationDirectory)
104 filename: (req, file, cb) => {
105 return generateReqFilename(file, mimeTypes, cb)
109 return multer({ storage, fileFilter }).any()
112 function isUserAbleToSearchRemoteURI (res: express.Response) {
113 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
115 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
116 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
119 function getCountVideos (req: express.Request) {
120 return req.query.skipCount !== true
123 // ---------------------------------------------------------------------------
129 isUserAbleToSearchRemoteURI,
136 // ---------------------------------------------------------------------------
138 async function generateReqFilename (
139 file: Express.Multer.File,
140 mimeTypes: { [id: string]: string | string[] },
141 cb: (err: Error, name: string) => void
143 let extension: string
144 const fileExtension = getLowercaseExtension(file.originalname)
145 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
147 // Take the file extension if we don't understand the mime type
148 if (!extensionFromMimetype) {
149 extension = fileExtension
151 // Take the first available extension for this mimetype
152 extension = extensionFromMimetype
155 let randomString = ''
158 randomString = await generateRandomString(16)
160 logger.error('Cannot generate random string for file name.', { err })
161 randomString = 'fake-random-string'
164 cb(null, randomString + extension)