我在Rails应用程序中使用Paperclip2.3.5在亚马逊S3上存储PDF文档。对于每个PDF,ImageMagick都会生成一个JPG缩略图。Im‘在模型中使用此配置:
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'但是有一个问题:生成的缩略图是通过应用程序“content_type /pdf”上传到S3的,这是错误的,因为它是一个JPG (你可以在S3上使用S3浏览工具看到文件的content_type )。对于原始的PDF文件,此content_type是正确的,但对于缩略图则不正确。这会在一些浏览器(如Chrome或Safari)中造成麻烦,因为它们不会以内联方式显示缩略图。
注意:存储在我的数据库(字段"file_content_type")中的应用程序是“content_type /pdf”,这仍然是正确的,因为它是原始文件的content_type。
如果缩略图应该与原始文件不同,我如何覆盖缩略图的content_type?
发布于 2010-11-28 20:05:44
我不得不克服这一点,这不是最好的解决方案,但我使用了回形针并将补丁保存在我自己的git repo - https://github.com/svetzal/paperclip中。
它是回形针的直接替代品,只需放入你的environment.rb即可
gem 'twm_paperclip',:lib =>‘回形针’
发布于 2011-04-29 05:03:27
这就是我们如何在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发布于 2013-01-21 21:05:57
这在回形针>= 2.7中得到了解决,您可以在此处看到:
https://github.com/thoughtbot/paperclip/blob/v2.7/lib/paperclip/storage/s3.rb#L290
写入S3的文件的mime类型是在上传之前明确确定的。
https://stackoverflow.com/questions/4203279
复制相似问题