Question

I gonna upload files to rackspace(video, audio and images) in rails with paperclip or carrierwave, I need to Know the kind of file, to show in the view with image_tag, or video_tag or audio_tag, rackspace tell me the kind of file? or I have to store in my database? thanks

Was it helpful?

Solution 2

Even if rackspace would tell you the file type, you don't want it to, since it would take so long to run roundtrips from your server to theirs.

My code examples below assume carrierwave, but I'm sure paperclip has similar options. Two options:

  1. Interpret the file extension

Something like: File.extname(user.avatar), which you then have to interpret however you like.

  1. Record & interpret the mime type.

The carrierwave readme explains how to get carrierwave to calculate it in the first place, and then you should probably store it to your database manually or using carrierwave-meta. Then user.avatar.content_type would be something like image/jpeg which you could easily interpret as a particular file type.

OTHER TIPS

You can query/set the file type by using the 'content_type' function located in the 'ruby-cloudfiles' library.

See here: https://github.com/rackerlabs/ruby-cloudfiles/blob/master/lib/cloudfiles/storage_object.rb#L80-L82

Something like this should work for creating the object:

container = conn.create_container('new_container')
obj = container.create_object('new_obj.txt')
obj.load_from_filename('./obj.txt')
obj.content_type = 'text/plain'

And to retrieve the object:

obj = container.object('new_obj.txt')
puts obj.content_type   # text/plain
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top