rack-app 7.6.5 → 7.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/rack/app/file_server.rb +38 -12
- data/lib/rack/app/singleton_methods/mounting.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf6acb7c4c89bd038387d214bd7379ade393a3f0b48088f4b7542b6a0c6f05ca
|
4
|
+
data.tar.gz: e084eaa3c611498a98bbdb7cb1a8ccbcdc81cc893e1c08848a515db4b9e536f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09f143bdb3a084226e0f51f9f8c471dff28b06ab99f4f52c0895abd3541673152ef3949f34711a5eb67fc53ce1544c0bfd0acf6d000b30cf803c5b28f96ce1c1'
|
7
|
+
data.tar.gz: 6667a98608f80d53984ec9147373e370edecec9b5b525b74c3ff0c354f0843ea685deb2b96b57cae354039f56b1d1ab8104ae20bb1cf884bfba98c36b741c537
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
7.
|
1
|
+
7.7.0
|
data/lib/rack/app/file_server.rb
CHANGED
@@ -1,10 +1,24 @@
|
|
1
1
|
class Rack::App::FileServer
|
2
|
+
attr_accessor :relative_file_paths
|
2
3
|
|
3
|
-
def
|
4
|
-
|
4
|
+
def self.serve_file(env, file_path)
|
5
|
+
dir_path = File.dirname(file_path)
|
6
|
+
basename = File.basename(file_path)
|
7
|
+
file_server = new(dir_path, map_relative_file_paths: false)
|
8
|
+
env = env.dup
|
9
|
+
env[::Rack::App::Constants::ENV::REQUEST_METHOD] = 'GET'
|
10
|
+
env[::Rack::App::Constants::ENV::PATH_INFO] = basename
|
11
|
+
file_server.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(root_folder, opts = {})
|
5
15
|
@root_folder = root_folder
|
6
|
-
@relative_file_paths =
|
16
|
+
@relative_file_paths = []
|
7
17
|
@rack_file_server = ::Rack::File.new(@root_folder, {})
|
18
|
+
|
19
|
+
if map_relative_file_paths?(opts)
|
20
|
+
map_relative_paths!
|
21
|
+
end
|
8
22
|
end
|
9
23
|
|
10
24
|
def call(env)
|
@@ -12,7 +26,7 @@ class Rack::App::FileServer
|
|
12
26
|
|
13
27
|
@relative_file_paths.each do |relative_file_path|
|
14
28
|
if path_info =~ /#{Regexp.escape(relative_file_path)}$/
|
15
|
-
env[::Rack::App::Constants::ENV::PATH_INFO]= relative_file_path
|
29
|
+
env[::Rack::App::Constants::ENV::PATH_INFO] = relative_file_path
|
16
30
|
break
|
17
31
|
end
|
18
32
|
end
|
@@ -20,19 +34,31 @@ class Rack::App::FileServer
|
|
20
34
|
@rack_file_server.call(env)
|
21
35
|
end
|
22
36
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
37
|
+
protected
|
38
|
+
|
39
|
+
def map_relative_file_paths?(opts = {})
|
40
|
+
unless opts.key?(:map_relative_file_paths)
|
41
|
+
return true
|
42
|
+
end
|
43
|
+
|
44
|
+
opts[:map_relative_file_paths]
|
29
45
|
end
|
30
46
|
|
31
|
-
|
47
|
+
def opts_set_defaults(opts)
|
48
|
+
unless opts.key?(:map_relative_file_paths)
|
49
|
+
opts[:map_relative_file_paths] = true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def map_relative_paths!
|
54
|
+
@relative_file_paths = Dir.glob(File.join(@root_folder, '**', '*'))
|
55
|
+
.map { |file_path| file_path.sub(@root_folder, '') }
|
56
|
+
.sort_by { |str| str.length }
|
57
|
+
.reverse
|
58
|
+
end
|
32
59
|
|
33
60
|
def clean_path_info(env)
|
34
61
|
path_info = ::Rack::Utils.unescape(env[::Rack::App::Constants::ENV::PATH_INFO])
|
35
62
|
::Rack::Utils.clean_path_info(path_info)
|
36
63
|
end
|
37
|
-
|
38
64
|
end
|
@@ -46,8 +46,8 @@ module Rack::App::SingletonMethods::Mounting
|
|
46
46
|
nil
|
47
47
|
end
|
48
48
|
|
49
|
-
def serve_files_from(
|
50
|
-
file_server = Rack::App::FileServer.new(Rack::App::Utils.expand_path(
|
49
|
+
def serve_files_from(dir_path, options={})
|
50
|
+
file_server = Rack::App::FileServer.new(Rack::App::Utils.expand_path(dir_path))
|
51
51
|
request_path = Rack::App::Utils.join(options[:to], Rack::App::Constants::PATH::MOUNT_POINT)
|
52
52
|
add_route(::Rack::App::Constants::HTTP::METHOD::ANY, request_path, file_server)
|
53
53
|
nil
|