主动存储 - 在初始化错误解决不工作

问题描述 投票:1回答:1

我试图解决在主动存储其中存储的文件的MIME类型设置不正确的已知问题,没有覆盖的能力。

https://github.com/rails/rails/issues/32632

这已经在Rails中的master分支得到解决,但它不似乎尚未释放(项目目前使用的5.2.0)。为此我试图解决使用中的问题提供了一个评论的问题:

在一个新的初始化(\config\initializers\active_record_fix.rb):

Rails.application.config.after_initialize do
  # Defeat the ActiveStorage MIME type detection.
  ActiveStorage::Blob.class_eval do
    def extract_content_type(io)
      return content_type if content_type
      Marcel::MimeType.for io, name: filename.to_s, declared_type: content_type
    end
  end
end

我在处理和使用delayed_jobs存储后台作业中的zip文件。初始化程序似乎并没有被获取调用。我已经重新启动服务器。我在本地运行的项目中使用heroku local处理后台作业。

这里是存储文件的代码:

file.attach(io: File.open(temp_zip_path), filename: 'Download.zip', content_type: 'application/zip')

任何想法,为什么上面的代码是不工作?主动存储喜欢有点随机决定该ZIP文件是PDF格式,并保存内容类型application\pdf。无关,尝试手动覆盖content_type后附着不工作:

file.content_type = 'application/zip'
file.save # No errors, but record doesn't update the content_type
ruby-on-rails rails-activestorage
1个回答
1
投票

尝试用到位Rails.application.config.to_prepare初始化事件的after_initialize

更多信息 :

© www.soinside.com 2019 - 2024. All rights reserved.