Class: RUtilAnts::URLAccess::URLHandlers::FTP

Inherits:
Object
  • Object
show all
Defined in:
lib/rUtilAnts/URLHandlers/FTP.rb

Overview

Handler of FTP URLs

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iURL) ⇒ FTP

Constructor

Parameters
  • iURL (String): The URL that this handler will manage



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 24

def initialize(iURL)
  @URL = iURL
  lURLMatch = iURL.match(/^(ftp|ftps):\/\/([^\/]*)\/(.*)$/)
  if (lURLMatch == nil)
    lURLMatch = iURL.match(/^(ftp|ftps):\/\/(.*)$/)
  end
  if (lURLMatch == nil)
    log_bug "URL #{iURL} was identified as an ftp like, but it appears to be false."
  else
    @URLProtocol, @URLServer, @URLPath = lURLMatch[1..3]
  end
end

Class Method Details

.get_matching_regexpsObject

Get a list of regexps matching the URL to get to this handler

Return
  • list<Regexp>: The list of regexps matching URLs from this handler



14
15
16
17
18
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 14

def self.get_matching_regexps
  return [
    /^(ftp|ftps):\/\/.*$/
  ]
end

Instance Method Details

#get_content(iFollowRedirections) ⇒ Object

Get the content of the URL

Parameters
  • iFollowRedirections (Boolean): Do we follow redirections while accessing the content ?

Return
  • Integer: Type of content returned

  • Object: The content, depending on the type previously returned:

    • Exception if CONTENT_ERROR: The corresponding error

    • String if CONTENT_REDIRECT: The new URL

    • String if CONTENT_STRING: The real content

    • String if CONTENT_LOCALFILENAME: The name of the local file name storing the content

    • String if CONTENT_LOCALFILENAME_TEMPORARY: The name of the temporary local file name storing the content



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 86

def get_content(iFollowRedirections)
  rContentFormat = nil
  rContent = nil

  begin
    require 'net/ftp'
    lFTPConnection = Net::FTP.new(@URLServer)
    lFTPConnection.
    lFTPConnection.chdir(File.dirname(@URLPath))
    rContent = get_corresponding_file_base_name
    rContentFormat = CONTENT_LOCALFILENAME_TEMPORARY
    log_debug "URL #{@URL} => Temporary file #{rContent}"
    lFTPConnection.getbinaryfile(File.basename(@URLPath), rContent)
    lFTPConnection.close
  rescue Exception
    rContent = $!
    rContentFormat = CONTENT_ERROR
    log_debug "Error accessing #{@URL}: #{rContent}"
  end

  return rContentFormat, rContent
end

#get_corresponding_file_base_nameObject

Get a corresponding file base name. This method has to make sure file extensions are respected, as it can be used for further processing.

Return
  • String: The file name



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 59

def get_corresponding_file_base_name
  lBase = File.basename(@URLPath)
  lExt = File.extname(@URLPath)
  lFileName = nil
  if (lExt.empty?)
    lFileName = lBase
  else
    # Check that extension has no characters following the URL (#, ? and ;)
    lBase = lBase[0..lBase.size-lExt.size-1]
    lFileName = "#{lBase}#{lExt.gsub(/^([^#\?;]*).*$/,'\1')}"
  end

  return get_valid_file_name(lFileName)
end

#get_crcObject

Get the current CRC of the URL

Return
  • Integer: The CRC



49
50
51
52
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 49

def get_crc
  # We consider FTP URLs to be definitive: CRCs will never change.
  return 0
end

#get_server_idObject

Get the server ID

Return
  • String: The server ID



41
42
43
# File 'lib/rUtilAnts/URLHandlers/FTP.rb', line 41

def get_server_id
  return "#{@URLProtocol}://#{@URLServer}"
end