mbailey-paperclip 2.3.2 → 2.3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ module Paperclip
15
15
  :default_url => "/:attachment/:style/missing.png",
16
16
  :default_style => :original,
17
17
  :storage => :filesystem,
18
+ :include_updated_timestamp => true,
18
19
  :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
19
20
  }
20
21
  end
@@ -39,6 +40,7 @@ module Paperclip
39
40
  @default_url = options[:default_url]
40
41
  @default_style = options[:default_style]
41
42
  @storage = options[:storage]
43
+ @include_updated_timestamp = options[:include_updated_timestamp]
42
44
  @whiny = options[:whiny_thumbnails] || options[:whiny]
43
45
  @convert_options = options[:convert_options]
44
46
  @processors = options[:processors]
@@ -90,6 +92,7 @@ module Paperclip
90
92
  instance_write(:file_name, uploaded_file.original_filename.strip)
91
93
  instance_write(:content_type, uploaded_file.content_type.to_s.strip)
92
94
  instance_write(:file_size, uploaded_file.size.to_i)
95
+ instance_write(:file_hash, uploaded_file.file_hash)
93
96
  instance_write(:updated_at, Time.now)
94
97
 
95
98
  @dirty = true
@@ -98,6 +101,7 @@ module Paperclip
98
101
 
99
102
  # Reset the file size if the original file was reprocessed.
100
103
  instance_write(:file_size, @queued_for_write[:original].size.to_i)
104
+ instance_write(:file_hash, @queued_for_write[:original].file_hash)
101
105
  ensure
102
106
  uploaded_file.close if close_uploaded_file
103
107
  end
@@ -109,7 +113,7 @@ module Paperclip
109
113
  # security, however, for performance reasons. set
110
114
  # include_updated_timestamp to false if you want to stop the attachment
111
115
  # update time appended to the url
112
- def url style_name = default_style, include_updated_timestamp = true
116
+ def url(style_name = default_style, include_updated_timestamp = @include_updated_timestamp)
113
117
  url = original_filename.nil? ? interpolate(@default_url, style_name) : interpolate(@url, style_name)
114
118
  include_updated_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
115
119
  end
@@ -174,6 +178,12 @@ module Paperclip
174
178
  instance_read(:file_size) || (@queued_for_write[:original] && @queued_for_write[:original].size)
175
179
  end
176
180
 
181
+ # Returns the hash of the file as originally assigned, and lives in the
182
+ # <attachment>_file_hash attribute of the model.
183
+ def file_hash
184
+ instance_read(:file_hash) || (@queued_for_write[:original] && @queued_for_write[:original].file_hash)
185
+ end
186
+
177
187
  # Returns the content_type of the file as originally assigned, and lives
178
188
  # in the <attachment>_content_type attribute of the model.
179
189
  def content_type
@@ -88,6 +88,11 @@ module Paperclip
88
88
  attachment.instance.id
89
89
  end
90
90
 
91
+ # Returns the file hash of the instance.
92
+ def file_hash attachment, style_name
93
+ attachment.file_hash
94
+ end
95
+
91
96
  # Returns the id of the instance in a split path form. e.g. returns
92
97
  # 000/001/234 for an id of 1234.
93
98
  def id_partition attachment, style_name
@@ -32,18 +32,26 @@ module Paperclip
32
32
  def size
33
33
  File.size(self)
34
34
  end
35
+
36
+ # Returns the hash of the file.
37
+ def file_hash
38
+ Digest::MD5.hexdigest(self.read)
39
+ end
35
40
  end
36
41
  end
37
42
 
38
43
  if defined? StringIO
39
44
  class StringIO
40
- attr_accessor :original_filename, :content_type
45
+ attr_accessor :original_filename, :content_type, :file_hash
41
46
  def original_filename
42
47
  @original_filename ||= "stringio.txt"
43
48
  end
44
49
  def content_type
45
50
  @content_type ||= "text/plain"
46
51
  end
52
+ def file_hash
53
+ @file_hash ||= Digest::MD5.hexdigest(self.string)
54
+ end
47
55
  end
48
56
  end
49
57
 
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "2.3.2" unless defined? Paperclip::VERSION
2
+ VERSION = "2.3.2.1" unless defined? Paperclip::VERSION
3
3
  end
data/lib/paperclip.rb CHANGED
@@ -26,6 +26,7 @@
26
26
  # See the +has_attached_file+ documentation for more details.
27
27
 
28
28
  require 'erb'
29
+ require 'digest'
29
30
  require 'tempfile'
30
31
  require 'paperclip/version'
31
32
  require 'paperclip/upfile'
@@ -446,6 +446,8 @@ class AttachmentTest < Test::Unit::TestCase
446
446
  @not_file = mock
447
447
  @tempfile = mock
448
448
  @not_file.stubs(:nil?).returns(false)
449
+ @not_file.stubs(:file_hash).returns('bd94545193321376b70136f8b223abf8')
450
+ @tempfile.stubs(:file_hash).returns('bd94545193321376b70136f8b223abf8')
449
451
  @not_file.expects(:size).returns(10)
450
452
  @tempfile.expects(:size).returns(10)
451
453
  @not_file.expects(:to_tempfile).returns(@tempfile)
@@ -754,5 +756,29 @@ class AttachmentTest < Test::Unit::TestCase
754
756
  assert_equal @file.size, @dummy.avatar.size
755
757
  end
756
758
  end
759
+
760
+ context "and avatar_file_hash column" do
761
+ setup do
762
+ ActiveRecord::Base.connection.add_column :dummies, :avatar_file_hash, :string
763
+ rebuild_class
764
+ @dummy = Dummy.new
765
+ end
766
+
767
+ should "not error when assigned an attachment" do
768
+ assert_nothing_raised { @dummy.avatar = @file }
769
+ end
770
+
771
+ should "return the right value when sent #avatar_file_hash" do
772
+ @dummy.avatar = @file
773
+ assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_file_hash
774
+ end
775
+
776
+ should "return the right value when saved, reloaded, and sent #avatar_file_hash" do
777
+ @dummy.avatar = @file
778
+ @dummy.save
779
+ @dummy = Dummy.find(@dummy.id)
780
+ assert_equal 'aec488126c3b33c08a10c3fa303acf27', @dummy.avatar_file_hash
781
+ end
782
+ end
757
783
  end
758
784
  end
data/test/helper.rb CHANGED
@@ -86,6 +86,7 @@ def rebuild_model options = {}
86
86
  table.column :avatar_content_type, :string
87
87
  table.column :avatar_file_size, :integer
88
88
  table.column :avatar_updated_at, :datetime
89
+ table.column :avatar_file_hash, :string
89
90
  end
90
91
  rebuild_class options
91
92
  end
@@ -105,6 +106,7 @@ class FakeModel
105
106
  :avatar_file_size,
106
107
  :avatar_last_updated,
107
108
  :avatar_content_type,
109
+ :avatar_file_hash,
108
110
  :id
109
111
 
110
112
  def errors
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbailey-paperclip
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 125
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
9
  - 2
10
- version: 2.3.2
10
+ - 1
11
+ version: 2.3.2.1
11
12
  platform: ruby
12
13
  authors:
13
14
  - Jon Yurek