Class: ElFinder::Pathname

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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
# File 'lib/el_finder/pathname.rb', line 10

def initialize(root, path = '.')
  @root = root.is_a?(ElFinder::Pathname) ? root.root : ::Pathname.new(root)

  @path = ::Pathname.new(path)
  @path = path.is_a?(ElFinder::Pathname) ? path.path : ::Pathname.new(path)
  if absolute?
    if @path.cleanpath.to_s.start_with?(@root.to_s)
      @path = ::Pathname.new @path.to_s.slice((@root.to_s.length + 1)..-1)
    elsif @path.cleanpath.to_s.start_with?(@root.realpath.to_s)
      @path = ::Pathname.new @path.to_s.slice((@root.realpath.to_s.length + 1)..-1)
    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

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#+(other) ⇒ Object



29
30
31
32
33
34
# File 'lib/el_finder/pathname.rb', line 29

def +(other)
  if other.is_a? ::ElFinder::Pathname
    other = other.path
  end
  self.class.new(@root, (@path + other).to_s)
end

#absolute?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/el_finder/pathname.rb', line 42

def absolute?
  @path.absolute?
end

#basename(*args) ⇒ Object



72
73
74
# File 'lib/el_finder/pathname.rb', line 72

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

#basename_sans_extensionObject



77
78
79
# File 'lib/el_finder/pathname.rb', line 77

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

#child_directories(with_directory = true) ⇒ Object



103
104
105
# File 'lib/el_finder/pathname.rb', line 103

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

#children(with_directory = true) ⇒ Object



108
109
110
# File 'lib/el_finder/pathname.rb', line 108

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

#cleanpathObject



62
63
64
# File 'lib/el_finder/pathname.rb', line 62

def cleanpath
  fullpath.cleanpath
end

#dirnameObject



87
88
89
# File 'lib/el_finder/pathname.rb', line 87

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

#duplicateObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/el_finder/pathname.rb', line 134

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(@root, dirname + "#{_basename} copy #{copy}#{extname}")
    copy += 1
  end while new_file.exist?
  new_file
end

#extnameObject



92
93
94
# File 'lib/el_finder/pathname.rb', line 92

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

#fullpathObject



57
58
59
# File 'lib/el_finder/pathname.rb', line 57

def fullpath
  @path.nil? ? @root : @root + @path
end

#is_root?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/el_finder/pathname.rb', line 37

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

#outside_of_root?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/el_finder/pathname.rb', line 52

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

#realpathObject



67
68
69
# File 'lib/el_finder/pathname.rb', line 67

def realpath
  fullpath.realpath
end

#relative?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/el_finder/pathname.rb', line 47

def relative?
  @path.relative?
end

#relative_to(other) ⇒ Object



118
119
120
# File 'lib/el_finder/pathname.rb', line 118

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

#rename(to) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/el_finder/pathname.rb', line 149

def rename(to)
  to = self.class.new(@root, to.to_s)
  realpath.rename(to.fullpath.to_s)
rescue Errno::EXDEV
  FileUtils.move(realpath.to_s, to.fullpath.to_s)
ensure
  @path = to.path
end

#to_sObject Also known as: to_str



97
98
99
# File 'lib/el_finder/pathname.rb', line 97

def to_s
  cleanpath.to_s
end

#touch(options = {}) ⇒ Object



113
114
115
# File 'lib/el_finder/pathname.rb', line 113

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

#uniqueObject



123
124
125
126
127
128
129
130
131
# File 'lib/el_finder/pathname.rb', line 123

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