Paperclip:S3上的PDF缩略图具有错误的content_type

3

我正在一个Rails应用程序中使用Paperclip 2.3.5来在Amazon S3上存储PDF文档。通过ImageMagick生成每个PDF的JPG缩略图。我在模型中使用了以下配置:

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'

但是存在一个问题:生成的缩略图上传到S3时,使用的content_type为"application/pdf",这是错误的,因为它是一个JPG文件(您可以通过类似Cyberduck的S3资源管理工具查看S3上文件的content_type)。对于原始的PDF文件,此content_type是正确的,但对于缩略图则不然。这会导致某些浏览器(如Chrome或Safari)无法内联显示缩略图。
注意:存储在我的数据库中的content_type(字段“file_content_type”)仍然是“application/pdf”,这仍然是正确的,因为它是原始文件的content_type。
如果缩略图的content_type应与原始文件不同,我该如何覆盖它?

这听起来像是Paperclip的一个Bug,为什么不在该问题上提交一个Bug报告呢? - Ariejan
有一个Paperclip的bug,pull request已经关闭:https://github.com/thoughtbot/paperclip/pull/414 - Alexander Presber
3个回答

3

这就是我们在 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

我遇到了类似的问题,这个方法对我有用。唯一的区别是我必须使用 before_#{attachment}_post_process 而不是 after_save - Zubin

1

我必须克服这个问题,虽然不是最优雅的解决方案,但我分叉了Paperclip并将补丁保存在自己的git存储库中 - https://github.com/svetzal/paperclip

它是Paperclip的直接替代品,只需将其放入您的environment.rb中

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


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接