paperclip 2.3.4 → 2.3.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

@@ -1,29 +1,27 @@
1
1
  # Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying
2
2
  # and Tempfile conversion.
3
3
  module IOStream
4
-
5
4
  # Returns a Tempfile containing the contents of the readable object.
6
- def to_tempfile
7
- name = respond_to?(:original_filename) ? original_filename : (respond_to?(:path) ? path : "stream")
5
+ def to_tempfile(object)
6
+ return object.to_tempfile if object.respond_to?(:to_tempfile)
7
+ name = object.respond_to?(:original_filename) ? object.original_filename : (object.respond_to?(:path) ? object.path : "stream")
8
8
  tempfile = Paperclip::Tempfile.new(["stream", File.extname(name)])
9
9
  tempfile.binmode
10
- self.stream_to(tempfile)
10
+ stream_to(object, tempfile)
11
11
  end
12
12
 
13
13
  # Copies one read-able object from one place to another in blocks, obviating the need to load
14
- # the whole thing into memory. Defaults to 8k blocks. If this module is included in both
15
- # StringIO and Tempfile, then either can have its data copied anywhere else without typing
16
- # worries or memory overhead worries. Returns a File if a String is passed in as the destination
17
- # and returns the IO or Tempfile as passed in if one is sent as the destination.
18
- def stream_to path_or_file, in_blocks_of = 8192
14
+ # the whole thing into memory. Defaults to 8k blocks. Returns a File if a String is passed
15
+ # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination.
16
+ def stream_to object, path_or_file, in_blocks_of = 8192
19
17
  dstio = case path_or_file
20
18
  when String then File.new(path_or_file, "wb+")
21
19
  when IO then path_or_file
22
20
  when Tempfile then path_or_file
23
21
  end
24
22
  buffer = ""
25
- self.rewind
26
- while self.read(in_blocks_of, buffer) do
23
+ object.rewind
24
+ while object.read(in_blocks_of, buffer) do
27
25
  dstio.write(buffer)
28
26
  end
29
27
  dstio.rewind
@@ -31,18 +29,6 @@ module IOStream
31
29
  end
32
30
  end
33
31
 
34
- class IO #:nodoc:
35
- include IOStream
36
- end
37
-
38
- %w( Tempfile StringIO ).each do |klass|
39
- if Object.const_defined? klass
40
- Object.const_get(klass).class_eval do
41
- include IOStream
42
- end
43
- end
44
- end
45
-
46
32
  # Corrects a bug in Windows when asking for Tempfile size.
47
33
  if defined? Tempfile
48
34
  class Tempfile
@@ -50,7 +50,7 @@ module Paperclip
50
50
  prefix, suffix = basename, ''
51
51
  end
52
52
 
53
- t = time.now.strftime("%y%m%d")
53
+ t = Time.now.strftime("%y%m%d")
54
54
  path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}#{suffix}"
55
55
  end
56
56
  end
@@ -127,10 +127,10 @@ module Paperclip
127
127
  # style, in the format most representative of the current storage.
128
128
  def to_file style = default_style
129
129
  return @queued_for_write[style] if @queued_for_write[style]
130
- filename = path(style).split(".")
130
+ filename = path(style)
131
131
  extname = File.extname(filename)
132
132
  basename = File.basename(filename, extname)
133
- file = Tempfile.new(basename, extname)
133
+ file = Tempfile.new([basename, extname])
134
134
  file.write(AWS::S3::S3Object.value(path(style), bucket_name))
135
135
  file.rewind
136
136
  return file
@@ -35,7 +35,9 @@ module Paperclip
35
35
 
36
36
  # Returns the hash of the file.
37
37
  def fingerprint
38
- Digest::MD5.hexdigest(self.read)
38
+ data = self.read
39
+ self.rewind
40
+ Digest::MD5.hexdigest(data)
39
41
  end
40
42
  end
