Sha256: f189a5bd4f87e25b338218c546b85feb8f5501d90f4ef55b2721920317ff9ca2
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
class Rack::App::FileServer attr_accessor :relative_file_paths def self.serve_file(env, file_path) dir_path = File.dirname(file_path) basename = File.basename(file_path) file_server = new(dir_path, map_relative_file_paths: false) env = env.dup env[::Rack::App::Constants::ENV::REQUEST_METHOD] = 'GET' env[::Rack::App::Constants::ENV::PATH_INFO] = basename file_server.call(env) end def initialize(root_folder, opts = {}) @root_folder = root_folder @relative_file_paths = [] @rack_file_server = ::Rack::File.new(@root_folder, {}) if map_relative_file_paths?(opts) map_relative_paths! end end def call(env) path_info = clean_path_info(env) @relative_file_paths.each do |relative_file_path| if path_info =~ /#{Regexp.escape(relative_file_path)}$/ env[::Rack::App::Constants::ENV::PATH_INFO] = relative_file_path break end end @rack_file_server.call(env) end protected def map_relative_file_paths?(opts = {}) unless opts.key?(:map_relative_file_paths) return true end opts[:map_relative_file_paths] end def opts_set_defaults(opts) unless opts.key?(:map_relative_file_paths) opts[:map_relative_file_paths] = true end end def map_relative_paths! @relative_file_paths = Dir.glob(File.join(@root_folder, '**', '*')) .map { |file_path| file_path.sub(@root_folder, '') } .sort_by { |str| str.length } .reverse end def clean_path_info(env) path_info = ::Rack::Utils.unescape(env[::Rack::App::Constants::ENV::PATH_INFO]) ::Rack::Utils.clean_path_info(path_info) end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
mountapi-0.11.1 | vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/file_server.rb |
rack-app-7.7.0 | lib/rack/app/file_server.rb |