You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
6.7 KiB

  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const rimraf = require('rimraf');
  5. const fileDir = path.join(__dirname, 'files');
  6. const tempDir = path.join(__dirname, 'temp');
  7. const uploadDir = path.join(__dirname, 'uploads');
  8. const clearDir = (dir) => {
  9. if (fs.existsSync(dir)) rimraf.sync(dir);
  10. fs.mkdirSync(dir, { recursive: true });
  11. };
  12. const clearUploadsDir = () => clearDir(uploadDir);
  13. const clearTempDir = () => clearDir(tempDir);
  14. const getUploadedFileData = (file) => ({
  15. md5: file.md5,
  16. name: file.name,
  17. size: file.size,
  18. uploadPath: path.join(uploadDir, file.name),
  19. uploadDir: uploadDir
  20. });
  21. const setup = (fileUploadOptions) => {
  22. const express = require('express');
  23. const expressFileupload = require('../lib/index');
  24. const app = express();
  25. app.use(expressFileupload(fileUploadOptions || {}));
  26. app.all('/upload/single', (req, res) => {
  27. if (!req.files) {
  28. return res.status(400).send('No files were uploaded.');
  29. }
  30. const testFile = req.files.testFile;
  31. const fileData = getUploadedFileData(testFile);
  32. testFile.mv(fileData.uploadPath, (err) => {
  33. if (err) {
  34. console.log('ERR', err); // eslint-disable-line
  35. return res.status(500).send(err);
  36. }
  37. res.json(fileData);
  38. });
  39. });
  40. app.all('/upload/single/promise', (req, res) => {
  41. if (!req.files) {
  42. return res.status(400).send('No files were uploaded.');
  43. }
  44. const testFile = req.files.testFile;
  45. const fileData = getUploadedFileData(testFile);
  46. testFile
  47. .mv(fileData.uploadPath)
  48. .then(() => {
  49. res.json(fileData);
  50. })
  51. .catch(err => {
  52. res.status(500).send(err);
  53. });
  54. });
  55. app.all('/upload/single/withfields', (req, res) => {
  56. if (!req.files) {
  57. return res.status(400).send('No files were uploaded.');
  58. }
  59. if (!req.body) {
  60. return res.status(400).send('No request body found');
  61. }
  62. const fields = ['firstName', 'lastName', 'email'];
  63. for (let i = 0; i < fields.length; i += 1) {
  64. if (!req.body[fields[i]] || !req.body[fields[i]].trim()) {
  65. return res.status(400).send(`Invalid field: ${fields[i]}`);
  66. }
  67. }
  68. const testFile = req.files.testFile;
  69. const fileData = getUploadedFileData(testFile);
  70. fields.forEach((field) => { fileData[field] = req.body[field]; });
  71. testFile.mv(fileData.uploadPath, (err) => {
  72. if (err) {
  73. return res.status(500).send(err);
  74. }
  75. res.json(fileData);
  76. });
  77. });
  78. app.all('/upload/single/truncated', (req, res) => {
  79. if (!req.files) {
  80. return res.status(400).send('No files were uploaded.');
  81. }
  82. // status 400 to differentiate from ending the request in the on limit
  83. return req.files.testFile.truncated
  84. ? res.status(400).send(`File too big`)
  85. : res.status(200).send('Upload succeed');
  86. });
  87. app.all('/upload/multiple', function(req, res) {
  88. if (!req.files) {
  89. return res.status(400).send('No files were uploaded.');
  90. }
  91. const fileNames = ['testFile1', 'testFile2', 'testFile3'];
  92. const testFiles = fileNames.map(file => req.files[file]);
  93. for (let i = 0; i < testFiles.length; i += 1) {
  94. if (!testFiles[i]) {
  95. return res.status(400).send(`${fileNames[i]} was not uploaded!`);
  96. }
  97. }
  98. const filesData = testFiles.map(file => getUploadedFileData(file));
  99. testFiles[0].mv(filesData[0].uploadPath, (err) => {
  100. if (err) {
  101. return res.status(500).send(err);
  102. }
  103. testFiles[1].mv(filesData[1].uploadPath, (err) => {
  104. if (err) {
  105. return res.status(500).send(err);
  106. }
  107. testFiles[2].mv(filesData[2].uploadPath, (err) => {
  108. if (err) {
  109. return res.status(500).send(err);
  110. }
  111. res.json(filesData);
  112. });
  113. });
  114. });
  115. });
  116. app.all('/upload/array', function(req, res) {
  117. if (!req.files) {
  118. return res.status(400).send('No files were uploaded.');
  119. }
  120. const testFiles = req.files.testFiles;
  121. if (!testFiles) {
  122. return res.status(400).send('No files were uploaded');
  123. }
  124. if (!Array.isArray(testFiles)) {
  125. return res.status(400).send('Files were not uploaded as an array');
  126. }
  127. if (!testFiles.length) {
  128. return res.status(400).send('Files array is empty');
  129. }
  130. const filesData = testFiles.map(file => getUploadedFileData(file));
  131. let uploadCount = 0;
  132. for (let i = 0; i < testFiles.length; i += 1) {
  133. testFiles[i].mv(filesData[i].uploadPath, (err) => {
  134. if (err) {
  135. return res.status(500).send(err);
  136. }
  137. uploadCount += 1;
  138. if (uploadCount === testFiles.length) {
  139. res.json(filesData);
  140. }
  141. });
  142. }
  143. });
  144. app.all('/fields/user', function(req, res) {
  145. if (!req.body) {
  146. return res.status(400).send('No request body found');
  147. }
  148. const fields = ['firstName', 'lastName', 'email'];
  149. for (let i = 0; i < fields.length; i += 1) {
  150. if (!req.body[fields[i]] || !req.body[fields[i]].trim()) {
  151. return res.status(400).send(`Invalid field: ${fields[i]}`);
  152. }
  153. }
  154. res.json({
  155. firstName: req.body.firstName,
  156. lastName: req.body.lastName,
  157. email: req.body.email
  158. });
  159. });
  160. app.all('/fields/nested', function(req, res) {
  161. if (!req.body) {
  162. return res.status(400).send('No request body found');
  163. }
  164. if (!req.body.name || !req.body.name.trim()) {
  165. return res.status(400).send('Invalid name');
  166. }
  167. if (!req.body.hobbies || !req.body.hobbies.length == 2) {
  168. return res.status(400).send('Invalid hobbies');
  169. }
  170. res.json({
  171. name: req.body.name,
  172. hobbies: req.body.hobbies
  173. });
  174. });
  175. app.all('/fields/flattened', function(req, res) {
  176. if (!req.body) {
  177. return res.status(400).send('No request body found');
  178. }
  179. if (!req.body.name || !req.body.name.trim()) {
  180. return res.status(400).send('Invalid name');
  181. }
  182. if (!req.body['hobbies[0]'] || !req.body['hobbies[0]'].trim()) {
  183. return res.status(400).send('Invalid hobbies[0]');
  184. }
  185. if (!req.body['hobbies[1]'] || !req.body['hobbies[1]'].trim()) {
  186. return res.status(400).send('Invalid hobbies[1]');
  187. }
  188. res.json({
  189. name: req.body.name,
  190. 'hobbies[0]': req.body['hobbies[0]'],
  191. 'hobbies[1]': req.body['hobbies[1]']
  192. });
  193. });
  194. app.all('/fields/array', function(req, res) {
  195. if (!req.body) {
  196. return res.status(400).send('No request body found');
  197. }
  198. if (!req.body.testField) {
  199. return res.status(400).send('Invalid field');
  200. }
  201. if (!Array.isArray(req.body.testField)) {
  202. return res.status(400).send('Field is not an array');
  203. }
  204. res.json(req.body.testField);
  205. });
  206. return app;
  207. };
  208. module.exports = {
  209. setup,
  210. fileDir,
  211. tempDir,
  212. uploadDir,
  213. clearTempDir,
  214. clearUploadsDir
  215. };