aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/express-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/express-utils.ts')
-rw-r--r--server/helpers/express-utils.ts77
1 files changed, 53 insertions, 24 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
index 780fd6345..08f77966f 100644
--- a/server/helpers/express-utils.ts
+++ b/server/helpers/express-utils.ts
@@ -1,9 +1,9 @@
1import express, { RequestHandler } from 'express' 1import express, { RequestHandler } from 'express'
2import multer, { diskStorage } from 'multer' 2import multer, { diskStorage } from 'multer'
3import { getLowercaseExtension } from '@shared/core-utils'
3import { HttpStatusCode } from '../../shared/models/http/http-error-codes' 4import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
4import { CONFIG } from '../initializers/config' 5import { CONFIG } from '../initializers/config'
5import { REMOTE_SCHEME } from '../initializers/constants' 6import { REMOTE_SCHEME } from '../initializers/constants'
6import { getLowercaseExtension } from '@shared/core-utils'
7import { isArray } from './custom-validators/misc' 7import { isArray } from './custom-validators/misc'
8import { logger } from './logger' 8import { logger } from './logger'
9import { deleteFileAndCatch, generateRandomString } from './utils' 9import { deleteFileAndCatch, generateRandomString } from './utils'
@@ -75,29 +75,8 @@ function createReqFiles (
75 cb(null, destinations[file.fieldname]) 75 cb(null, destinations[file.fieldname])
76 }, 76 },
77 77
78 filename: async (req, file, cb) => { 78 filename: (req, file, cb) => {
79 let extension: string 79 return generateReqFilename(file, mimeTypes, cb)
80 const fileExtension = getLowercaseExtension(file.originalname)
81 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
82
83 // Take the file extension if we don't understand the mime type
84 if (!extensionFromMimetype) {
85 extension = fileExtension
86 } else {
87 // Take the first available extension for this mimetype
88 extension = extensionFromMimetype
89 }
90
91 let randomString = ''
92
93 try {
94 randomString = await generateRandomString(16)
95 } catch (err) {
96 logger.error('Cannot generate random string for file name.', { err })
97 randomString = 'fake-random-string'
98 }
99
100 cb(null, randomString + extension)
101 } 80 }
102 }) 81 })
103 82
@@ -112,6 +91,24 @@ function createReqFiles (
112 return multer({ storage }).fields(fields) 91 return multer({ storage }).fields(fields)
113} 92}
114 93
94function 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
98): RequestHandler {
99 const storage = diskStorage({
100 destination: (req, file, cb) => {
101 cb(null, destinationDirectory)
102 },
103
104 filename: (req, file, cb) => {
105 return generateReqFilename(file, mimeTypes, cb)
106 }
107 })
108
109 return multer({ storage, fileFilter }).any()
110}
111
115function isUserAbleToSearchRemoteURI (res: express.Response) { 112function isUserAbleToSearchRemoteURI (res: express.Response) {
116 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined 113 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
117 114
@@ -128,9 +125,41 @@ function getCountVideos (req: express.Request) {
128export { 125export {
129 buildNSFWFilter, 126 buildNSFWFilter,
130 getHostWithPort, 127 getHostWithPort,
128 createAnyReqFiles,
131 isUserAbleToSearchRemoteURI, 129 isUserAbleToSearchRemoteURI,
132 badRequest, 130 badRequest,
133 createReqFiles, 131 createReqFiles,
134 cleanUpReqFiles, 132 cleanUpReqFiles,
135 getCountVideos 133 getCountVideos
136} 134}
135
136// ---------------------------------------------------------------------------
137
138async function generateReqFilename (
139 file: Express.Multer.File,
140 mimeTypes: { [id: string]: string | string[] },
141 cb: (err: Error, name: string) => void
142) {
143 let extension: string
144 const fileExtension = getLowercaseExtension(file.originalname)
145 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
146
147 // Take the file extension if we don't understand the mime type
148 if (!extensionFromMimetype) {
149 extension = fileExtension
150 } else {
151 // Take the first available extension for this mimetype
152 extension = extensionFromMimetype
153 }
154
155 let randomString = ''
156
157 try {
158 randomString = await generateRandomString(16)
159 } catch (err) {
160 logger.error('Cannot generate random string for file name.', { err })
161 randomString = 'fake-random-string'
162 }
163
164 cb(null, randomString + extension)
165}