ruby-on-rails - ActiveStorage:旧网址仍然请求弃用:variation_key 中的combine_options
问题描述
最近我从 Rails 6.0.3.4 升级到 6.1.3。ActiveStorage deprecated combine_options
,我从我的应用程序中清除了它。所有新的请求都按预期工作。Internet Bots(Facebook、Google、...)缓存托管在网站(如我的)上的图像的 URL。根据我的 Rollbar 记录,他们每天会请求几次。
应该加载 ActiveStorage 附件的缓存 URL 在 URL 中包含旧variation_key
的。当 blob 想要使用 decoded 加载时variation_key
,我看到它combine_options
仍然存在。这会抛出一个500 Internal Server Error with ArgumentError (Active Storage's ImageProcessing transformer doesn't support :combine_options, as it always generates a single ImageMagick command.):
.
有什么办法可以阻止这些错误出现?
导轨版本:6.1.3。红宝石版本:2.7.2p137
解决方案
我已经使用一些中间件解决了这个问题。这将拦截所有传入的请求,扫描它们是否是ActiveStorage
url,找到不推荐使用的请求,combine_options
然后返回 404 not found。这段代码也会报错是当前环境是开发环境,这样我就不会再不小心引入废弃的代码了。
对于那些可能有同样问题的人,这里是代码。
应用程序.rb
require_relative '../lib/stopper'
config.middleware.use ::Stopper
库/stopper.rb
class Stopper
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
path = req.path
if problematic_active_storage_url?(path)
if ENV["RACK_ENV"] == 'development'
raise "Problematic route, includes deprecated combine_options"
end
[404, {}, ['not found']]
else
@app.call(env)
end
end
def problematic_active_storage_url?(path)
if active_storage_path?(path) && !valid_variation_key?(variation_key_from_path(path))
return true
end
false
end
def active_storage_path?(path)
path.start_with?("/rails/active_storage/representations/")
end
def variation_key_from_path(path)
if path.start_with?("/rails/active_storage/representations/redirect/")
path.split('/')[6]
elsif path.start_with?("/rails/active_storage/representations/")
path.split('/')[5]
end
end
def valid_variation_key?(var_key)
if decoded_variation = ActiveStorage::Variation.decode(var_key)
if transformations = decoded_variation.transformations
if transformations[:combine_options].present?
return false
end
end
end
true
end
end
推荐阅读
- java - TestNG 中的注释是强类型的。这是什么意思?
- c++ - getline 循环禁用打印到 txt
- tfs - 导入包含任务组的任务组
- c++ - Raspberry Pi 上的 Portaudio,找不到输入流的设备
- scala - Scala:使用同名的函数和内部对象
- firebase - Flutter 从 QuerySnapshot 转换为 Future
- >>
- android - Android 在preferences.xml 文件上使用字符串资源
- json - json在powershell中展开
- javascript - FCC Refactor Global Variables Out of Functions,数组未同时使用两种方法
- javascript - 在所见即所得编辑器中上传和显示图像