Class: ElFinderS3::Pathname

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

Direct Known Subclasses

S3Pathname

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_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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#+(other) ⇒ Object



43
44
45
46
47
48
# 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?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/el_finder_s3/pathname.rb', line 58

def absolute?
  @path.absolute?
end

#basename(*args) ⇒ Object



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

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

#basename_sans_extensionObject



128
129
130
# File 'lib/el_finder_s3/pathname.rb', line 128

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

#child_directories(with_directory = true) ⇒ Object



171
172
173
# 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) ⇒ Object



182
183
184
# 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

#cleanpathObject



107
108
109
# File 'lib/el_finder_s3/pathname.rb', line 107

def cleanpath
  fullpath.cleanpath
end

#directory?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/el_finder_s3/pathname.rb', line 75

def directory?
  realpath.directory?
rescue Errno::ENOENT
  false
end

#dirnameObject



135
136
137
# File 'lib/el_finder_s3/pathname.rb', line 135

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

#duplicateObject



212
213
214
215
216
217
218
219
220
221
222
223
224
# 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?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/el_finder_s3/pathname.rb', line 69

def exist?
  realpath.exist?
rescue Errno::ENOENT
  false
end

#extnameObject



142
143
144
# File 'lib/el_finder_s3/pathname.rb', line 142

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

#file?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/el_finder_s3/pathname.rb', line 81

def file?
  realpath.file?
rescue Errno::ENOENT
  false
end

#files(with_directory = true) ⇒ Object



176
177
178
# 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

#fullpathObject



98
99
100
101
102
# 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?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/el_finder_s3/pathname.rb', line 53

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

#outside_of_root?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/el_finder_s3/pathname.rb', line 91

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

#realpathObject



114
115
116
# File 'lib/el_finder_s3/pathname.rb', line 114

def realpath
  fullpath.realpath
end

#relative?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/el_finder_s3/pathname.rb', line 65

def relative?
  @path.relative?
end

#relative_to(other) ⇒ Object



194
195
196
# File 'lib/el_finder_s3/pathname.rb', line 194

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

#rename(to) ⇒ Object



229
230
231
232
233
# 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_sObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# 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_sObject Also known as: to_str



149
150
151
# File 'lib/el_finder_s3/pathname.rb', line 149

def to_s
  cleanpath.to_s
end

#touch(options = {}) ⇒ Object



187
188
189
190
191
# 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) ⇒ Object

of initialize



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

def type=(value)
  @type = value
  fullpath.type = value
end

#uniqueObject



199
200
201
202
203
204
205
206
207
# 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