我试图解决在主动存储其中存储的文件的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
尝试用到位Rails.application.config.to_prepare
初始化事件的after_initialize
。
更多信息 :