Active Storage - 初始化程序中的错误解决方法不起作用
Active Storage - Bug workaround in initializer not working
我正在尝试解决 Active Storage 中的一个已知问题,即存储文件的 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')
知道上面的代码为什么不起作用吗? Active Storage 喜欢随机决定此 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
初始化事件。
更多信息:
我正在尝试解决 Active Storage 中的一个已知问题,即存储文件的 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')
知道上面的代码为什么不起作用吗? Active Storage 喜欢随机决定此 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
初始化事件。
更多信息: