问题描述
问题
不推荐使用将set_content_type
方法添加到上载器的 CarrierWave::MimeTypes
.除其他外,此方法将尝试检测给定类型的内容类型(application/octet-stream
或binary/octet-stream
).
弃用消息对于CarrierWave::MimeTypes
说:
但是,如果定义了该类是通用类还是非通用类,则此类始终返回现有的内容类型. 在此处查看代码.. >
当前解决方案
目前,我们正在通过清除内容类型(如果它是通用类型)并让库正确检测它来手动进行处理.我们可以通过调用::MIME::Types.type_for
来设置它自己,但是我们正在尝试使我们的代码尽可能地与升级兼容.
问题/tl;博士
既然不推荐使用CarrierWave::MimeTypes
,是否有最佳实践来处理具有通用内容类型(application/octet-stream
)的CarrierWave上传?
对于在我们变得更好之前来到这里的人,我们现有的解决方案如下:
# we replicate this idea of generic types from CarrierWave::MimeTypes
GENERIC_CONTENT_TYPES = %w[application/octet-stream binary/octet-stream]
# and add a clearing method to our uploader processor
process :clear_generic_content_type
def clear_generic_content_type
file.content_type = nil if GENERIC_CONTENT_TYPES.include?(file.try(:content_type))
end
Problem
CarrierWave::MimeTypes
, which added the set_content_type
method to an uploader is deprecated. Among other things, this method would attempt to detect the content type if the given one was generic (either application/octet-stream
or binary/octet-stream
).
The deprecation message for CarrierWave::MimeTypes
says:
However this class always returns the existing content type if it is defined whether it is generic or not. See the code here.
Current Solution
For now we're manually handling this by clearing the content type if it is a generic type and having the library then properly detect it. We could set it ourselves with a call to ::MIME::Types.type_for
however we're attempting to keep our code as upgrade compatible as we can.
Question / tl;dr
Is there a best practice for handling CarrierWave uploads with generic content types (application/octet-stream
) now that CarrierWave::MimeTypes
is deprecated?
Our existing solution is as follows for those who got here before we have anything better:
# we replicate this idea of generic types from CarrierWave::MimeTypes
GENERIC_CONTENT_TYPES = %w[application/octet-stream binary/octet-stream]
# and add a clearing method to our uploader processor
process :clear_generic_content_type
def clear_generic_content_type
file.content_type = nil if GENERIC_CONTENT_TYPES.include?(file.try(:content_type))
end
这篇关于在不赞成使用CarrierWave :: MimeTypes的情况下,上传者应如何处理/覆盖通用内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!