Commit edad85f0 by Jon Yurek

Moved fingerprint generation into Atatchment

parent 9e3b5db6
...@@ -93,7 +93,7 @@ module Paperclip ...@@ -93,7 +93,7 @@ module Paperclip
instance_write(:file_name, uploaded_file.original_filename.strip) instance_write(:file_name, uploaded_file.original_filename.strip)
instance_write(:content_type, uploaded_file.content_type.to_s.strip) instance_write(:content_type, uploaded_file.content_type.to_s.strip)
instance_write(:file_size, uploaded_file.size.to_i) 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) instance_write(:updated_at, Time.now)
@dirty = true @dirty = true
...@@ -102,7 +102,7 @@ module Paperclip ...@@ -102,7 +102,7 @@ module Paperclip
# Reset the file size if the original file was reprocessed. # Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i) 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 ensure
uploaded_file.close if close_uploaded_file uploaded_file.close if close_uploaded_file
end end
...@@ -181,7 +181,7 @@ module Paperclip ...@@ -181,7 +181,7 @@ module Paperclip
# Returns the hash of the file as originally assigned, and lives in the # Returns the hash of the file as originally assigned, and lives in the
# <attachment>_fingerprint attribute of the model. # <attachment>_fingerprint attribute of the model.
def fingerprint 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 end
# Returns the content_type of the file as originally assigned, and lives # Returns the content_type of the file as originally assigned, and lives
...@@ -197,6 +197,12 @@ module Paperclip ...@@ -197,6 +197,12 @@ module Paperclip
time && time.to_f.to_i time && time.to_f.to_i
end 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 # Paths and URLs can have a number of variables interpolated into them
# to vary the storage location based on name, id, style, class, etc. # to vary the storage location based on name, id, style, class, etc.
# This method is a deprecated access into supplying and retrieving these # This method is a deprecated access into supplying and retrieving these
......
...@@ -32,13 +32,6 @@ module Paperclip ...@@ -32,13 +32,6 @@ module Paperclip
def size def size
File.size(self) File.size(self)
end end
# Returns the hash of the file.
def fingerprint
data = self.read
self.rewind
Digest::MD5.hexdigest(data)
end
end end
end end
......
...@@ -463,14 +463,11 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -463,14 +463,11 @@ class AttachmentTest < Test::Unit::TestCase
setup do setup do
rebuild_model rebuild_model
@not_file = mock @not_file = mock("not_file")
@tempfile = mock @tempfile = mock("tempfile")
@not_file.stubs(:nil?).returns(false) @not_file.stubs(:nil?).returns(false)
@not_file.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
@tempfile.stubs(:fingerprint).returns('bd94545193321376b70136f8b223abf8')
@not_file.expects(:size).returns(10) @not_file.expects(:size).returns(10)
@tempfile.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(:original_filename).returns("sheep_say_bæ.png\r\n")
@not_file.expects(:content_type).returns("image/png\r\n") @not_file.expects(:content_type).returns("image/png\r\n")
...@@ -479,6 +476,9 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -479,6 +476,9 @@ class AttachmentTest < Test::Unit::TestCase
@attachment.expects(:valid_assignment?).with(@not_file).returns(true) @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
@attachment.expects(:queue_existing_for_delete) @attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process) @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 @dummy.avatar = @not_file
end end
......
...@@ -33,21 +33,4 @@ class UpfileTest < Test::Unit::TestCase ...@@ -33,21 +33,4 @@ class UpfileTest < Test::Unit::TestCase
end end
assert_equal 'text/plain', file.content_type assert_equal 'text/plain', file.content_type
end 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 end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment