Class: ElFinderFtp::Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/el_finder_ftp/pathname.rb

Direct Known Subclasses

FtpPathname

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_or_root, path = '.') ⇒ Pathname

Returns a new instance of Pathname.

Raises:

  • (SecurityError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/el_finder_ftp/pathname.rb', line 10

def initialize(adapter_or_root, path = '.')
  @adapter = adapter_or_root.is_a?(ElFinderFtp::Pathname) ? adapter_or_root.adapter : adapter_or_root

  @root = path.is_a?(ElFinderFtp::Pathname) ? path.root : FtpPathname.new(@adapter, '/')

  if path.is_a?(ElFinderFtp::Pathname) 
    @path = path.path 
  elsif path.is_a?(ElFinderFtp::FtpPathname) 
    @path = path
  else
    @path = FtpPathname.new(@adapter, path)
  end
  if absolute?
    if @path.cleanpath.to_s.start_with?(@root.to_s)
      @path = FtpPathname.new( @adapter, @path.to_s.slice((@root.to_s.length)..-1), @path.attrs)
    elsif @path.cleanpath.to_s.start_with?(@root.realpath.to_s)
      @path = FtpPathname.new( @adapter, @path.to_s.slice((@root.realpath.to_s.length)..-1), @path.attrs)
    else
      raise SecurityError, "Absolute paths are not allowed" 
    end
  end
  raise SecurityError, "Paths outside the root are not allowed" if outside_of_root?

end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/el_finder_ftp/pathname.rb', line 7

def adapter
  @adapter
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/el_finder_ftp/pathname.rb', line 7

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/el_finder_ftp/pathname.rb', line 7

def root
  @root
end

Instance Method Details

#+(other) ⇒ Object



36
37
38
39
40
41
# File 'lib/el_finder_ftp/pathname.rb', line 36

def +(other)
  if other.is_a? ::ElFinderFtp::Pathname
    other = other.path
  end
  self.class.new(@adapter, @path + other)
end

#absolute?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/el_finder_ftp/pathname.rb', line 49

def absolute?
  @path.absolute?
end

#basename(*args) ⇒ Object



79
80
81
# File 'lib/el_finder_ftp/pathname.rb', line 79

def basename(*args)
  @path.basename(*args)
end

#basename_sans_extensionObject



84
85
86
# File 'lib/el_finder_ftp/pathname.rb', line 84

def basename_sans_extension
  @path.basename(@path.extname)
end

#child_directories(with_directory = true) ⇒ Object



110
111
112
# File 'lib/el_finder_ftp/pathname.rb', line 110

def child_directories(with_directory=true)
  adapter.children(self, with_directory).select{ |child| child.directory? }.map{|e| self.class.new(@adapter, e)}
end

#children(with_directory = true) ⇒ Object



121
122
123
# File 'lib/el_finder_ftp/pathname.rb', line 121

def children(with_directory=true)
  adapter.children(self, with_directory).map{|e| self.class.new(@adapter, e)}
end

#cleanpathObject



69
70
71
# File 'lib/el_finder_ftp/pathname.rb', line 69

def cleanpath
  fullpath.cleanpath
end

#dirnameObject



94
95
96
# File 'lib/el_finder_ftp/pathname.rb', line 94

def dirname
  self.class.new(@adapter, @path.dirname)
end

#duplicateObject



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/el_finder_ftp/pathname.rb', line 147

def duplicate
  _basename = basename_sans_extension
  copy = 1
  if _basename.to_s =~ /^(.*) copy (\d+)$/
    _basename = $1
    copy = $2.to_i
  end
  begin
    new_file = self.class.new(@adapter, dirname + "#{_basename} copy #{copy}#{extname}")
    copy += 1
  end while new_file.exist?
  new_file
end

#extnameObject



99
100
101
# File 'lib/el_finder_ftp/pathname.rb', line 99

def extname
  @path.nil? ? '' : @path.extname
end

#files(with_directory = true) ⇒ Object



115
116
117
# File 'lib/el_finder_ftp/pathname.rb', line 115

def files(with_directory=true)
  adapter.children(self, with_directory).select{ |child| child.file? }.map{|e| self.class.new(@adapter, e)}
end

#fullpathObject



64
65
66
# File 'lib/el_finder_ftp/pathname.rb', line 64

def fullpath
  @fullpath ||= (@path.nil? ? @root : @root + @path)
end

#is_root?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/el_finder_ftp/pathname.rb', line 44

def is_root?
  @path.to_s == '.'
end

#outside_of_root?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/el_finder_ftp/pathname.rb', line 59

def outside_of_root?
  !cleanpath.to_s.start_with?(@root.to_s)
end

#realpathObject



74
75
76
# File 'lib/el_finder_ftp/pathname.rb', line 74

def realpath
  fullpath.realpath
end

#relative?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/el_finder_ftp/pathname.rb', line 54

def relative?
  @path.relative?
end

#relative_to(other) ⇒ Object



131
132
133
# File 'lib/el_finder_ftp/pathname.rb', line 131

def relative_to(other)
  @path.relative_path_from(other)
end

#rename(to) ⇒ Object



162
163
164
165
166
# File 'lib/el_finder_ftp/pathname.rb', line 162

def rename(to)
  to = self.class.new(@adapter, to)
  realpath.rename(to.fullpath.to_s)
  to
end

#to_sObject Also known as: to_str



104
105
106
# File 'lib/el_finder_ftp/pathname.rb', line 104

def to_s
  cleanpath.to_s
end

#touch(options = {}) ⇒ Object



126
127
128
# File 'lib/el_finder_ftp/pathname.rb', line 126

def touch(options = {})
  adapter.touch(cleanpath, options)
end

#uniqueObject



136
137
138
139
140
141
142
143
144
# File 'lib/el_finder_ftp/pathname.rb', line 136

def unique
  return self.dup unless fullpath.file?
  copy = 1
  begin
    new_file = self.class.new(@adapter, dirname + "#{basename_sans_extension} #{copy}#{extname}")
    copy += 1
  end while new_file.exist?
  new_file
end