随着 `CarrierWave::MimeTypes` 被弃用,上传者应该如何 handle/overwrite 通用内容类型?

With `CarrierWave::MimeTypes` deprecated, how should uploaders handle/overwrite generic content types?

问题

CarrierWave::MimeTypes,将 set_content_type 方法添加到上传器已弃用。除此之外,如果给定的内容类型是通用的(application/octet-streambinary/octet-stream),this method 将尝试检测内容类型。

CarrierWave::MimeTypesdeprecation message 说:

get the content_type from the SanitizedFile object directly

然而,这个 class 总是 returns 现有的内容类型,无论它是否是通用的。 See the code here.

当前解决方案

现在我们通过清除内容类型(如果它是通用类型)并让库正确检测它来手动处理此问题。我们可以通过调用 ::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

Here on Carrierwave docs 表示不再需要设置内容类型:

Setting the content type

As of v0.11.0, the mime-types gem is a runtime dependency and the content type is set automatically. You no longer need to do this manually.