server.js 6.8 KB

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