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

时间:2021-07-16 21:07:48

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:

我在一个Rails应用中使用Paperclip 2.3.5来在Amazon S3上存储PDF文档。每个JPG缩略图都是由ImageMagick生成的。我在模型中使用这个配置:

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.

但是有一个问题:生成的缩略图被上传至S3,并带有content_type“application/pdf”,这是错误的,因为它是一个JPG(您可以通过类似Cyberduck的S3探测工具看到S3上文件的content_type)。对于原始的PDF文件,这个content_type是正确的,但不是缩略图。这在某些浏览器(如Chrome或Safari)中会造成麻烦,因为它们不显示内联缩略图。

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.

注意:在我的数据库中存储的content_type(字段“file_content_type”)是“application/pdf”,这仍然是正确的,因为它是原始文件的content_type。

How can I override the content_type for a thumbnail if it should be different from the original file?

如果一个缩略图应该与原始文件不同,那么如何重写这个content_type呢?

3 个解决方案

#1


0  

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

我必须克服这一点,这不是最优雅的解决方案,但我放弃了Paperclip,并在我自己的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'

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

#2


3  

This is how we fixed it on brighterplanet.com/research, which has pdf documents and png previews:

这是我们在brighterplanet.com/research.com上修复它的方法,该网站有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


0  

This is fixed in paperclip >= 2.7, as you can see here:

这是固定在paperclip >= 2.7,如你所见:

https://github.com/thoughtbot/paperclip/blob/v2.7/lib/paperclip/storage/s3.rb#L290

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.

写入S3的文件的mime类型是在上传之前确定的。

#1


0  

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

我必须克服这一点,这不是最优雅的解决方案,但我放弃了Paperclip,并在我自己的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'

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

#2


3  

This is how we fixed it on brighterplanet.com/research, which has pdf documents and png previews:

这是我们在brighterplanet.com/research.com上修复它的方法,该网站有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


0  

This is fixed in paperclip >= 2.7, as you can see here:

这是固定在paperclip >= 2.7,如你所见:

https://github.com/thoughtbot/paperclip/blob/v2.7/lib/paperclip/storage/s3.rb#L290

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.

写入S3的文件的mime类型是在上传之前确定的。