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