1 | const ACCEPTABLE_CONTENT_TYPE = /^(multipart\/.+);(.*)$/i;
|
2 | const UNACCEPTABLE_METHODS = ['GET', 'HEAD'];
|
3 |
|
4 | /**
|
5 | * Ensures the request contains a content body
|
6 | * @param {Object} req Express req object
|
7 | * @returns {Boolean}
|
8 | */
|
9 | const hasBody = (req) => {
|
10 | return ('transfer-encoding' in req.headers) ||
|
11 | ('content-length' in req.headers && req.headers['content-length'] !== '0');
|
12 | };
|
13 |
|
14 | /**
|
15 | * Ensures the request is not using a non-compliant multipart method
|
16 | * such as GET or HEAD
|
17 | * @param {Object} req Express req object
|
18 | * @returns {Boolean}
|
19 | */
|
20 | const hasAcceptableMethod = req => !UNACCEPTABLE_METHODS.includes(req.method);
|
21 |
|
22 | /**
|
23 | * Ensures that only multipart requests are processed by express-fileupload
|
24 | * @param {Object} req Express req object
|
25 | * @returns {Boolean}
|
26 | */
|
27 | const hasAcceptableContentType = req => ACCEPTABLE_CONTENT_TYPE.test(req.headers['content-type']);
|
28 |
|
29 | /**
|
30 | * Ensures that the request in question is eligible for file uploads
|
31 | * @param {Object} req Express req object
|
32 | * @returns {Boolean}
|
33 | */
|
34 | module.exports = req => hasBody(req) && hasAcceptableMethod(req) && hasAcceptableContentType(req);
|