new rails plugin: mimetype_fu
Written by matt on June 13th, 2007
mimetype_fu/ is a new plugin I just wrote. It's simple and it can be really useful if you need to get the mime type of a file already on your server.
During one of my project, I add to migrate old assets from a legacy system to a new Rails app. The new app uses attachment fu and even though techno weenie did an amazing job, attachmentfu validation is based on the content type. Afu gets the content type coming from the CGI query.
Unit test has a helper faking this process but in real life, if you use a Flash uploader (Flash doesn't give you the proper mime type/content type) or if you want to migrate files, the attachment_fu validation won't work for you.
The solution is simple: mimetype_fu/
mimetype_fu/ extends the File class and is really easy to use:
File.mime_type?(@file)
Check it out http://code.google.com/p/mimetype-fu/
Expect a post showing how a ninja would use the mimetypefu / attachmentfu combo :)
Comments
-
Wish I had stumbled upon your plugin before I began hacking attachment_fu to do the same thing for patronsocialclub.com. I ended up changing the uploaded_data function to look for Flash's bogus "application/octet-stream" content-type and returning the correct type using: def determine_content_type(filename) extension=filename.split('.').last.downcase mime_mappings = { "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "zip" => "application/zip" } return mime_mappings[extension] end I'm still having trouble getting Flash file uploads to work on OSX. They work fine on a PC. I ran across this blog post but am not sure its the same issue (http://www.oscartrelles.com/archives/filereference_bug_in_flash_player_9_for_mac_os). Speaking of Flash and Rails, I ran across WebORB this morning. It suppports Flex RPC calls. I blogged about it at jeffontech.vox.com. Thanks for your plugin. I can't wait to try it out. Cheers, J.
-
I've been working on a flash photo upload application using attachment_fu and swfupload and run into the mimetype problem. Replacing in attachment_fu.fb,
self.content_type = file_data.content_type
withself.content_type = !(file_data.content_type.strip == "application/octet-stream") ? file_data.content_type : File.mime_type?(file_data.original_filename)
does the job perfectly. Thanks for your plugin. Saved me lots of times.


