gpt4 book ai didi

ruby-on-rails - 回形针:PDF 缩略图在 S3 上有错误的 content_type

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:49 25 4
gpt4 key购买 nike

我在 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 的内容类型为“application/pdf”,这是错误的,因为它是 JPG(您可以使用 Cyber​​duck 等 S3 探索工具查看 S3 上文件的内容类型).对于原始 PDF 文件,此 content_type 是正确的,但对于缩略图则不正确。这会在某些不显示内联缩略图的浏览器(例如 Chrome 或 Safari)中造成问题。

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

如果缩略图与原始文件不同,我如何覆盖缩略图的 content_type?

最佳答案

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

关于ruby-on-rails - 回形针:PDF 缩略图在 S3 上有错误的 content_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com