Class: FTPUtils
- Inherits:
-
Object
- Object
- FTPUtils
- Defined in:
- lib/ftputils.rb,
lib/ftputils/ftpuri.rb,
lib/ftputils/ftpfile.rb,
lib/ftputils/ftpconnection.rb
Defined Under Namespace
Classes: FTPConnection, FTPFile, FTPURI
Class Method Summary collapse
- .cp(src, dest, options = {}) ⇒ Object
- .cp_r(src, dest, options = {}) ⇒ Object
- .ls(path) ⇒ Object
- .mkdir_p(path) ⇒ Object
- .mv(src, dest, options = {}) ⇒ Object
- .rm(path) ⇒ Object
- .rm_r(path) ⇒ Object
Class Method Details
.cp(src, dest, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ftputils.rb', line 12 def self.cp(src, dest, = {}) # handle all combinations of copying to/from FTP and local files case [ftp_url?(src), ftp_url?(dest)] when [true, true] raise "src should be a filename, not a directory" if FTPFile.directory?(src) dest_path = FTPFile.dirname(dest) + "/" + ( FTPFile.basename(dest) || FTPFile.basename(src) ) FTPConnection.connect(src).fxpto(FTPConnection.connect(dest), dest_path, FTPFile.relative_path(src)) when [true, false] raise "src should be a filename, not a directory" if FTPFile.directory?(src) filename = FTPFile.basename(src) if File.directory? dest dest += "/#{filename}" end connection = FTPConnection.connect(src) connection.chdir FTPFile.dirname(src) connection.getbinaryfile filename, dest, 1024 when [false, true] raise "src should be a filename, not a directory" if File.directory? src dest_path = FTPFile.relative_path(dest) if FTPFile.directory?(dest) dest_path += "/#{File.basename(src)}" end connection = FTPConnection.connect(dest) connection.chdir FTPFile.dirname(dest) connection.putbinaryfile src, dest_path, 1024 when [false, false] FileUtils.cp src, dest, end end |
.cp_r(src, dest, options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ftputils.rb', line 100 def self.cp_r(src, dest, = {}) # handle all combinations of copying to/from FTP and local files if ftp_url?(src) if FTPFile.directory?(src) mkdir_p dest connection = FTPConnection.connect(src) files = connection.nlst files.each {|file| cp_r "#{src}/#{file}", "#{dest}/#{file}", } else cp(src, dest, ) end elsif ftp_url?(dest) if FTPFile.directory?(dest) mkdir_p dest files = Dir.entries(src) files.each {|file| cp_r "#{src}/#{file}", "#{dest}/#{file}", } else cp(src, dest, ) end else FileUtils.cp_r src, dest end end |
.ls(path) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ftputils.rb', line 126 def self.ls(path) if ftp_url?(path) if FTPFile.directory?(path) connection = FTPConnection.connect(path) connection.chdir FTPFile.relative_path(path) return connection.nlst else nil end else Dir.entries path end end |
.mkdir_p(path) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ftputils.rb', line 84 def self.mkdir_p(path) if ftp_url?(path) connection = FTPConnection.connect(path) subdirs = FTPFile.relative_path(path).split(/\//) subdirs.each do |subdir| next if subdir == "" connection.mkdir subdir connection.chdir subdir end else FileUtils.mkdir_p path end end |
.mv(src, dest, options = {}) ⇒ Object
61 62 63 64 |
# File 'lib/ftputils.rb', line 61 def self.mv(src, dest, = {}) cp(src, dest, ) rm(src) end |
.rm(path) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ftputils.rb', line 49 def self.rm(path) if ftp_url?(path) raise "Can't use FTPUtils.rm on directories. Instead use FTPUtils.rm_r" if FTPFile.directory?(path) connection = FTPConnection.connect(path) connection.chdir FTPFile.dirname(path) connection.delete FTPFile.basename(path) else FileUtils.rm path end end |
.rm_r(path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ftputils.rb', line 66 def self.rm_r(path) if ftp_url?(path) if FTPFile.directory?(path) connection = FTPConnection.connect(path) connection.chdir FTPFile.relative_path(path) files = connection.nlst files.each {|file| rm_r "#{path}/#{file}"} connection.rmdir FTPFile.relative_path(path) else rm(path) end else FileUtils.rm_r path end end |