paperclipftp 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +0 -1
  2. data/VERSION +1 -1
  3. data/lib/paperclipftp.rb +37 -9
  4. metadata +8 -25
data/Rakefile CHANGED
@@ -11,7 +11,6 @@ begin
11
11
  gem.homepage = "http://github.com/cdamian/paperclipftp"
12
12
  gem.authors = ["Damian Caruso"]
13
13
  gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
14
- gem.add_dependency "paperclip", ">= 2.3.0"
15
14
  gem.add_development_dependency "yard", ">= 0"
16
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -12,8 +12,15 @@ module Paperclip
12
12
  @verify_size = !!@options[:ftp_verify_size_on_upload]
13
13
  @timeout = @options[:ftp_timeout] || 3600
14
14
  end
15
+ # it is better to share and keep ftp connection because otherwise some methods (like
16
+ # exists?, to_file etc) will open new connection each and every time without closing it - bad
17
+ unless base.class.respond_to? (:ftp_conn)
18
+ class << base.class; attr_accessor :ftp_conn; end
19
+ end
15
20
  end
16
21
 
22
+ #class << self; attr_accessor :ftp_conn; end
23
+
17
24
  def exists?(style = default_style)
18
25
  Timeout::timeout(@timeout, FtpTimeout) do
19
26
  file_size(ftp_path(style)) > 0
@@ -54,7 +61,7 @@ module Paperclip
54
61
  # avoiding those weird occasional 0 file sizes by not using instance method file.size
55
62
  local_file_size = File.size(file.path)
56
63
  remote_file_size = file_size(remote_path)
57
- raise Net::FTPError.new "Uploaded #{remote_file_size} bytes instead of #{local_file_size} bytes" unless remote_file_size == local_file_size
64
+ raise Net::FTPError.new("Uploaded #{remote_file_size} bytes instead of #{local_file_size} bytes") unless remote_file_size == local_file_size
58
65
  end
59
66
  end
60
67
  end
@@ -89,20 +96,34 @@ module Paperclip
89
96
  private
90
97
 
91
98
  def ftp
92
- if @ftp.nil? || @ftp.closed?
99
+ if ftp_conn.nil? || ftp_conn.closed?
93
100
  Timeout::timeout(@timeout, FtpTimeout) do
94
- @ftp = Net::FTP.new(@ftp_credentials[:host], @ftp_credentials[:username], @ftp_credentials[:password])
95
- @ftp.debug_mode = @debug_mode
96
- @ftp.passive = @passive_mode
101
+ first_try = true
102
+ begin
103
+ debug "Creating ftp connection"
104
+ self.class.ftp_conn = Net::FTP.new(@ftp_credentials[:host], @ftp_credentials[:username], @ftp_credentials[:password])
105
+ ftp_conn.debug_mode = @debug_mode
106
+ ftp_conn.passive = @passive_mode
107
+ rescue EOFError => e
108
+ debug "Caught EOFError (#{e.inspect})"
109
+ if first_try
110
+ sleep 1
111
+ first_try = false
112
+ retry
113
+ else
114
+ raise e
115
+ end
116
+ end
97
117
  end
98
118
  end
99
- @ftp
119
+ ftp_conn
100
120
  end
101
121
 
102
122
  def close_ftp_connection
103
- unless @ftp.nil? || @ftp.closed?
104
- @ftp.close
105
- @ftp = nil
123
+ unless ftp_conn.nil? || ftp_conn.closed?
124
+ debug "Closing ftp connection"
125
+ ftp_conn.close
126
+ self.class.ftp_conn = nil
106
127
  end
107
128
  end
108
129
 
@@ -137,6 +158,7 @@ module Paperclip
137
158
  #File not exists
138
159
  -1
139
160
  rescue Net::FTPReplyError => e
161
+ debug "Caught Net::FTPReplyError (#{e.inspect})"
140
162
  close_ftp_connection
141
163
  raise e
142
164
  end
@@ -159,7 +181,13 @@ module Paperclip
159
181
  end
160
182
  end
161
183
 
184
+ def ftp_conn
185
+ self.class.ftp_conn
186
+ end
162
187
 
188
+ def debug(text)
189
+ puts "PaperclipFTP: #{text}" if @debug_mode
190
+ end
163
191
  end
164
192
  end
165
193
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclipftp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Damian Caruso
@@ -15,29 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-13 00:00:00 -04:00
18
+ date: 2011-05-26 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: paperclip
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 2
32
- - 3
33
- - 0
34
- version: 2.3.0
35
- type: :runtime
36
- version_requirements: *id001
37
21
  - !ruby/object:Gem::Dependency
38
22
  name: yard
39
23
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ requirement: &id001 !ruby/object:Gem::Requirement
41
25
  none: false
42
26
  requirements:
43
27
  - - ">="
@@ -47,7 +31,7 @@ dependencies:
47
31
  - 0
48
32
  version: "0"
49
33
  type: :development
50
- version_requirements: *id002
34
+ version_requirements: *id001
51
35
  description: Ftp storage support for paperclip file attachment
52
36
  email: damian.caruso@gmail.com
53
37
  executables: []
@@ -99,6 +83,5 @@ rubygems_version: 1.6.2
99
83
  signing_key:
100
84
  specification_version: 3
101
85
  summary: Ftp storage support for paperclip file attachment
102
- test_files:
103
- - test/paperclipftp_test.rb
104
- - test/test_helper.rb
86
+ test_files: []
87
+