【问题标题】:How to set content_type for Fog storage in Paperclip如何在回形针中为雾存储设置 content_type
【发布时间】:2025-11-22 22:15:03
【问题描述】:

我正在使用回形针通过 Fog 将文件上传到 S3。但是,我在为文本文件设置正确的content_type 时遇到问题。我搜索了互联网上的所有地方,但我没有找到解决这个问题的方法。您知道如何覆盖雾存储的 content_type 吗?

谢谢

【问题讨论】:

  • 'text/plain' 呢?
  • 看起来很像this question
  • @JohnC 默认为“text/plain”。我想让它成为“文本/纯文本;charset=utf-8”。您链接中的问题不同,因为我不想验证内容类型,而是想在保存时更改内容类型

标签: ruby-on-rails amazon-s3 paperclip fog


【解决方案1】:

我通过覆盖回形针中的 UploadedFileAdapter 自己设法解决了这个问题。我将这个类中的函数更新为这样的东西

def determine_content_type
  content_type = @target.content_type.to_s.strip
  if content_type_detector
    content_type = content_type_detector.new(@target.path).detect
  end

  #NOTE: override paperclip, need to set the utf-8 for text file
  content_type = "text/plain; charset=utf-8" if content_type == "text/plain"

  content_type
end

【讨论】:

    最近更新 更多