With `CarrierWave::MimeTypes` deprecated, how shou

2019-04-17 13:40发布

问题:

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?

回答1:

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


回答2:

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.