问题
不推荐使用CarrierWave::MimeTypes ,它将set_content_type方法添加到上传器中。 除其他外,如果给定的内容类型是通用的( application/octet-stream或binary/octet-stream ),则此方法将尝试检测内容类型。
CarrierWave::MimeTypes的弃用消息说:
直接从SanitizedFile对象获取content_type
但是,如果定义它是否是通用的,则此类始终返回现有内容类型。 请参阅此处的代码 。
当前解决方案
现在我们通过清除内容类型来手动处理它,如果它是泛型类型并让库正确检测它。 我们可以通过调用::MIME::Types.type_for来自己设置它,但是我们试图保持我们的代码尽可能兼容升级。
问题/ tl;博士
现在CarrierWave::MimeTypes已被弃用,是否有最佳实践处理带有通用内容类型( application/octet-stream )的CarrierWave上传?
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?
最满意答案
对于那些在我们有更好的东西之前来到这里的人,我们现有的解决方案如下:
# 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)) endOur 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更多推荐
发布评论