41
43
  end
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "2.3.4" unless defined? Paperclip::VERSION
2
+ VERSION = "2.3.5" unless defined? Paperclip::VERSION
3
3
  end
@@ -1,14 +1,7 @@
1
1
  require 'test/helper'
2
2
 
3
3
  class IOStreamTest < Test::Unit::TestCase
4
- context "IOStream" do
5
- should "be included in IO, File, Tempfile, and StringIO" do
6
- [IO, File, Tempfile, StringIO].each do |klass|
7
- assert klass.included_modules.include?(IOStream), "Not in #{klass}"
8
- end
9
- end
10
- end
11
-
4
+ include IOStream
12
5
  context "A file" do
13
6
  setup do
14
7
  @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@@ -21,7 +14,7 @@ class IOStreamTest < Test::Unit::TestCase
21
14
  context "and given a String" do
22
15
  setup do
23
16
  FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
24
- assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
17
+ assert @result = stream_to(@file, File.join(ROOT, 'tmp', 'iostream.string.test'))
25
18
  end
26
19
 
27
20
  should "return a File" do
@@ -38,7 +31,7 @@ class IOStreamTest < Test::Unit::TestCase
38
31
  setup do
39
32
  tempfile = Tempfile.new('iostream.test')
40
33
  tempfile.binmode
41
- assert @result = @file.stream_to(tempfile)
34
+ assert @result = stream_to(@file, tempfile)
42
35
  end
43
36
 
44
37
  should "return a Tempfile" do
@@ -53,9 +46,9 @@ class IOStreamTest < Test::Unit::TestCase
53
46
 
54
47
  end
55
48
 
56
- context "that is sent #to_tempfile" do
49
+ context "that is converted #to_tempfile" do
57
50
  setup do
58
- assert @tempfile = @file.to_tempfile
51
+ assert @tempfile = to_tempfile(@file)
59
52
  end
60
53
 
61
54
  should "convert it to a Paperclip Tempfile" do
@@ -351,6 +351,11 @@ class StorageTest < Test::Unit::TestCase
351
351
  should "be on S3" do
352
352
  assert true
353
353
  end
354
+
355
+ should "generate a tempfile with the right name" do
356
+ file = @dummy.avatar.to_file
357
+ assert_match /^original.*\.png$/, File.basename(file.path)
358
+ end
354
359
  end
355
360
  end
356
361
  end
@@ -8,7 +8,6 @@ class ThumbnailTest < Test::Unit::TestCase
8
8
  end
9
9
 
10
10
  should "have its path contain a real extension" do
11
- p @tempfile.path
12
11
  assert_equal ".jpg", File.extname(@tempfile.path)
13
12
  end
14
13
 
@@ -33,4 +33,21 @@ class UpfileTest < Test::Unit::TestCase
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
36
53
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 4
10
- version: 2.3.4
9
+ - 5
10
+ version: 2.3.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jon Yurek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-06 00:00:00 -04:00
18
+ date: 2010-10-26 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -119,8 +119,10 @@ files:
119
119
  - lib/generators/paperclip/templates/paperclip_migration.rb.erb
120
120
  - lib/generators/paperclip/USAGE
121
121
  - lib/paperclip/attachment.rb
122
+ - lib/paperclip/attachment.rbc
122
123
  - lib/paperclip/callback_compatability.rb
123
124
  - lib/paperclip/command_line.rb
125
+ - lib/paperclip/configuration.rbc
124
126
  - lib/paperclip/geometry.rb
125
127
  - lib/paperclip/interpolations.rb
126
128
  - lib/paperclip/iostream.rb
@@ -139,6 +141,7 @@ files:
139
141
  - lib/paperclip/upfile.rb
140
142
  - lib/paperclip/version.rb
141
143
  - lib/paperclip.rb
144
+ - lib/paperclip.rbc
142
145
  - lib/tasks/paperclip.rake
143
146
  - test/attachment_test.rb
144
147
  - test/command_line_test.rb