Andy Harvey
Andy Harvey

Reputation: 12653

How to determine multipart/form-data filetypes using Ruby?

I'm using the Griddler gem in a Rails 3.2 app to handle incoming emails with attachments.

The documentation indicates that attached files are multipart/form-data files.

Is there an easy ruby way to interrogate these files and determine their filetype? I need to handle different filetypes using different methods.

Upvotes: 1

Views: 114

Answers (1)

fontno
fontno

Reputation: 6852

According to this rails guide under section 5 'Uploading Files'

The object in the params hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an original_filename attribute containing the name the file had on the user’s computer and a content_type attribute containing the MIME type of the uploaded file.

so you could interrogate uploaded_io.content_type and use different methods to work with whatever content_type is.

Hope this gets you started in the right direction

Upvotes: 1

Related Questions