Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved fingerprint generation into Atatchment

  • Loading branch information...
commit edad85f0c79ad02d3c111d318e6dba8d6ca881a8 1 parent 9e3b5db
@jyurek jyurek authored
View
12 lib/paperclip/attachment.rb
@@ -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
@@ -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
@@ -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
@@ -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
View
7 lib/paperclip/upfile.rb
@@ -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
View
10 test/attachment_test.rb
@@ -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")
@@ -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
View
17 test/upfile_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.