Skip to content

Commit 90cd609

Browse files
author
Jon Yurek
committed
Rewind the file after reading it for fingerprinting
1 parent f9e592a commit 90cd609

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/paperclip/upfile.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def size
3535

3636
# Returns the hash of the file.
3737
def fingerprint
38-
Digest::MD5.hexdigest(self.read)
38+
data = self.read
39+
self.rewind
40+
Digest::MD5.hexdigest(data)
3941
end
4042
end
4143
end

test/upfile_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,21 @@ class << file
3333
end
3434
assert_equal 'text/plain', file.content_type
3535
end
36+
37+
should "return a MD5 fingerprint of the file" do
38+
file = StringIO.new("1234567890")
39+
class << file
40+
include Paperclip::Upfile
41+
end
42+
assert_equal "e807f1fcf82d132f9bb018ca6738a19f", file.fingerprint
43+
end
44+
45+
should "still be readable after the file fingerprints itself" do
46+
file = StringIO.new("1234567890")
47+
class << file
48+
include Paperclip::Upfile
49+
end
50+
file.fingerprint
51+
assert_equal "1234567890", file.read
52+
end
3653
end

0 commit comments

Comments
 (0)