1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { extname } from 'path'
4 import { HttpStatusCode } from '../../shared/core-utils/miscs/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'
11 import { ProblemDocument, ProblemDocumentExtension } from 'http-problem-details'
13 function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
14 if (paramNSFW === 'true') return true
15 if (paramNSFW === 'false') return false
16 if (paramNSFW === 'both') return undefined
18 if (res?.locals.oauth) {
19 const user = res.locals.oauth.token.User
21 // User does not want NSFW videos
22 if (user.nsfwPolicy === 'do_not_list') return false
28 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
34 function cleanUpReqFiles (
35 req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] }
37 const filesObject = req.files
38 if (!filesObject) return
40 if (isArray(filesObject)) {
41 filesObject.forEach(f => deleteFileAndCatch(f.path))
45 for (const key of Object.keys(filesObject)) {
46 const files = filesObject[key]
48 files.forEach(f => deleteFileAndCatch(f.path))
52 function getHostWithPort (host: string) {
53 const splitted = host.split(':')
55 // The port was not specified
56 if (splitted.length === 1) {
57 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
65 function badRequest (req: express.Request, res: express.Response) {
66 return res.type('json')
67 .status(HttpStatusCode.BAD_REQUEST_400)
71 function createReqFiles (
73 mimeTypes: { [id: string]: string | string[] },
74 destinations: { [fieldName: string]: string }
76 const storage = multer.diskStorage({
77 destination: (req, file, cb) => {
78 cb(null, destinations[file.fieldname])
81 filename: async (req, file, cb) => {
83 const fileExtension = extname(file.originalname)
84 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
86 // Take the file extension if we don't understand the mime type
87 if (!extensionFromMimetype) {
88 extension = fileExtension
90 // Take the first available extension for this mimetype
91 extension = extensionFromMimetype
97 randomString = await generateRandomString(16)
99 logger.error('Cannot generate random string for file name.', { err })
100 randomString = 'fake-random-string'
103 cb(null, randomString + extension)
107 const fields: { name: string, maxCount: number }[] = []
108 for (const fieldName of fieldNames) {
115 return multer({ storage }).fields(fields)
118 function isUserAbleToSearchRemoteURI (res: express.Response) {
119 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
121 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
122 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
125 function getCountVideos (req: express.Request) {
126 return req.query.skipCount !== true
129 // helpers added in server.ts and used in subsequent controllers used
130 const apiResponseHelpers = (req, res: express.Response, next = null) => {
131 res.fail = (options) => {
132 const { data, status, message, title, type, docs, instance } = {
135 status: options.status || HttpStatusCode.BAD_REQUEST_400
138 const extension = new ProblemDocumentExtension({
140 docs: docs || res.docs
144 res.setHeader('Content-Type', 'application/problem+json')
145 res.json(new ProblemDocument({
149 type: type && '' + type,
154 if (next !== null) next()
157 // ---------------------------------------------------------------------------
162 isUserAbleToSearchRemoteURI,