不推荐使用`CarrierWave :: MimeTypes`,上传者应如何处理/覆盖通用内容类型?(With `CarrierWave::MimeTypes` deprecated, how sho

系统教程 行业动态 更新时间:2024-06-14 16:55:57
不推荐使用`CarrierWave :: MimeTypes`,上传者应如何处理/覆盖通用内容类型?(With `CarrierWave::MimeTypes` deprecated, how should uploaders handle/overwrite generic content types?)

问题

不推荐使用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)) end

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

更多推荐

本文发布于:2023-04-10 11:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/58008a1c7b823c88c3968863e834fd60.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:推荐使用   上传者   应如何   类型   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!