Skip to content

Commit

Permalink
Rewind the file after reading it for fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Oct 8, 2010
1 parent f9e592a commit 90cd609
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/paperclip/upfile.rb
Expand Up @@ -35,7 +35,9 @@ def size

# Returns the hash of the file.
def fingerprint
Digest::MD5.hexdigest(self.read)
data = self.read
self.rewind
Digest::MD5.hexdigest(data)
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/upfile_test.rb
Expand Up @@ -33,4 +33,21 @@ class << file
end
assert_equal 'text/plain', file.content_type
end

should "return a MD5 fingerprint of the file" do
file = StringIO.new("1234567890")
class << file
include Paperclip::Upfile
end
assert_equal "e807f1fcf82d132f9bb018ca6738a19f", file.fingerprint
end

should "still be readable after the file fingerprints itself" do
file = StringIO.new("1234567890")
class << file
include Paperclip::Upfile
end
file.fingerprint
assert_equal "1234567890", file.read
end
end

0 comments on commit 90cd609

Please sign in to comment.