Clip: PDF thumbnail has invalid content_type on S3

I use Paperclip 2.3.5 in a Rails application to store PDFs on Amazon S3. For each PDF, a JPG thumbnail is created 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 a problem: the generated sketch is uploaded to S3 using the content_type "application / pdf", which is WRONG because it is a JPG (you can see the content_type of the file on S3 using the S3 search tool such as Cyberduck). For the original PDF, this content_type is correct, but not for the thumbnail. This causes problems in some browsers (e.g. Chrome or Safari) that do not display the inline thumbnail.

: Content_type, ( "file_content_type" ), "application/pdf", - , content_type .

content_type , ?

+3
3

, , "" git https://github.com/svetzal/paperclip

Paperclip, . rb

gem 'twm_paperclip',: lib = > 'paperclip'

0

brighterplanet.com/research, pdf png:

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
+3

This is fixed in the>> 2.7 folder, 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 recorded in S3 is determined specifically before downloading.

0
source

Source: https://habr.com/ru/post/1775383/


All Articles