Ruby on Rails 使用命令行计算图像的 md5 ``

Ruby on Rails Calculating md5 of image with command line ``

我正在尝试在 ruby 脚本和 rails 服务器上的 ruby 上生成图像的 md5 散列。

在脚本中(运行 本地 mac)我在做:

`md5 -q path-to-file`

它工作得很好并生成类似 Added new md5: efe99a09e6e1b192314891b960018bd4 的东西当我 运行 在服务器上执行相同的命令时(运行 在 linux machine), 它显示为空字符串

def add_md5_if_empty(test_image)
  if md5.nil?
    self.md5 = `md5 -q #{test_image.image.path}`
    logger.info "Added new md5: #{self.md5} for image at path: #{test_image.image.path}"
    test_image.save
  end
end


I, [2016-02-24T03:48:36.879648 #42]  INFO -- : Added new md5:  for image at path: /app/public/system/test_images/images/000/209/309/original/filename.png

我的猜测是 rails 服务器上的 ruby (linux machine) 没有 md5 命令。另外我的项目正在使用 Docker 所以也许我缺少一些依赖性。任何帮助将不胜感激。

纯Ruby:

require 'digest/md5'
# ...
self.md5 = Digest::MD5.hexdigest(File.binread(test_image.image.path))

编辑:更简单:

self.md5 = Digest::MD5.file(test_image.image.path).hexdigest