class GxG::Networking::FtpClient

Public Class Methods

new() click to toggle source
# File lib/gxg/net_clients.rb, line 4822
def initialize()
  @client = nil
  self
end

Public Instance Methods

change_directory(dir_name=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5157
def change_directory(dir_name=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        unless dir_name
          raise ArgumentError, "You MUST provide a directory path to change to."
        end
        @client.chdir(dir_name)
        if @client.pwd() != dir_name
          raise Exception, "Failed to change directory."
        else
          result = true
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:dir_name => dir_name}})
  end
  result
end
closed?() click to toggle source
# File lib/gxg/net_clients.rb, line 4912
def closed?()
  if @client
    @client.closed?
  else
    true
  end
end
entries(dir_name=nil, filter=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5103
def entries(dir_name=nil, filter=nil)
  result = []
  unless dir_name
    dir_name = @client.pwd()
  end
  begin
    if @client
      present_dir = @client.pwd()
      @client.chdir(dir_name)
      if filter
        list = @client.list(filter)
      else
        list = @client.list()
      end
      list.each do |entry|
        # TODO: process entry text
        data = entry.split(" ")
        record = {:title => "", :type => :virtual_directory, :owner_type => :virtual_directory, :on_device => nil, :on_device_major => nil, :on_device_minor => nil, :is_device => nil, :is_device_major => nil, :is_device_minor => nil, :inode => nil, :flags => [:read], :hardlinks_to => 0, :user_id => nil, :group_id => nil, :size => 0, :block_size => 0, :blocks => 0, :accessed => nil, :modified => nil, :status_modified => nil, :permissions => nil, :mode=>nil}
        #
        record[:permissions] = ::File.unix_permissions_to_gxg(data[0].to_s)
        if record[:permissions][:other][:write]
          record[:flags] << :write
        end
        #              record[:owner] = data[2]
        #              record[:group] = data[3]
        record[:size] = data[4].to_i
        record[:modified] = ::DateTime::parse(data[5..7].join(" "))
        record[:title] = data[8]
        result << record
      end
      @client.chdir(present_dir)
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:dir_name => dir_name, :filter => filter}})
  end
  result
end
get_file(file_name=nil, save_path=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5025
def get_file(file_name=nil, save_path=nil)
  result = nil
  begin
    if @client
      if save_path
        result = @client.getbinaryfile(file_name, save_path, 1024)
      else
        result = ::GxG::ByteArray.new
        @client.getbinaryfile(file_name, save_path) do |data_chunk|
          result << data_chunk
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:file_name => file_name, :save_path => save_path}})
  end
  result
end
help() click to toggle source
# File lib/gxg/net_clients.rb, line 4952
def help()
  result = nil
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed connection."
      else
        result = @client.help()
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end
login(the_url=nil, options={}) click to toggle source
# File lib/gxg/net_clients.rb, line 4827
def login(the_url=nil, options={})
  result = false
  login_options = {}
  begin
    unless the_url.is_a?(::URI::Generic)
      raise ArgumentError, "You MUST provide a valid URI"
    end
    # The available options are:
    #
    # port::      Port number (default value is 21)
    if options[:port].is_a?(::Numeric)
      login_options[:port] = options[:port].to_i
    end
    # ssl::       If options[:ssl] is true, then an attempt will be made
    #             to use SSL (now TLS) to connect to the server.  For this
    #             to work OpenSSL [OSSL] and the Ruby OpenSSL [RSSL]
    #             extensions need to be installed.  If options[:ssl] is a
    #             hash, it's passed to OpenSSL::SSL::SSLContext#set_params
    #             as parameters.
    if options[:use_ssl] == true
      login_options[:ssl] = true
    end
    # private_data_connection::  If true, TLS is used for data connections.
    #                            Default: +true+ when options[:ssl] is true.
    # username::  Username for login.  If options[:username] is the string
    #             "anonymous" and the options[:password] is +nil+,
    #             "anonymous@" is used as a password.
    login_options[:username] = the_url.user
    # password::  Password for login.
    login_options[:password] = the_url.password
    # account::   Account information for ACCT.
    if options[:account_info]
      login_options[:account] = options[:account_info]
    end
    # passive::   When +true+, the connection is in passive mode. Default:
    #             +true+.
    if options[:passive] == false
      login_options[:passive] = false
    end
    # open_timeout::  Number of seconds to wait for the connection to open.
    #                 See Net::FTP#open_timeout for details.  Default: +nil+.
    if options[:open_timeout].is_a?(::Numeric)
      login_options[:open_timeout] = options[:open_timeout]
    end
    # read_timeout::  Number of seconds to wait for one block to be read.
    #                 See Net::FTP#read_timeout for details.  Default: +60+.
    if options[:read_timeout].is_a?(::Numeric)
      login_options[:read_timeout] = options[:read_timeout]
    end
    # ssl_handshake_timeout::  Number of seconds to wait for the TLS
    #                          handshake.
    #                          See Net::FTP#ssl_handshake_timeout for
    #                          details.  Default: +nil+.
    if options[:ssl_handshake_timeout].is_a?(::Numeric)
      login_options[:ssl_handshake_timeout] = options[:ssl_handshake_timeout]
    end
    #
    @client = Net::FTP.new(the_url.hostname, login_options)
    if @client.is_a?(Net::FTP)
      @client.login
      if @client.closed?
        raise Exception, "Failed to log into FTP session"
      else
        result = true
      end
    else
      raise Exception, "Failed to create FTP session"
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:url => the_url, :options => options}})
  end
  result
