rack-grid-serve 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/grid_serve.rb +65 -27
- data/test.rb +7 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00c045ab328e8d6c02e99b63a60aaa0b969c50b9
|
4
|
+
data.tar.gz: 4191a3f5970bc899555e4b0b94ad603f83229983
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f38407d72fe33bd7f0c756562a43952f4bb7687741d394aa33c23616138c90a74ab2832841ff06554e9d2c7ab26dcce69579e980f1d0121e8ca826b42a465beb
|
7
|
+
data.tar.gz: 114504fbfb3cd42f95bcc063ec2b74672fa84dd0e77a885055b28927fe786871117b24eb7b0523da2d5aaf90a871e1914dc2e3d706989ec3344ec2ae3f47843b
|
data/lib/rack/grid_serve.rb
CHANGED
@@ -3,46 +3,72 @@ require 'rack/request'
|
|
3
3
|
require 'rack/utils'
|
4
4
|
begin
|
5
5
|
require 'rack/conditional_get'
|
6
|
-
rescue LoadError
|
6
|
+
rescue LoadError
|
7
7
|
require 'rack/conditionalget'
|
8
8
|
end
|
9
9
|
|
10
10
|
class Rack::GridServe
|
11
11
|
|
12
|
-
VERSION = '0.0.
|
12
|
+
VERSION = '0.0.9'.freeze
|
13
|
+
|
14
|
+
DEFAULT_PREFIX = 'gridfs'.freeze
|
15
|
+
DEFAULT_CACHE_CONTROL = 'no-cache'.freeze
|
16
|
+
EDGE_SLASHES_RE = /\A\/|\/\z/.freeze
|
17
|
+
EMPTY_STRING = ''.freeze
|
13
18
|
|
14
19
|
def initialize app, opts={}
|
15
20
|
@app = app
|
16
21
|
@db = opts[:db]
|
17
|
-
@prefix = (opts[:prefix] ||
|
18
|
-
@
|
22
|
+
@prefix = (opts[:prefix] || DEFAULT_PREFIX).gsub(EDGE_SLASHES_RE, EMPTY_STRING)
|
23
|
+
@prefix = "/#{@prefix}/".freeze
|
24
|
+
@cache_control = opts[:cache_control] || DEFAULT_CACHE_CONTROL
|
19
25
|
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
27
|
+
PATH_INFO = 'PATH_INFO'.freeze
|
28
|
+
CONTENT_TYPE = 'Content-Type'.freeze
|
29
|
+
CONTENT_LENGTH = 'Content-Length'.freeze
|
30
|
+
LAST_MODIFIED = 'Last-Modified'.freeze
|
31
|
+
CACHE_CONTROL = 'Cache-Control'.freeze
|
32
|
+
ETAG = 'ETag'.freeze
|
33
|
+
TEXT_PLAIN = 'text/plain'.freeze
|
34
|
+
NOT_FOUND = 'Not Found'.freeze
|
35
|
+
NOT_FOUND_RESPONSE = [
|
36
|
+
404,
|
37
|
+
{
|
38
|
+
CONTENT_TYPE => TEXT_PLAIN
|
39
|
+
}.freeze,
|
40
|
+
[NOT_FOUND].freeze
|
41
|
+
].freeze
|
42
|
+
UPLOAD_DATE_KEY = 'uploadDate'.freeze
|
43
|
+
CONTENT_TYPE_KEY = 'contentType'.freeze
|
44
|
+
ID_KEY = '_id'.freeze
|
45
|
+
MD5_KEY = 'md5'.freeze
|
24
46
|
|
25
|
-
def
|
26
|
-
|
27
|
-
if under_prefix?
|
28
|
-
file = find_file
|
47
|
+
def call env
|
48
|
+
path_info = env[PATH_INFO].to_s
|
49
|
+
if under_prefix? path_info
|
50
|
+
file = find_file path_info
|
29
51
|
if file.nil?
|
30
|
-
|
52
|
+
NOT_FOUND_RESPONSE
|
31
53
|
else
|
32
|
-
last_modified = Time.at file[
|
54
|
+
last_modified = Time.at file[UPLOAD_DATE_KEY].to_i
|
33
55
|
headers = {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
56
|
+
CONTENT_TYPE => file[CONTENT_TYPE_KEY],
|
57
|
+
ETAG => file[MD5_KEY],
|
58
|
+
LAST_MODIFIED => last_modified.httpdate,
|
59
|
+
CACHE_CONTROL => @cache_control
|
38
60
|
}
|
39
|
-
|
61
|
+
if can_use_cached?(env, file[MD5_KEY])
|
62
|
+
headers.delete(CONTENT_TYPE)
|
63
|
+
headers.delete(CONTENT_LENGTH)
|
64
|
+
[304, headers, []]
|
65
|
+
else
|
40
66
|
content = String.new
|
41
|
-
@db.fs.open_download_stream(file[
|
67
|
+
@db.fs.open_download_stream(file[ID_KEY]) do |stream|
|
42
68
|
content = stream.read
|
43
69
|
end
|
44
70
|
[200, headers, [content]]
|
45
|
-
|
71
|
+
end
|
46
72
|
end
|
47
73
|
else
|
48
74
|
@app.call env
|
@@ -51,12 +77,12 @@ class Rack::GridServe
|
|
51
77
|
|
52
78
|
private
|
53
79
|
|
54
|
-
def under_prefix?
|
55
|
-
|
80
|
+
def under_prefix? path_info
|
81
|
+
path_info.start_with?(@prefix) and path_info.size > @prefix.size
|
56
82
|
end
|
57
83
|
|
58
|
-
def id_or_filename
|
59
|
-
str =
|
84
|
+
def id_or_filename path_info
|
85
|
+
str = path_info.sub @prefix, EMPTY_STRING
|
60
86
|
if BSON::ObjectId.legal? str
|
61
87
|
BSON::ObjectId.from_string str
|
62
88
|
else
|
@@ -64,13 +90,15 @@ class Rack::GridServe
|
|
64
90
|
end
|
65
91
|
end
|
66
92
|
|
67
|
-
|
68
|
-
|
93
|
+
OR = '$or'.freeze
|
94
|
+
|
95
|
+
def find_file path_info
|
96
|
+
str = id_or_filename path_info
|
69
97
|
if str.is_a? BSON::ObjectId
|
70
98
|
@db.fs.find({_id: str}).first
|
71
99
|
else
|
72
100
|
@db.fs.find({
|
73
|
-
|
101
|
+
OR => [
|
74
102
|
{filename: str},
|
75
103
|
{filename: "/#{str}"}
|
76
104
|
]
|
@@ -78,5 +106,15 @@ class Rack::GridServe
|
|
78
106
|
end
|
79
107
|
end
|
80
108
|
|
109
|
+
READ_METHODS = ['GET'.freeze, 'HEAD'.freeze].freeze
|
110
|
+
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
111
|
+
HTTP_IF_NONE_MATCH = 'HTTP_IF_NONE_MATCH'.freeze
|
112
|
+
|
113
|
+
def can_use_cached? env, md5
|
114
|
+
READ_METHODS.include?(env[REQUEST_METHOD]) and
|
115
|
+
env[HTTP_IF_NONE_MATCH] and
|
116
|
+
env[HTTP_IF_NONE_MATCH] == md5
|
117
|
+
end
|
118
|
+
|
81
119
|
end
|
82
120
|
|
data/test.rb
CHANGED
@@ -4,6 +4,8 @@ require 'rack/grid_serve'
|
|
4
4
|
require 'rack/test'
|
5
5
|
require 'minitest/autorun'
|
6
6
|
|
7
|
+
Mongo::Logger.logger.level = Logger::ERROR
|
8
|
+
|
7
9
|
MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'rack-grid-serve-test')
|
8
10
|
DB = MONGO.database
|
9
11
|
DB.drop
|
@@ -43,7 +45,7 @@ class TestRackGridServe < MiniTest::Test
|
|
43
45
|
include Helpers
|
44
46
|
|
45
47
|
def app
|
46
|
-
Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB))
|
48
|
+
Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB).freeze).freeze
|
47
49
|
end
|
48
50
|
|
49
51
|
def test_finds_file_by_id
|
@@ -108,11 +110,13 @@ class TestRackGridServePrefix < MiniTest::Test
|
|
108
110
|
include Helpers
|
109
111
|
|
110
112
|
def app
|
111
|
-
Rack::Lint.new(
|
113
|
+
Rack::Lint.new(
|
114
|
+
Rack::GridServe.new(inner_app, db: DB, prefix: '/attachment/prefix/').freeze
|
115
|
+
).freeze
|
112
116
|
end
|
113
117
|
|
114
118
|
def test_finds_file_with_custom_prefix
|
115
|
-
prefix = "/attachment/prefix/"
|
119
|
+
# prefix = "/attachment/prefix/"
|
116
120
|
get '/attachment/prefix/tmnt.css'
|
117
121
|
assert_file_found
|
118
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-grid-serve
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mickael Riga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.
|
105
|
+
rubygems_version: 2.4.5.1
|
106
106
|
signing_key:
|
107
107
|
specification_version: 4
|
108
108
|
summary: Rack middleware for serving files stored in MongoDB GridFS
|