Class: FTPUtils::FTPFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ftputils/ftpfile.rb

Class Method Summary collapse

Class Method Details

.basename(path, suffix = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ftputils/ftpfile.rb', line 4

def self.basename(path, suffix=nil)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    if suffix
      return ftp_uri.filename.gsub!(/#{suffix}\Z/,'')
    else
      return ftp_uri.filename
    end
  else
    if suffix
      return File.basename(path, suffix)
    else
      return File.basename(path)
    end
  end
end

.directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ftputils/ftpfile.rb', line 20

def self.directory?(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    begin
      connection = FTPUtils::FTPConnection.connect(path)
      connection.chdir(ftp_uri.path)
      
      return true
    rescue Net::FTPPermError
      return false
    end
  else
    return File.directory? path
  end
end

.dirname(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ftputils/ftpfile.rb', line 35

def self.dirname(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    return ftp_uri.dirname
  else
    return File.dirname(path)
  end
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ftputils/ftpfile.rb', line 43

def self.exists?(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    connection = FTPUtils::FTPConnection.connect(path)
    connection.chdir ftp_uri.dirname

    begin
      if connection.size(ftp_uri.filename) > 0
        return true
      else
        return false
      end
    rescue
      return false
    end
  else
    return File.exists?(path)
  end
end

.expand_path(path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ftputils/ftpfile.rb', line 62

def self.expand_path(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    return path
  else
    return File.expand_path(path)
  end
end

.file?(path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ftputils/ftpfile.rb', line 70

def self.file?(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    connection = FTPUtils::FTPConnection.connect(path)
    connection.chdir(ftp_uri.dirname)

    begin
      connection.size(ftp_uri.filename)
      return true
    rescue Net::FTPPermError
      return false
    end
  else
    return File.file? path
  end
end

.mtime(path) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/ftputils/ftpfile.rb', line 86

def self.mtime(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    connection = FTPUtils::FTPConnection.connect(path)
    connection.chdir(ftp_uri.dirname)

    return connection.mtime(ftp_uri.filename)
  else
    return File.mtime path
  end
end

.relative_path(path) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/ftputils/ftpfile.rb', line 97

def self.relative_path(path)
  if ftp_uri = FTPUtils::FTPURI.parse(path)
    return ftp_uri.path
  else
    return nil
  end
end