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

TypeError when checking mime-types #501

Closed
thinkverse opened this issue Mar 27, 2019 · 3 comments
Closed

TypeError when checking mime-types #501

thinkverse opened this issue Mar 27, 2019 · 3 comments

Comments

@thinkverse
Copy link

Hello everyone.
I have an issue when trying to check for wrong mime-types, I have a simple function that checks if the mime-type is included in an array of mime-types, and responds with an 404 is type doesn't match, and otherwise I let formidable handle the file.

const isInvalidMimeType = fileMimeType => {
  let mimeTypes = [...]

  return (!mimeTypes.includes(fileMimeType)) ? true : false
}

form.onPart = function (part) {
  if (part.filename) {
    if (isInvalidMimeType(part.mime)) {
      return res.status(404).json({ 'status': 404, 'message': 'Wrong file type'})
    } else {
      form.handlePart(part)
    }
  }
}

My issue is that if it is the wrong filetype, formidable still tries to upload the file, so I get;

TypeError: Cannot read property 'path' of undefined

That error in turn kills the entire application, is there anyway to let formidable know that the file isn't suppose to be uploaded and just return the response?

You can find the whole project in here in this repo, repo:uploadz

@tunnckoCore
Copy link
Member

tunnckoCore commented Mar 27, 2019

is there anyway to let formidable know that the file isn't suppose to be uploaded and just return the response?

I'm pretty sure we discussed this in few issues for aborting/canceling the upload.

But for now, req.destroy() from #119 (comment) and #471

@thinkverse
Copy link
Author

Thank you @tunnckoCore, returning req.destory() fixed the issue of the app destroying itself.

form.onPart = function (part) {
  if (part.filename) {
    if (isInvalidMimeType(part.mime)) {
      res.status(404).json({ 'status': 404, 'message': 'Wrong file type'})
      return req.destroy()
    } else {
      form.handlePart(part)
    }
  }
}

The fixed code it anyone wants it.

@tunnckoCore
Copy link
Member

Great to hear that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants