2

Our users have two ways of uploading images. One is through a simple HTML form and the other is through an iPhone app called Aurigma. We use Paperclip to process the images and store them on S3. Images that are uploaded with Aurigma end up having the wrong content-type, which causes them to open as an application.

I tried two solutions:

before_save :set_content_type

def set_content_type
  self.image.instance_write(:content_type,"image/png")
end

And:

before_post_process :set_content_type

def set_content_type
    self.image.instance_write(:content_type, MIME::Types.type_for(self.image_file_name).to_s)
end

It seems as if both solutions are ignored.

Using paperclip version 3.0.2, Aurigma version 1.3 and I'm uploading a screenshot from my iPhone. This is my paperclip configuration:

has_attached_file :image, {
   :convert_options => { :all => '-auto-orient' }, 
   :styles => {
     :iphone3 => "150x150",
     :web => "300x300"
   },
   :storage => :s3, 
   :bucket => ENV['S3_BUCKET'],
   :s3_credentials => {
      :access_key_id => ENV['S3_KEY'],
      :secret_access_key => ENV['S3_SECRET']
   }, 
   :path => "/pictures/:id/:style.:extension",
   :url => "/pictures/:id/:style.:extension"}
}
Sjors Provoost
  • 1,921
  • 1
  • 19
  • 34

2 Answers2

0

I just answered a similar question.

You need to do a copy to itself or use a pre-signed url with the content-type specified in the querystring.

Using the AWS SDK for Ruby and url_for:

object = bucket.objects.myobject
url = object.url_for(:read, :response_content_type => "image/png")
Community
  • 1
  • 1
Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85
  • Thanks, but I'm not sure how to implement this. I just added my Paperclip configuration to my question. Paperclip handles interaction with S3, so I don't know to access the bucket and find my object. Or do you suggest that I directly access the bucket with the S3 gem, somehow retrieve the image and then modify the content type? – Sjors Provoost May 04 '12 at 12:42
  • @SjorsProvoost - To be clear I am not a ruby developer so you'll have to fill in the blanks... When uploading the files you set the `content-type` using Aurigma or Paperclip - I would be suprised if both didn't have options to set this. It looks like Paperclip sets it based on the extension but this might help overriding it if necessary: http://stackoverflow.com/questions/2581157/overriding-content-type-for-rails-paperclip-plugin. Otherwise, I'd use the AWS SDK directly to copy the file within S3 (to itself) and set the correct `content-type`. – Geoff Appleford May 04 '12 at 13:35
  • Or use the ASW SDK to generate the urls that you use on your site and set the `content-type` in the url. This doesn't change what is set on S3 but simply serves the file with a different `content-type`. I suspect that Paperclip probably has an option to do this for you too... Sorry I cant be more specific. – Geoff Appleford May 04 '12 at 13:37
  • Thanks, I should be able to figure it out. That link you gave me is actually one of the things I tried before; something seems to have changed in the Paperclip internals in the mean time. – Sjors Provoost May 05 '12 at 14:56
0

As far as I understand first you upload all the files from client devices to your own server (through Aurigma Up) and then these files are uploaded to Amazon S3. I have similar problem trying to change content-type on client device. This is not possible. You should send files on your server and then change content type before uploading files to S3.

vachevski
  • 34
  • 3