diff --git a/bin/app.js b/bin/app.js
index e1abc76..845f324 100644
--- a/bin/app.js
+++ b/bin/app.js
@@ -8,6 +8,27 @@ const _path = require("path");
const config = require('./config');
const utils = require('./utils');
+/**
+ * @desc move file in request
+ */
+const mvFiles = async (path, files) => {
+ const selectedFiles = Array.isArray(files) ? files : [files];
+ let mvTask = [];
+ for (let i = 0; i < selectedFiles.length; i++) {
+ const selectedFile = selectedFiles[i];
+ const selectedFileName = new Buffer(selectedFile.name, 'ascii').toString('utf8');
+ const uploadPath = _path.resolve(__dirname, path) + '/' + selectedFileName;
+ utils.debugLog(`upload path: ${uploadPath}`);
+ mvTask.push(new Promise((resolve, reject) => {
+ selectedFile.mv(uploadPath).then((err) => err ? reject({ uploadPath, err }) : resolve({ uploadPath }));
+ }));
+ }
+ const mvRes = await Promise.allSettled(mvTask);
+ const fulfilledList = mvRes.filter(({ status }) => status === 'fulfilled');
+ const rejectedList = mvRes.filter(({ status }) => status === 'rejected');
+ return { fulfilledList, rejectedList };
+}
+
const start = ({ port, path, receive, onStart, postUploadRedirectUrl, shareAddress }) => {
const app = express();
@@ -29,32 +50,22 @@ const start = ({ port, path, receive, onStart, postUploadRedirectUrl, shareAddre
res.send(form.toString().replace(/\{shareAddress\}/, shareAddress));
});
- app.post('/upload', (req, res) => {
+ app.post('/upload', async (req, res) => {
if (!req.files || Object.keys(req.files).length === 0) {
res.status(400).send('No files were received.');
return;
}
-
- const selectedFile = req.files.selected;
-
- const selectedFileName = new Buffer(selectedFile.name, 'ascii').toString('utf8');
- const uploadPath = _path.resolve(__dirname, path) + '/' + selectedFileName;
- utils.debugLog(`upload path: ${uploadPath}`);
-
- selectedFile.mv(uploadPath).then(err => {
- if (err) {
- return res.status(500).send(err);
- }
-
- console.log(`File recevied: ${uploadPath}`)
-
- res.send(`
-
- `);
- });
+ const { fulfilledList, rejectedList } = await mvFiles(path, req.files.selected);
+ const fulfilledMsg = fulfilledList.map(({ value: { uploadPath } }) => uploadPath).join(',\n');
+ const rejectedMsg = rejectedList.map(({ reason: { uploadPath } }) => uploadPath).join(',\n');
+ const successMsg = fulfilledList.length !== 0 ? `Shared at \n ${fulfilledMsg}` : ""
+ const errorMsg = rejectedList.length !== 0 ? `${successMsg ? `\n\r`: ""}Sharing failed: \n ${rejectedMsg}` : "";
+ res.send(`
+
+ `);
});
}
diff --git a/bin/receive-form.html b/bin/receive-form.html
index d745ef8..a5e1697 100644
--- a/bin/receive-form.html
+++ b/bin/receive-form.html
@@ -55,7 +55,7 @@
View directory📁
-
+