Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Origin feat upload multiple files #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions bin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(`
<script>
window.alert('Shared at ${uploadPath}');
window.location.href = '${postUploadRedirectUrl}';
</script>
`);
});
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(`
<script>
window.alert(\`${successMsg}${errorMsg}\`);
window.location.href = '${postUploadRedirectUrl}';
</script>
`);
});
}

Expand Down
2 changes: 1 addition & 1 deletion bin/receive-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<label id="share" for="file-input" class="btn">Share file🚀</label>
</div>
<a href="{shareAddress}" target="_blank">View directory📁<a/>
<input id="file-input" type="file" name="selected" style="visibility:hidden; display:none;"/>
<input id="file-input" type="file" name="selected" multiple=true style="visibility:hidden; display:none;"/>
</form>
</body>

Expand Down