server.js 8.3 KB

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