在不赞成使用CarrierWave :: MimeTypes的情况下,上传者应如何处理/覆盖通用内容类型? [英] With `CarrierWave::MimeTypes` deprecated, how should uploaders handle/overwrite generic content types?

查看:86
本文介绍了在不赞成使用CarrierWave :: MimeTypes的情况下,上传者应如何处理/覆盖通用内容类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

不推荐使用将set_content_type方法添加到上载器的

CarrierWave::MimeTypes.除其他外,此方法将尝试检测给定类型的内容类型(application/octet-streambinary/octet-stream).

弃用消息对于CarrierWave::MimeTypes说:

直接从SanitizedFile对象获取content_type

但是,如果定义了该类是通用类还是非通用类,则此类始终返回现有的内容类型. 在此处查看代码.. >

当前解决方案

目前,我们正在通过清除内容类型(如果它是通用类型)并让库正确检测它来手动进行处理.我们可以通过调用::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:

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?

解决方案

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的情况下,上传者应如何处理/覆盖通用内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