Class: Apipie::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/static_dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileHandler

Returns a new instance of FileHandler.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/apipie/static_dispatcher.rb', line 4

def initialize(root)
  @root          = root.chomp('/')
  @compiled_root = /^#{Regexp.escape(root)}/
  @file_server   = if defined?(::Rack::Files)
                     ::Rack::Files.new(@root)
                   else
                     # Deprecated in Rack 3.0, kept
                     # for backward compatibility
                     ::Rack::File.new(@root)
                   end
end

Instance Method Details

#cache_extensionObject



44
45
46
47
48
49
50
51
# File 'lib/apipie/static_dispatcher.rb', line 44

def cache_extension
  if ::ActionController::Base.respond_to?(:default_static_extension)
    ::ActionController::Base.default_static_extension
  else
    ::ActionController::Base.page_cache_extension
  end

end

#call(env) ⇒ Object



33
34
35
# File 'lib/apipie/static_dispatcher.rb', line 33

def call(env)
  @file_server.call(env)
end

#extObject



37
38
39
40
41
42
# File 'lib/apipie/static_dispatcher.rb', line 37

def ext
  @ext ||= begin
    ext = cache_extension
    "{,#{ext},/index#{ext}}"
  end
end

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/apipie/static_dispatcher.rb', line 16

def match?(path)
  # Replace all null bytes
  path = ::Rack::Utils.unescape(path || '')
                      .encode(Encoding::UTF_8, invalid: :replace, replace: '')
                      .gsub("\x0", '')

  full_path = path.empty? ? @root : File.join(@root, path)
  paths = "#{full_path}#{ext}"

  matches = Dir[paths]
  match = matches.detect { |m| File.file?(m) }
  if match
    match.sub!(@compiled_root, '')
    match
  end
end