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

When uploading a file, path does not include extension #29

Open
jasonlally opened this issue Jun 29, 2013 · 5 comments
Open

When uploading a file, path does not include extension #29

jasonlally opened this issue Jun 29, 2013 · 5 comments
Labels

Comments

@jasonlally
Copy link

When I attach an image, this is what req.file.image returns. The path does not include an extension although that seems to be where mongoose-attachments is grabbing the extension. Would it be better to grab it from name? Will that always include the extension?

Here's a returned file image object example:

{ domain: null,
  _events: null,
  _maxListeners: 10,
  size: 39907,
  path: '/var/folders/0p/j35t5_z11zj_qv9nnqqzvmtw0000gr/T/65bd99d6a382b583ad8e9b2c39728b76',
  name: 'image-name.jpg',
  type: 'image/jpeg',
  hash: null,
  lastModifiedDate: Sat Jun 29 2013 16:43:31 GMT-0700 (PDT),
  _writeStream:
   { domain: null,
     _events: null,
     _maxListeners: 10,
     path: '/var/folders/0p/j35t5_z11zj_qv9nnqqzvmtw0000gr/T/65bd99d6a382b583ad8e9b2c39728b76',
     fd: 24,
     writable: false,
     flags: 'w',
     encoding: 'binary',
     mode: 438,
     bytesWritten: 39907,
     busy: false,
     _queue: [],
     _open: [Function],
     drainable: true,
     flush: [Function],
     write: [Function],
     end: [Function],
     destroy: [Function],
     destroySoon: [Function],
     pipe: [Function],
     setMaxListeners: [Function],
     emit: [Function],
     addListener: [Function],
     on: [Function],
     once: [Function],
     removeListener: [Function],
     removeAllListeners: [Function],
     listeners: [Function] },
  open: [Function],
  toJSON: [Function],
  write: [Function],
  end: [Function],
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }
@bithavoc
Copy link
Member

Hi,

That seems to be the request, is the uploading including the extension?

@jasonlally
Copy link
Author

I believe so, I'm using a standard upload form and passing via a route configured in express to a controller that grabs the file from req.files.image. I'm working on my local dev machine.

Here's the controller for create:

exports.create = function (req, res) {
  var person = new Person(req.body)
  person.uploadAndSave(req.files.image, function (err) {
    if (!err) {
      req.flash('success', 'Successfully added new person!')
      return res.redirect('/people')
    }
  })
}

Here's the upload and save method on the model:

uploadAndSave: function (image, cb) {
    if (!image || image.size == 0) return this.save(cb)

    var self = this

    self.attach('image', image, function(err){
      if(err) return cb(err)
      self.save(cb)
    })
  }

@jasonlally
Copy link
Author

I modified the mongoose-attachments locally for now to grab the extension from the 'name,' but I'm sure that's probably problematic, but it works for now so I can keep moving on the application. If it is an actual bug, I could work on a fix, but it's probably something I've misconfigured.

@nuarhu
Copy link
Contributor

nuarhu commented Jul 2, 2013

You did not specify why you need the extension, so I'm describing how I'm doing it. I'm not using the extension provided during upload as this is user input or might not even be present.

To check for the type of the uploaded image I'm running it through the 'mime-magic' module which returns the mime type like image/png (which allows me to reject all non-image/* type uploads). From that output I parse the subtype - in this case png and store it as the extension field in mongo.

@nuarhu
Copy link
Contributor

nuarhu commented Aug 23, 2013

@jasonlally , if you have a working solution - would you mind committing a pull request?

Thanks!

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

No branches or pull requests

3 participants