Skip to content

Instantly share code, notes, and snippets.

@kmiscia
Created April 26, 2012 17:20
Show Gist options
  • Save kmiscia/2501081 to your computer and use it in GitHub Desktop.
Save kmiscia/2501081 to your computer and use it in GitHub Desktop.
class FtpRemoteFileUtils < RemoteFileUtils
attr_reader :destination_dir
CURL_COMMAND = OS.is_ubuntu ? "/usr/bin/curl" : "/usr/local/largecurl/bin/curl"
def initialize(params)
super(params)
raise ArgumentError unless params[:destination_dir]
@destination_dir = params[:destination_dir]
end
def mkdir(dir)
destination_dir = "#{@destination_dir}/#{clean_dir(dir)}"
`#{CURL_COMMAND} --ftp-port - --user #{@username}:#{@password} --disable-eprt ftp://#{@host}/#{destination_dir}/`
$?.success?
end
# dir param assumeed to be /mnt/[ip_address]/path/we/care/about
def mkdir_p(dir)
destination_dir = "#{@destination_dir}/#{clean_dir(dir)}"
`#{CURL_COMMAND} --ftp-port - --ftp-create-dirs --user psguser:psguser --disable-eprt ftp://#{@host}/#{destination_dir}/`
$?.success?
end
def directory?(dir)
dir = clean_dir(dir)
`#{CURL_COMMAND} --ftp-port - --user #{@username}:#{@password} --disable-eprt ftp://#{@host}/ -Q 'CWD #{dir}'`
return $?.success?
end
def glob(dir)
ip = OS.get_ip_address.split(".")
dir_list_raw = `curl --ftp-port - --user #{@username}:#{@password} --disable-eprt ftp://#{@host}/ -Q 'PORT #{ip[0]},#{ip[1]},#{ip[2]},#{ip[3]},212,161 LIST'`
dir_list_clean = dir_list_raw.scan(/\d\s(\w*|\D*)\r/)
dir_list_clean.collect {|d| d.match(/.*\.(SCH|sch)/)}
end
def mtime(file)
#TODO
end
def basename(file_with_path)
file_with_path.split("/").last
end
def mv(filename, destination_directory)
if filename.local? && destination_directory.remote?
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -o "#{filename}"`
FileUtils.rm(filename)
elsif filename.local? && destination_directory.local?
FileUtils.mv(filename, destination_directory)
elsif filename.remote? && destination_directory.local?
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -T "#{clean_destination}/#{basename(filename)}"`
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/#{clean_destination} -Q 'DELE #{basename(filename)}'
else # both remote
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -Q \"RNFR #{origin} RNTO #{destination}\"`
return $?.success?
end
end
def cp(filename, destination_directory)
clean_destination = clean_dir(destination)
if filename.local? && destination_directory.remote?
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -T "#{clean_destination}/#{basename(filename)}"`
return $?.success?
elsif filename.local? && destination_directory.local?
FileUtils.cp(filename, destination_directory)
elsif filename.remote? && destination_directory.local?
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -o "#{filename}"`
return $?.success?
else # both remote
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -o "#{filename}"`
`#{CURL_COMMAND} --ftp-port - --user #{username}:#{password} --disable-eprt ftp://#{@host}/ -T "#{clean_destination}/#{basename(filename)}"`
`rm #{basename(origin)}`
return $?.success?
end
end
private
def get
#TODO
end
def put
#TODO
end
# Removes autofs path elements we don't care about
# Eg. "/mnt/ftp/192.168.23.45/mount_23134645732/summary/myfile.txt" should return "/summary/myfile.txt"
def clean_dir(dir)
dir_elements = dir.split("/")
dir_elements.last(dir_elements.length - 5).join("/")
end
end
class String
def local?
!remote?
end
def remote?
self.start_with?("/mnt")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment