olek-paperclipftp 0.1.0 → 0.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/paperclipftp.rb +31 -17
  3. metadata +3 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.0.1
data/lib/paperclipftp.rb CHANGED
@@ -16,27 +16,29 @@ module Paperclip
16
16
  end
17
17
 
18
18
  def exists?(style = default_style)
19
- move_to_remote_path(File.dirname(path(style)))
20
- ftp.size(File.basename(path(style))) > 0 ? true : false
21
- rescue Net::FTPPermError => e
22
- #File not exists
23
- false
24
- rescue Net::FTPReplyError => e
25
- ftp.close
26
- raise e
19
+ file_size(path(style)) > 0
27
20
  end
28
21
 
29
22
  def to_file style = default_style
30
- @queued_for_write[style] || "ftp://#{path(style)}"
23
+ return @queued_for_write[style] if @queued_for_write[style]
24
+ file = Tempfile.new(path(style))
25
+ ftp.getbinaryfile(path(style), file.path)
26
+ file.rewind
27
+ return file
31
28
  end
29
+
32
30
  alias_method :to_io, :to_file
33
31
 
34
32
  def flush_writes
35
33
  @queued_for_write.each do |style, file|
34
+ local_file_size = file.size
36
35
  file.close
37
- move_to_remote_path(File.dirname(path(style)))
38
- log("uploading #{path(style)}")
39
- ftp.putbinaryfile(file.path, File.basename(path(style)))
36
+ remote_path = path(style)
37
+ ensure_parent_folder_for(remote_path)
38
+ log("uploading #{remote_path}")
39
+ ftp.putbinaryfile(file.path, remote_path)
40
+ remote_file_size = file_size(remote_path)
41
+ raise Net::FTPError.new "Uploaded #{remote_file_size} bytes instead of #{local_file_size} bytes" unless remote_file_size == local_file_size
40
42
  end
41
43
  @queued_for_write = {}
42
44
  rescue Net::FTPReplyError => e
@@ -50,9 +52,8 @@ module Paperclip
50
52
  def flush_deletes
51
53
  @queued_for_delete.each do |path|
52
54
  begin
53
- move_to_remote_path(File.dirname(path))
54
55
  log("deleting #{path}")
55
- ftp.delete(File.basename(path))
56
+ ftp.delete(path)
56
57
  rescue Net::FTPPermError, Net::FTPReplyError
57
58
  end
58
59
  end
@@ -65,9 +66,10 @@ module Paperclip
65
66
  ftp.close
66
67
  end
67
68
 
68
- def move_to_remote_path(rpath)
69
+ def ensure_parent_folder_for(remote_path)
70
+ dir_path = File.dirname(remote_path)
69
71
  ftp.chdir("/")
70
- rpath.split(File::SEPARATOR).each do |rdir|
72
+ dir_path.split(File::SEPARATOR).each do |rdir|
71
73
  rdir = rdir.strip
72
74
  unless rdir.blank?
73
75
  list = ftp.ls.collect { |f| f.split.last }
@@ -77,14 +79,26 @@ module Paperclip
77
79
  ftp.chdir(rdir)
78
80
  end
79
81
  end
82
+ ftp.chdir("/")
80
83
  end
81
84
 
85
+ private
86
+
82
87
  def parse_credentials
83
88
  creds = YAML.load_file(File.join(RAILS_ROOT,"config","paperclipftp.yml"))
84
89
  creds = creds.stringify_keys
85
90
  (creds[RAILS_ENV] || creds).symbolize_keys
86
91
  end
87
- private :parse_credentials
92
+
93
+ def file_size(remote_path)
94
+ ftp.size(remote_path)
95
+ rescue Net::FTPPermError => e
96
+ #File not exists
97
+ -1
98
+ rescue Net::FTPReplyError => e
99
+ ftp.close
100
+ raise e
101
+ end
88
102
 
89
103
  end
90
104
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olek-paperclipftp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 69
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
9
  - 0
10
- version: 0.1.0
10
+ - 1
11
+ version: 0.1.0.1
11
12
  platform: ruby
12
13
  authors:
13
14
  - Damian Caruso