Commit 968ef780

mo khan <mo@mokhan.ca>
2014-06-07 22:54:10
compute a sha256 for an image.
1 parent 4688a3a
Changed files (2)
app
services
infrastructure
spec
services
infrastructure
app/services/infrastructure/image.rb
@@ -18,6 +18,10 @@ class Image
     @exif.parse_geolocation_from(@path)
   end
 
+  def sha256
+    @sha256 ||= Digest::SHA256.file(@path).to_s
+  end
+
   def resize_to_fit(width, height)
     manipulate! do |img|
       img.resize "#{width}x#{height}"
spec/services/infrastructure/image_spec.rb
@@ -9,5 +9,10 @@ describe Image do
     it "sanitizes the filename" do
       expect(Image.new("/Users/mo/blah huh.png").filename).to eql("blah_huh.png")
     end
+
+    it "gives the SHA256 of the image" do
+      path = File.join(Rails.root, 'spec/fixtures/images/gps.jpg')
+      expect(Image.new(path).sha256).to eql('a1b1b9b8b22d3a4a3523ebb0dc2c57c685938427e12e8a6439fbab104da6b1d8')
+    end
   end
 end