end
logout() click to toggle source
# File lib/gxg/net_clients.rb, line 4901
def logout()
  if @client
    @client.close
    if @client.closed?
      true
    else
      false
    end
  end
end
make_directory(dir_name=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5181
def make_directory(dir_name=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        unless dir_name
          raise ArgumentError, "You MUST provide a directory path to create."
        end
        result = @client.mkdir(dir_name)
        unless result
          raise Exception, "Failed to change directory."
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:dir_name => dir_name}})
  end
  result
end
present_directory() click to toggle source
# File lib/gxg/net_clients.rb, line 5141
def present_directory()
  result = nil
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        result = @client.pwd()
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end
put_file(local_path=nil, remote_file=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5044
def put_file(local_path=nil, remote_file=nil)
  result = false
  begin
    if @client
      result = @client.putbinaryfile(local_path, remote_file, 1024)
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:local_path => local_path, :remote_file => remote_file}})
  end
  result
end
remove_directory(dir_name=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5203
def remove_directory(dir_name=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        unless dir_name
          raise ArgumentError, "You MUST provide a directory name to remove."
        end
        result = @client.rmdir(dir_name)
        unless result
          raise Exception, "Failed to remove directory."
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:dir_name => dir_name}})
  end
  result
end
remove_file(file_name=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5081
def remove_file(file_name=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        unless file_name
          raise ArgumentError, "You MUST provide a file name to remove."
        end
        result = @client.delete(file_name)
        unless result
          raise Exception, "Failed to remove file."
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:file_name => file_name}})
  end
  result
end
rename_file(file_name=nil, new_file_name=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 5056
def rename_file(file_name=nil, new_file_name=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed session"
      else
        unless file_name
          raise ArgumentError, "You MUST provide a current file name to rename."
        end
        unless new_file_name
          raise ArgumentError, "You MUST provide a new name to rename the file."
        end
        result = @client.rename(file_name, new_file_name)
        unless result
          raise Exception, "Failed to rename file."
        end
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:file_name => file_name, :new_file_name => new_file_name}})
  end
  result
end
reopen() click to toggle source
# File lib/gxg/net_clients.rb, line 5006
def reopen()
  result = false
  begin
    if @client
      if @client.closed?
        @client.connect()
        if @client.closed?
          raise Exception, "Failed to re-open connection."
        end
      else
        result = true
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end
send_account_info(acct_info=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 4968
def send_account_info(acct_info=nil)
  result = false
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed connection."
      else
        unless acct_info.is_a?(::String)
          raise ArgumentError, "You MUST provide account information (as a String)."
        end
        result = @client.acct(acct_info)
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:account_info => acct_info}})
  end
  result
end
send_command(command=nil) click to toggle source
# File lib/gxg/net_clients.rb, line 4987
def send_command(command=nil)
  result = nil
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed connection."
      else
        unless command.is_a?(::String)
          raise ArgumentError, "You must provide a String to issue FTP commands."
        end
        result = @client.sendcmd(command)
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {:command => command}})
  end
  result
end
status() click to toggle source
# File lib/gxg/net_clients.rb, line 4920
def status()
  result = nil
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed connection."
      else
        result = @client.status()
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end
system_info() click to toggle source
# File lib/gxg/net_clients.rb, line 4936
def system_info()
  result = nil
  begin
    if @client
      if @client.closed?
        raise Exception, "Attempted to access a closed connection."
      else
        result = @client.system()
      end
    end
  rescue Exception => the_error
    log_error({:error => the_error, :parameters => {}})
  end
  result
end