Skip to content

Commit

Permalink
Moved fingerprint generation into Atatchment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Nov 23, 2010
1 parent 9e3b5db commit edad85f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
12 changes: 9 additions & 3 deletions lib/paperclip/attachment.rb
Expand Up @@ -93,7 +93,7 @@ def assign uploaded_file
instance_write(:file_name, uploaded_file.original_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i)
instance_write(:fingerprint, uploaded_file.fingerprint)
instance_write(:fingerprint, generate_fingerprint(uploaded_file))
instance_write(:updated_at, Time.now)

@dirty = true
Expand All @@ -102,7 +102,7 @@ def assign uploaded_file

# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
instance_write(:fingerprint, @queued_for_write[:original].fingerprint)
instance_write(:fingerprint, generate_fingerprint(@queued_for_write[:original]))
ensure
uploaded_file.close if close_uploaded_file
end
Expand Down Expand Up @@ -181,7 +181,7 @@ def size
# Returns the hash of the file as originally assigned, and lives in the
# <attachment>_fingerprint attribute of the model.
def fingerprint
instance_read(:fingerprint) || (@queued_for_write[:original] && @queued_for_write[:original].fingerprint)
instance_read(:fingerprint) || (@queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original]))
end

# Returns the content_type of the file as originally assigned, and lives
Expand All @@ -197,6 +197,12 @@ def updated_at
time && time.to_f.to_i
end

def generate_fingerprint(source)
data = source.read
source.rewind if source.respond_to?(:rewind)
Digest::MD5.hexdigest(data)
end

# Paths and URLs can have a number of variables interpolated into them
# to vary the storage location based on name, id, style, class, etc.
# This method is a deprecated access into supplying and retrieving these
Expand Down
7 changes: 0 additions & 7 deletions lib/paperclip/upfile.rb
Expand Up @@ -32,13 +32,6 @@ def original_filename
def size
File.size(self)
end

# Returns the hash of the file.
def fingerprint
data = self.read
self.rewind
Digest::MD5.hexdigest(data)
end
end
end

Expand Down
10 changes: 5 additions & 5 deletions test/attachment_test.rb
Expand Up @@ -463,14 +463,11 @@ def do_after_all; end
setup do
rebuild_model

@not_file = mock
@tempfile = mock
@not_file = mock("not_file")
@tempfile = mock("tempfile")
@not_file.stubs(:nil?).returns(false)
@not_file.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
@tempfile.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
@not_file.expects(:size).returns(10)
@tempfile.expects(:size).returns(10)
@not_file.expects(:to_tempfile).returns(@tempfile)
@not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
@not_file.expects(:content_type).returns("image/png\r\n")

Expand All @@ -479,6 +476,9 @@ def do_after_all; end
@attachment.expects(:valid_assignment?).with(@not_file).returns(true)
@attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process)
@attachment.expects(:to_tempfile).returns(@tempfile)
@attachment.expects(:generate_fingerprint).with(@tempfile).returns("12345")
@attachment.expects(:generate_fingerprint).with(@not_file).returns("12345")
@dummy.avatar = @not_file
end

Expand Down
17 changes: 0 additions & 17 deletions test/upfile_test.rb
Expand Up @@ -33,21 +33,4 @@ 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 edad85f

Please sign in to comment.