class ElFinderS3::Pathname

Attributes

adapter[R]
path[R]
root[R]

Public Class Methods

new(adapter_or_root, path = '.') click to toggle source
# File lib/el_finder_s3/pathname.rb, line 10
def initialize(adapter_or_root, path = '.')
  @adapter = adapter_or_root.is_a?(ElFinderS3::Pathname) ? adapter_or_root.adapter : adapter_or_root

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

  if path.is_a?(ElFinderS3::Pathname)
    @path = path.path
  elsif path.is_a?(ElFinderS3::S3Pathname)
    @path = path
  else
    @path = S3Pathname.new(@adapter, path)
  end
  if absolute?
    if @path.cleanpath.to_s.start_with?(@root.to_s)
      @path = S3Pathname.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 = S3Pathname.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

Public Instance Methods

+(other) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 43
def +(other)
  if other.is_a? ::ElFinderS3::Pathname
    other = other.path
  end
  self.class.new(@adapter, @path + other)
end
absolute?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 58
def absolute?
  @path.absolute?
end
basename(*args) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 121
def basename(*args)
  @path.basename(*args)
end
basename_sans_extension() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 128
def basename_sans_extension
  @path.basename(@path.extname)
end
child_directories(with_directory=true) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 171
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) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 182
def children(with_directory=true)
  adapter.children(self, with_directory).map { |e| self.class.new(@adapter, e) }
end
cleanpath() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 107
def cleanpath
  fullpath.cleanpath
end
directory?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 75
def directory?
  realpath.directory?
rescue Errno::ENOENT
  false
end
dirname() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 135
def dirname
  self.class.new(@adapter, @path.dirname)
end
duplicate() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 212
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
exist?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 69
def exist?
  realpath.exist?
rescue Errno::ENOENT
  false
end
extname() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 142
def extname
  @path.nil? ? '' : @path.extname
end
file?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 81
def file?
  realpath.file?
rescue Errno::ENOENT
  false
end
files(with_directory=true) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 176
def files(with_directory=true)
  adapter.children(self, with_directory).select { |child| child.file? }.map { |e| self.class.new(@adapter, e) }
end
fullpath() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 98
def fullpath
  @fullpath ||= (@path.nil? ? @root : @root + @path)
  b = @path.nil? ? @root : @root + @path
  return @fullpath
end
is_root?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 53
def is_root?
  @path.to_s == '.'
end
outside_of_root?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 91
def outside_of_root?
  !cleanpath.to_s.start_with?(@root.to_s)
end
realpath() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 114
def realpath
  fullpath.realpath
end
relative?() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 65
def relative?
  @path.relative?
end
relative_to(other) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 194
def relative_to(other)
  @path.relative_path_from(other)
end
rename(to) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 229
def rename(to)
  to = self.class.new(@adapter, to)
  realpath.rename(to.fullpath.to_s)
  to
end
to_prefix_s() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 153
def to_prefix_s
  prefix_s = cleanpath.to_s
  if prefix_s == '/'
    return ''
  elsif prefix_s[0] == '/'
    prefix_s[0] = ''
  end

  if prefix_s[prefix_s.size-1] != '/'
    prefix_s = prefix_s + '/'
  end
  return prefix_s
end
to_s() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 149
def to_s
  cleanpath.to_s
end
Also aliased as: to_str
to_str()

of to_s

Alias for: to_s
touch(options = {}) click to toggle source
# File lib/el_finder_s3/pathname.rb, line 187
def touch(options = {})
  file2Touch = cleanpath
  self.file? ? file2Touch.type = :file : file2Touch.type = :directory
  adapter.touch(file2Touch, options)
end
type=(value) click to toggle source

of initialize

# File lib/el_finder_s3/pathname.rb, line 37
def type=(value)
  @type = value
  fullpath.type = value
end
unique() click to toggle source
# File lib/el_finder_s3/pathname.rb, line 199
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