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'
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 (
34 req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] }
36 const filesObject = req.files
37 if (!filesObject) return
39 if (isArray(filesObject)) {
40 filesObject.forEach(f => deleteFileAndCatch(f.path))
44 for (const key of Object.keys(filesObject)) {
45 const files = filesObject[key]
47 files.forEach(f => deleteFileAndCatch(f.path))
51 function getHostWithPort (host: string) {
52 const splitted = host.split(':')
54 // The port was not specified
55 if (splitted.length === 1) {
56 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
64 function badRequest (req: express.Request, res: express.Response) {
65 return res.type('json')
66 .status(HttpStatusCode.BAD_REQUEST_400)
70 function createReqFiles (
72 mimeTypes: { [id: string]: string | string[] },
73 destinations: { [fieldName: string]: string }
75 const storage = multer.diskStorage({
76 destination: (req, file, cb) => {
77 cb(null, destinations[file.fieldname])
80 filename: async (req, file, cb) => {
82 const fileExtension = extname(file.originalname)
83 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
85 // Take the file extension if we don't understand the mime type
86 if (!extensionFromMimetype) {
87 extension = fileExtension
89 // Take the first available extension for this mimetype
90 extension = extensionFromMimetype
96 randomString = await generateRandomString(16)
98 logger.error('Cannot generate random string for file name.', { err })
99 randomString = 'fake-random-string'
102 cb(null, randomString + extension)
106 const fields: { name: string, maxCount: number }[] = []
107 for (const fieldName of fieldNames) {
114 return multer({ storage }).fields(fields)
117 function isUserAbleToSearchRemoteURI (res: express.Response) {
118 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
120 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
121 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
124 function getCountVideos (req: express.Request) {
125 return req.query.skipCount !== true
128 // ---------------------------------------------------------------------------
133 isUserAbleToSearchRemoteURI,