123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 'use strict';
- const path = require('path');
- const fileDir = path.join(__dirname, 'files');
- const uploadDir = path.join(__dirname, 'uploads');
- const fs = require('fs');
- const rimraf = require('rimraf');
- const clearUploadsDir = function() {
- if (!fs.existsSync(uploadDir)) {
- fs.mkdirSync(uploadDir);
- } else {
- rimraf.sync(uploadDir);
- fs.mkdirSync(uploadDir);
- }
- };
- const setup = function(fileUploadOptions) {
- const express = require('express');
- const expressFileupload = require('../lib/index');
- const app = express();
- fileUploadOptions = fileUploadOptions || {};
- app.use(expressFileupload(fileUploadOptions));
- app.all('/upload/single', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- let testFile = req.files.testFile;
- let uploadPath = path.join(uploadDir, testFile.name);
- testFile.mv(uploadPath, function(err) {
- if (err) {
- console.log('ERR', err); // eslint-disable-line
- return res.status(500).send(err);
- }
- //res.send('File uploaded to ' + uploadPath);
- res.json({
- name: testFile.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath,
- md5: testFile.md5,
- size: testFile.size
- });
- });
- });
- app.all('/upload/single/promise', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- let testFile = req.files.testFile;
- let uploadPath = path.join(uploadDir, testFile.name);
- testFile
- .mv(uploadPath)
- .then(() => {
- res.json({
- name: testFile.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath,
- md5: testFile.md5,
- size: testFile.size
- });
- })
- .catch(err => {
- res.status(500).send(err);
- });
- });
- app.all('/upload/single/withfields', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- if (!req.body) {
- return res.status(400).send('No request body found');
- }
- if (!req.body.firstName || !req.body.firstName.trim()) {
- return res.status(400).send('Invalid first name');
- }
- if (!req.body.lastName || !req.body.lastName.trim()) {
- return res.status(400).send('Invalid last name');
- }
- if (!req.body.email || !req.body.email.trim()) {
- return res.status(400).send('Invalid email');
- }
- let testFile = req.files.testFile;
- let uploadPath = path.join(uploadDir, testFile.name);
- testFile.mv(uploadPath, function(err) {
- if (err) {
- return res.status(500).send(err);
- }
- res.json({
- firstName: req.body.firstName,
- lastName: req.body.lastName,
- email: req.body.email,
- name: testFile.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath,
- md5: testFile.md5,
- size: testFile.size
- });
- });
- });
- app.all('/upload/single/truncated', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- if (req.files.testFile.truncated) {
- // status 400 to differentiate from ending the request in the on limit
- return res.status(400).send(`File too big`);
- }
- return res.status(200).send('Upload succeed');
- });
- app.all('/upload/multiple', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- let testFile1 = req.files.testFile1;
- let testFile2 = req.files.testFile2;
- let testFile3 = req.files.testFile3;
- let uploadPath1 = path.join(uploadDir, testFile1.name);
- let uploadPath2 = path.join(uploadDir, testFile2.name);
- let uploadPath3 = path.join(uploadDir, testFile3.name);
- if (!testFile1) {
- return res.status(400).send('testFile1 was not uploaded');
- }
- if (!testFile2) {
- return res.status(400).send('testFile2 was not uploaded');
- }
- if (!testFile3) {
- return res.status(400).send('testFile3 was not uploaded');
- }
- testFile1.mv(uploadPath1, function(err) {
- if (err) {
- return res.status(500).send(err);
- }
- testFile2.mv(uploadPath2, function(err) {
- if (err) {
- return res.status(500).send(err);
- }
- testFile3.mv(uploadPath3, function(err) {
- if (err) {
- return res.status(500).send(err);
- }
- //res.send('Files uploaded to ' + uploadDir);
- res.json([
- {
- name: testFile1.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath1,
- md5: testFile1.md5,
- size: testFile1.size
- },
- {
- name: testFile2.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath2,
- md5: testFile2.md5,
- size: testFile2.size
- },
- {
- name: testFile2.name,
- uploadDir: uploadDir,
- uploadPath: uploadPath2,
- md5: testFile2.md5,
- size: testFile2.size
- }
- ]);
- });
- });
- });
- });
- app.all('/upload/array', function(req, res) {
- if (!req.files) {
- return res.status(400).send('No files were uploaded.');
- }
- let testFiles = req.files.testFiles;
- if (!testFiles) {
- return res.status(400).send('No files were uploaded');
- }
- if (!Array.isArray(testFiles)) {
- return res.status(400).send('Files were not uploaded as an array');
- }
- if (!testFiles.length) {
- return res.status(400).send('Files array is empty');
- }
- let uploadResults = [];
- for (let i = 0; i < testFiles.length; i++) {
- let uploadPath = path.join(uploadDir, testFiles[i].name);
- testFiles[i].mv(uploadPath, function(err) {
- if (err) {
- return res.status(500).send(err);
- }
- uploadResults.push({
- name: testFiles[i].name,
- uploadDir: uploadDir,
- uploadPath: uploadPath,
- md5: testFiles[i].md5,
- size: testFiles[i].size
- });
- if (uploadResults.length === testFiles.length) {
- res.json(uploadResults);
- }
- });
- }
- });
- app.all('/fields/user', function(req, res) {
- if (!req.body) {
- return res.status(400).send('No request body found');
- }
- if (!req.body.firstName || !req.body.firstName.trim()) {
- return res.status(400).send('Invalid first name');
- }
- if (!req.body.lastName || !req.body.lastName.trim()) {
- return res.status(400).send('Invalid last name');
- }
- if (!req.body.email || !req.body.email.trim()) {
- return res.status(400).send('Invalid email');
- }
- res.json({
- firstName: req.body.firstName,
- lastName: req.body.lastName,
- email: req.body.email
- });
- });
- app.all('/fields/nested', function(req, res) {
- if (!req.body) {
- return res.status(400).send('No request body found');
- }
- if (!req.body.name || !req.body.name.trim()) {
- return res.status(400).send('Invalid name');
- }
- if (!req.body.hobbies || !req.body.hobbies.length == 2) {
- return res.status(400).send('Invalid hobbies');
- }
- res.json({
- name: req.body.name,
- hobbies: req.body.hobbies
- });
- });
- app.all('/fields/flattened', function(req, res) {
- if (!req.body) {
- return res.status(400).send('No request body found');
- }
- if (!req.body.name || !req.body.name.trim()) {
- return res.status(400).send('Invalid name');
- }
- if (!req.body['hobbies[0]'] || !req.body['hobbies[0]'].trim()) {
- return res.status(400).send('Invalid hobbies[0]');
- }
- if (!req.body['hobbies[1]'] || !req.body['hobbies[1]'].trim()) {
- return res.status(400).send('Invalid hobbies[1]');
- }
- res.json({
- name: req.body.name,
- 'hobbies[0]': req.body['hobbies[0]'],
- 'hobbies[1]': req.body['hobbies[1]']
- });
- });
- app.all('/fields/array', function(req, res) {
- if (!req.body) {
- return res.status(400).send('No request body found');
- }
- if (!req.body.testField) {
- return res.status(400).send('Invalid field');
- }
- if (!Array.isArray(req.body.testField)) {
- return res.status(400).send('Field is not an array');
- }
- res.json(req.body.testField);
- });
- return app;
- };
- module.exports = {
- setup,
- fileDir,
- uploadDir,
- clearUploadsDir
- };
|