How to fix content-type for Aurigma uploads to S3? - ruby-on-rails

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"}
}

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")

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.

Related

Paperclip validation issue on production

I have a Problem when I deploy my application on google cloud I get this error
has contents that are not what they are reported to be
Locally it works fine! I already tried to using the command_path. So I really don't know what I have to do next...
This is my model
has_mongoid_attached_file :image,
:styles => { :large => "380x380!" , :medium => "240x240", :small => "120x120!" },
:storage => :fog,
:fog_public => true,
:fog_directory => 'XXXX',
:path => "images/:id/:style/:basename.:extension",
:fog_credentials => { :provider => 'Google',
:google_storage_access_key_id => 'XXXXX',
:google_storage_secret_access_key => 'XXXXX'}
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Thank you for your efforts. I hope you guys can help me
Okay I found a result. I just created a initializers/paperclip.rb file
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
Right now it work's perfectly for me.
If you have problems with ImageMagick on App Engine using Rails see this link
That issue occurs because the content-type discovered from file command returns empty string.
Actually system is not able to find the file executable so a exception is raised and empty string is returned back.
Check the code below
begin
Paperclip.run("file", "-b --mime :file", :file => '/tmp/RackMultipart20160826-15649-kwvnq2.png').split(/[:;]\s+/).first
rescue Cocaine::CommandLineError
""
end
Solution:-
Add below line in you initializer file.
Paperclip.options[:command_path] = '/usr/bin'
Looks like Google Cloud can't determine MIME type of uploaded files.
You can map file extensions to types in you initializer (application.rb, production.rb or create initializers/paperclip.rb)
Paperclip.options[:content_type_mappings] = {
:jpg => "image/jpeg",
:png => "image/png",
:gif => "image/gif"
}
But this way spoofing check won't be performed for image files.
I know I am late to the party, but in working with a legacy RoR system, I ran into this issue. The problem arose in setting the app up in Docker. Ultimately paperclip calling imagemagick was attempting to use file to identify mime-type and the minimal Docker did not have it installed. apt-get install file fixed it.

Resource interpreted as Image but transferred with MIME type application/xml aws

I am using the paperclip and aws-sdk gems to upload avatars to AWS, but when I render the photos in my application, the console gives me the following response:
Resource interpreted as Image but transferred with MIME type application/xml aws
Below are my codes:
development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => "appname-development",
:access_key_id => "##########",
:secret_access_key => "##########"
}
user.rb
attr_accessible :profile_picture
has_attached_file :profile_picture,
:styles => {
:big => '200x200>',
:small => '50x50#'
}
show.html.erb
<%= image_tag #user.profile_picture.url(:small) %>
I went to the S3 console and checked my photos, they all have the content-type of image/jpeg.
As I browsed around the internet, it looks like no one has really encountered this issue before, does anyone know what is going on?
Thank you.
This means, that something you get from S3 is not an image but often an error message in xml.
'puts' link to your photo into console and paste it to browser. You will see, that you have got not photo but an error message.

setting content-type for mp4 files on s3

I am adding user uploaded videos to my RoRs site with the help of the paperclip gem and s3 storage. For some reason that I can't figure out, whenever a user uploads an mp4 file, s3 sets content-type for that file as application/mp4 instead of video/mp4.
Note that I have registered mp4 mime type in an initializer file:
Mime::Type.lookup_by_extension('mp4').to_s
=> "video/mp4"
Here is the relevant part of my Post model:
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename"
validates_attachment_content_type :video,
:content_type => ['video/mp4'],
:message => "Sorry, this site currently only supports MP4 video"
What am I missing in my paperclip and/or s3 set-up.
####Update#####
For some reason that is beyond my knowledge of Rails, my default mime types for mp4 contained files is the following:
MIME::Types.type_for("my_video.mp4").to_s
=> "[application/mp4, audio/mp4, video/mp4, video/vnd.objectvideo]"
So, when paperclip send an mp4 file to s3, it seems to identify the file's mime type as the first default, "application/mp4". That is why s3 identifies the file as having a content-type of "application/mp4". Because I want to enable streaming of these mp4 files, I need paperclip to identify the file as having a mime type of "video/mp4".
Is there a way to modify paperclip (maybe in a before_post_process filter) to allow for this, or is there a way to modify rails through an init file to identify mp4 files as being "video/mp4". If I could do either, which way is best.
Thanks for your help
Turns out that I needed to set a default s3 header content_type in the model. This isn't the best solution for me because at some point I might start allowing video containers other than mp4. But it gets me moving on to the next problem.
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename",
:s3_headers => { "Content-Type" => "video/mp4" }
I did the following:
...
MIN_VIDEO_SIZE = 0.megabytes
MAX_VIDEO_SIZE = 2048.megabytes
VALID_VIDEO_CONTENT_TYPES = ["video/mp4", /\Avideo/] # Note: The regular expression /\Avideo/ will match anything that starts with "video"
has_attached_file :video, {
url: BASE_URL,
path: "video/:id_partition/:filename"
}
validates_attachment :video,
size: { in: MIN_VIDEO_SIZE..MAX_VIDEO_SIZE },
content_type: { content_type: VALID_VIDEO_CONTENT_TYPES }
before_validation :validate_video_content_type, on: :create
before_post_process :validate_video_content_type
def validate_video_content_type
if video_content_type == "application/octet-stream"
# Finds the first match and returns it.
# Alternatively you could use the ".select" method instead which would find all mime types that match any of the VALID_VIDEO_CONTENT_TYPES
mime_type = MIME::Types.type_for(video_file_name).find do |type|
type.to_s.match Regexp.union(VALID_VIDEO_CONTENT_TYPES)
end
self.video_content_type = mime_type.to_s unless mime_type.blank?
end
end
...

