Skip to content
Open
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions upload/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ app.use(async function(ctx, next) {
// ignore non-POSTs
if ('POST' != ctx.method) return await next();

const file = ctx.request.body.files.file;
const reader = fs.createReadStream(file.path);
const stream = fs.createWriteStream(path.join(os.tmpdir(), Math.random().toString()));
reader.pipe(stream);
console.log('uploading %s -> %s', file.name, stream.path);
const files = ctx.request.body.files.file;
if (files instanceof Array) {
Copy link

@fl0w fl0w May 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with Array.isArray here, even better would be to not special-case and DRY streaming (e.g. always assume arrays).

files.forEach(function(file, index) {
const reader = fs.createReadStream(file.path);
const stream = fs.createWriteStream(path.join(os.tmpdir(), Math.random().toString()));
reader.pipe(stream);
console.log('uploading %s -> %s', file.name, stream.path);
});
} else {
const file = files;
const reader = fs.createReadStream(file.path);
const stream = fs.createWriteStream(path.join(os.tmpdir(), Math.random().toString()));
reader.pipe(stream);
console.log('uploading %s -> %s', file.name, stream.path);
}

ctx.redirect('/');
});
Expand Down