With `CarrierWave::MimeTypes` deprecated, how shou

2019-04-17 13:39发布

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:

get the content_type from the SanitizedFile object directly

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?

2条回答
贼婆χ
2楼-- · 2019-04-17 13:58

Here on Carrierwave docs says that setting the content type is no longer needed:

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.

查看更多
地球回转人心会变
3楼-- · 2019-04-17 14:11

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
查看更多
登录 后发表回答