ruby Rails:查看服务器是否安装了libvips或其他软件包

du7egjpx  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(95)

我将我的应用程序文件移动到ActiveStorage解决方案,并希望利用其图像插值功能(调整大小,裁剪等)。
我有大约100多个应用程序示例,它们位于大约30台服务器上。并不是所有的都安装了libvipsimagemagick包,所以我想只在存在variant方法的情况下才添加该lib。例如:

if has_my_server_libvips? 
  logo_header = Portal.logo_header.variant({ resize_to_fit: [230, 50] })
else
  logo_header = Portal.logo_header
end

是否有任何Ruby / RoR内置/自定义方法来检查它?

gwbalxhn

gwbalxhn1#

您可以检查这些库是否有可执行命令。这只是一个想法,你可以按照你的意愿来实现它。

logo_header =
  if %i[vips magick].any? { |command| `which #{command}`.present? } 
    Portal.logo_header.variant({ resize_to_fit: [230, 50] })
  else
    Portal.logo_header
  end

which command返回可执行文件bin的路径(末尾有新行字符),如果没有则返回空字符串
您也可以定义这样的方法(例如在Kernel中)

module Kernel
  def command_exists?(command)
    command = command.delete('^a-zA-Z')

    !`which #{command}`.empty?
  end
end

但要小心:在调用此方法之前清理参数以防止注入!

相关问题