Module: Paperclip::Storage::Ftp

Defined in:
lib/paperclipftp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
# File 'lib/paperclipftp.rb', line 4

def self.extended base
  require 'net/ftp'
  base.instance_eval do
    @ftp_credentials = parse_credentials
  end
end

Instance Method Details

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'lib/paperclipftp.rb', line 19

def exists?(style = default_style)
  move_to_remote_path(File.dirname(path(style)))
  not ftp.size(File.basename(path(style))).zero?
rescue Net::FTPPermError => e
  #File not exists
  false
rescue Net::FTPReplyError => e
  ftp.close
  raise e
end

#flush_deletesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/paperclipftp.rb', line 51

def flush_deletes
  @queued_for_delete.each do |path|
    begin
      move_to_remote_path(File.dirname(path))
      log("FTP deleting #{path}")
      ftp.delete(File.basename(path))
    rescue Net::FTPPermError, Net::FTPReplyError
    end
  end
  @queued_for_delete = []
rescue Net::FTPReplyError => e
  raise e
rescue Net::FTPPermError => e
  raise e
ensure
  ftp.close 
end

#flush_writesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/paperclipftp.rb', line 35

def flush_writes
  @queued_for_write.each do |style, file|
    file.close
    move_to_remote_path(File.dirname(path(style)))
    log("FTP Uploading #{path(style)}")
    ftp.putbinaryfile(file.path, File.basename(path(style)))
  end
  @queued_for_write = {}
rescue Net::FTPReplyError => e
  raise e
rescue Net::FTPPermError => e
  raise e
ensure
  ftp.close
end

#ftpObject



11
12
13
14
15
16
17
# File 'lib/paperclipftp.rb', line 11

def ftp
  return @ftp unless @ftp.nil? || @ftp.closed?
  @ftp = Net::FTP.new(@ftp_credentials[:host])
  @ftp.(@ftp_credentials[:username], @ftp_credentials[:password])
  @ftp.passive = @ftp_credentials[:passive] || true
  @ftp
end

#move_to_remote_path(rpath) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/paperclipftp.rb', line 69

def move_to_remote_path(rpath)
  ftp.chdir("/")
  rpath.split(File::SEPARATOR).each do |rdir|
    rdir = rdir.strip
    unless rdir.blank?
      list = ftp.ls.collect { |f| f.split.last }
      unless list.include?(rdir)
       ftp.mkdir(rdir)
      end
      ftp.chdir(rdir)
    end
 end
end

#to_file(style = default_style) ⇒ Object Also known as: to_io



30
31
32
# File 'lib/paperclipftp.rb', line 30

def to_file style = default_style
  @queued_for_write[style] || "ftp://#{path(style)}"
end