Ruby On Rails: 获取回形针图像的 md5 哈希值

Ruby On Rails: Get md5 hash of paperclip image

我正在使用名为回形针的 gem 将图像上传到 rails 服务器上的 ruby。图像已正确上传并且正在运行。我正在努力使用 md5 哈希,以便可以向 rails 服务器发出单个请求,返回我的脚本确切需要上传的图像(我不想重复)。

在脚本中,我使用

成功计算了 md5

require 'digest/md5'md5 = Digest::MD5.file(filename).hexdigest

在服务器上为了检查 md5 是否相同(要上传的图像与服务器上的图像)我需要计算服务器上每个回形针图像的 md5。

图像称为 TestImages,模型如下所示

class TestImage < ActiveRecord::Base
  has_attached_file :image, styles: {thumbnail: '100x100', small: '350x350'}
  validates_attachment :image, content_type: {content_type: ["application/octet-stream", "multipart/form-data", "image/jpg", "image/jpeg", "image/png", "image/gif"]}
  belongs_to :build
  belongs_to :test
end

有人说回形针增加了指纹(md5 表示)的功能,但我不确定如何设置它。它似乎是自动完成的,但需要作为列存储在数据库中?这是另一个 post 我在看 Rails: How does MD5 checksum work in paperclip?

如果它不能使用回形针(回形针指纹),我可以使用与脚本中相同的 digest/md5 方法,但我似乎无法找到回形针图像的完整图像路径。

使用这个无效 image.md5 = Digest::MD5.file(test_image.image.path).hexdigest

这给出了以下错误:

Errno::ENOENT (No such file or directory @ rb_sysopen - /Users/scott.bishop/Code/visual-automation/public/system/test_images/images//original/testBasic_2x.png):
  app/controllers/test_images_controller.rb:37:in `create'

我不确定它想要什么路径。任何帮助将不胜感激。

事实证明回形针图像路径有效。

require 'digest/md5'
image.md5 = Digest::MD5.file(PAPER_CLIP_IMAGE.path).hexdigest