Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved fingerprint generation into Atatchment

  • Loading branch information...
commit edad85f0c79ad02d3c111d318e6dba8d6ca881a8 1 parent 9e3b5db
Jon Yurek jyurek authored
12 lib/paperclip/attachment.rb
@@ -93,7 +93,7 @@ def assign uploaded_file
93 93 instance_write(:file_name, uploaded_file.original_filename.strip)
94 94 instance_write(:content_type, uploaded_file.content_type.to_s.strip)
95 95 instance_write(:file_size, uploaded_file.size.to_i)
96   - instance_write(:fingerprint, uploaded_file.fingerprint)
  96 + instance_write(:fingerprint, generate_fingerprint(uploaded_file))
97 97 instance_write(:updated_at, Time.now)
98 98
99 99 @dirty = true
@@ -102,7 +102,7 @@ def assign uploaded_file
102 102
103 103 # Reset the file size if the original file was reprocessed.
104 104 instance_write(:file_size, @queued_for_write[:original].size.to_i)
105   - instance_write(:fingerprint, @queued_for_write[:original].fingerprint)
  105 + instance_write(:fingerprint, generate_fingerprint(@queued_for_write[:original]))
106 106 ensure
107 107 uploaded_file.close if close_uploaded_file
108 108 end
@@ -181,7 +181,7 @@ def size
181 181 # Returns the hash of the file as originally assigned, and lives in the
182 182 # <attachment>_fingerprint attribute of the model.
183 183 def fingerprint
184   - instance_read(:fingerprint) || (@queued_for_write[:original] && @queued_for_write[:original].fingerprint)
  184 + instance_read(:fingerprint) || (@queued_for_write[:original] && generate_fingerprint(@queued_for_write[:original]))
185 185 end
186 186
187 187 # Returns the content_type of the file as originally assigned, and lives
@@ -197,6 +197,12 @@ def updated_at
197 197 time && time.to_f.to_i
198 198 end
199 199
  200 + def generate_fingerprint(source)
  201 + data = source.read
  202 + source.rewind if source.respond_to?(:rewind)
  203 + Digest::MD5.hexdigest(data)
  204 + end
  205 +
200 206 # Paths and URLs can have a number of variables interpolated into them
201 207 # to vary the storage location based on name, id, style, class, etc.
202 208 # This method is a deprecated access into supplying and retrieving these
7 lib/paperclip/upfile.rb
@@ -32,13 +32,6 @@ def original_filename
32 32 def size
33 33 File.size(self)
34 34 end
35   -
36   - # Returns the hash of the file.
37   - def fingerprint
38   - data = self.read
39   - self.rewind
40   - Digest::MD5.hexdigest(data)
41   - end
42 35 end
43 36 end
44 37
10 test/attachment_test.rb
@@ -463,14 +463,11 @@ def do_after_all; end
463 463 setup do
464 464 rebuild_model
465 465
466   - @not_file = mock
467   - @tempfile = mock
  466 + @not_file = mock("not_file")
  467 + @tempfile = mock("tempfile")
468 468 @not_file.stubs(:nil?).returns(false)
469   - @not_file.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
470   - @tempfile.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
471 469 @not_file.expects(:size).returns(10)
472 470 @tempfile.expects(:size).returns(10)
473   - @not_file.expects(:to_tempfile).returns(@tempfile)
474 471 @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
475 472 @not_file.expects(:content_type).returns("image/png\r\n")
476 473
@@ -479,6 +476,9 @@ def do_after_all; end
479 476 @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
480 477 @attachment.expects(:queue_existing_for_delete)
481 478 @attachment.expects(:post_process)
  479 + @attachment.expects(:to_tempfile).returns(@tempfile)
  480 + @attachment.expects(:generate_fingerprint).with(@tempfile).returns("12345")
  481 + @attachment.expects(:generate_fingerprint).with(@not_file).returns("12345")
482 482 @dummy.avatar = @not_file
483 483 end
484 484
17 test/upfile_test.rb
@@ -33,21 +33,4 @@ class << file
33 33 end
34 34 assert_equal 'text/plain', file.content_type
35 35 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
53 36 end

0 comments on commit edad85f

Please sign in to comment.
Something went wrong with that request. Please try again.