rkusa / koa-formidable

Formidable middleware for Koa
MIT License
25 stars 2 forks source link

the events of formidable do not work. #7

Closed violinxliu closed 7 years ago

violinxliu commented 7 years ago
  console.log('start upload');
  let newPath = '';
  const form = new require('formidable').IncomingForm(); 
  form.encoding = 'utf-8'; 
  form.uploadDir = AVATAR_PATH; 
  form.keepExtensions = true; 
  form.maxFieldsSize = 2 * 1024 * 1024; 
  form.on('fileBegin', function(name, file) {
      console.log(name, file);
      console.log('type:', file.fulAvatar.type);
      const avatarName = UUID.v4() + '.' + extName;
      newPath = form.uploadDir + avatarName;
      file.path = newPath;
  });
  form.on('aborted', function() {
    console.log('aborted');
  });
  form.on('end', function() {
    console.log('end');
  });
  form.on('error', function(err) {
    console.log(error);
  });
  const result = yield formidable.parse(form, this.request);

the 'fileBegin', 'aborted', 'end', all of these events do not work. How to fix it?

rkusa commented 7 years ago

When you do a npm list, do you have multiple entries for formidable?

violinxliu commented 7 years ago

No.maybe you should have a try or can you show us a demo.

rkusa commented 7 years ago

I have some ideas for why it does not work. I would give a try, however, I do not have koa-formidable in use currently and I am not sure if I find some time to setup a working example in next couple of days.

violinxliu commented 7 years ago

Anyway, Thank you for your reply. I will try continue to fix this, even I have finish my problem by any other solution. By the way, do you have Gitter account, maybe we can talk more convenient with this IM.

rkusa commented 7 years ago

I can be found on matrix.org/riot.im (as rkusa), which has a Gitter bridge, but I have never used it.

I think the issue could be the following lines https://github.com/rkusa/koa-formidable/blob/master/index.js#L21-L23

    var form = opts instanceof formidable.IncomingForm
      ? opts
      : new formidable.IncomingForm(opts)

Because, if the require('formidable') of koa-formidable receives a different IncomfingForm then your require('formidable') does, then koa-formidable creates a new instance instead of using yours (because instanceof fails). If this is the case, changing the instanceof statement into something like typeof opts.parse === 'function', may fix the issue.

violinxliu commented 7 years ago

ok, I am busy now. later I will try. well, Thank you and have a nice day.

violinxliu commented 7 years ago

so excited to tell you that your idea is right.

violinxliu commented 7 years ago

/ fix this issue, just like this. I had proved this is right on my project. */ var form = typeof opts.parse === 'function' ? opts : new formidable.IncomingForm(opts);

violinxliu commented 7 years ago
 var form = opts && opts.parse && typeof opts.parse === 'function'
      ? opts
      : new formidable.IncomingForm(opts) 

The code like this will be more better.

rkusa commented 7 years ago

Thanks for checking back!

May I ask you a favor? Could you check if the following works, too:

var form = (opts && typeof opts === 'function' && opts.constructor.name === 'IncomingForm')
      ? opts
      : new formidable.IncomingForm(opts)

This would be a more correct solution. If it works, I would like to integrate this change and publish a new version

violinxliu commented 7 years ago

your code has something wrong. because opts is an object. well, I think the code like this is fine.

var form = (opts && typeof opts === 'object' && typeof opts.parse === 'function' && opts.constructor.name === 'IncomingForm')
          ? opts
          : new formidable.IncomingForm(opts)
rkusa commented 7 years ago

Ah right.

var form = (typeof opts === 'object' && opts.constructor.name === 'IncomingForm')
      ? opts
      : new formidable.IncomingForm(opts)

should do the trick then. I'll update the library. Thanks for testing

violinxliu commented 7 years ago

Great! when are you going to publish the new version? er, Can I make a suggestion? I think you should update the document about how to use the events of formidable on README.md. because it will be more detailed for developer to use your lib.

rkusa commented 7 years ago

I've release 1.1.0 and added an example to the README. Next step would probably to release a version compatible with the most recent Koa using async/await... but I need to setup either tests or an example application first ...

violinxliu commented 7 years ago

Great! I had saw it. In order to compatible with Koa2, you have to do so. Everything takes time, so there's no hurry, just follow the plan you make step by step.