Paperclip and S3: Multiple thumbnail sizes not saving

I'm using Paperclip to handle image uploads for my Rails app, and it's working great when I use system storage: multiple thumbnail sizes ("styles" in Paperclip parlance) are saved to file, and I can access any of them by passing the style name to the url method.
When I set up the app to store images on S3 (using the aws-s3 gem), however, only one image is stored in my S3 bucket. For what it's worth, only the last style listed is stored. So, if in my model, I've got:
has_attached_file :photo,
:styles => { :large => "1000x1000>",
:medium => "600x600>",
:thumb => "200x200>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => AppConstants.bucket,
:path => "pictures/:id/:filename"
Only the "thumb" size will be saved to S3.
Has anybody encountered a similar problem? How can I fix this?
I'm not sure why this works locally, but you didn't specify :style in your path declaration.

Paperclip: PDF thumbnail has wrong content_type on S3

I'm using Paperclip 2.3.5 within a Rails app to store PDF documents on Amazon S3. For every PDF a JPG thumbnail is generated by ImageMagick. Im' using this configuration in the model:
has_attached_file :file,
:styles => { :thumb => { :geometry => "200x200>",
:format => :jpg
} },
:whiny => false,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:s3_permissions => 'authenticated-read',
:s3_headers => { 'Expires' => 1.year.from_now.httpdate },
:url => "s3.amazonaws.com",
:path => "documents/:id/:style/:basename.:extension",
:bucket => 'mybucket'
But there is problem: The generated thumbnail is uploaded to S3 with the content_type "application/pdf", which is WRONG, because it's a JPG (you can see the content_type of a file on S3 with a S3 exploring tool like Cyberduck). For the original PDF file this content_type is correct, but not for the thumbnail. This causes trouble in some browsers (e.g. Chrome or Safari) which don't show the thumbnail inline.
Beware: The content_type stored in my database (field "file_content_type") is "application/pdf", which is still correct, because it's the content_type for the original file.
How can I override the content_type for a thumbnail if it should be different from the original file?
This is how we fixed it on brighterplanet.com/research, which has pdf documents and png previews:
has_attached :pdf_document,
:storage => :s3,
# [... other settings ...]
# PDFs work better in Windows 7 / IE if you give them content-type: attachment
:s3_headers => { 'Content-Disposition' => 'attachment' },
:styles => { :preview => { :geometry => '135', :format => :png } }
after_save :fix_thumbnail
def fix_thumbnail(force = false)
# application/pdf and application/x-pdf have both been seen...
return unless force or pdf_document_content_type.include?('pdf')
# set content type and disposition
s3 = AWS::S3.new(YAML.load(File.read("#{RAILS_ROOT}/config/aws_s3.yml")))
t = s3.buckets[PAPERCLIP_BUCKET].objects[pdf_document.path(:thumbnail)]
content = t.read
t.write(:data => content, :content_type => 'image/png', :content_disposition => 'inline', :acl => :public_read)
nil
end
I had to overcome this, not the most elegant solution but I forked Paperclip and hold the patch in my own git repo - https://github.com/svetzal/paperclip
It is a direct replacement for Paperclip, just put in your environment.rb
gem 'twm_paperclip', :lib => 'paperclip'
This is fixed in paperclip >= 2.7, as you can see here:
https://github.com/thoughtbot/paperclip/blob/v2.7/lib/paperclip/storage/s3.rb#L290
the mime-type of the file that is written to S3 is determined specifically before uploading.

Resources