From a6fe33cfcf4ac3f837ae1ab0a9b48507cd361dbc Mon Sep 17 00:00:00 2001 From: yuanx <1220998011@qq.com> Date: Sat, 18 Feb 2023 21:22:09 +0800 Subject: [PATCH] feat: Support upload multiple files --- bin/app.js | 55 ++++++++++++++++++++++++++----------------- bin/receive-form.html | 2 +- 2 files changed, 34 insertions(+), 23 deletions(-) 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📁 - +