rack-grid-serve 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack/grid_serve.rb +16 -11
  3. data/test.rb +63 -17
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66bbd379048cb82f31b37e0bcb709b134c03f79b
4
- data.tar.gz: 9276473a170cca1d1a43722293f80312f6554874
3
+ metadata.gz: 6deb8f0b7a078322007d82d31e83f659c86049f3
4
+ data.tar.gz: 6cdb2ae95ce99e60864862fb1b48d338e393c8e8
5
5
  SHA512:
6
- metadata.gz: 0613287fda9a502c945447ae47be72e675dd83e1fe98170e06c27d288cefaf8b6e938e2ac9f2e008aaed58d0e0e333a2b5b7ee1a0a1588f590a709c50882b6ee
7
- data.tar.gz: a97f26ba15c0541a21cc109b92196efaf15f40f4b0a821bf43d2f03fef88aec4762297be869fc6c5184ea27bf8623e2d077408ae2d386faf5da18202b426c990
6
+ metadata.gz: c324950ed06aa204cacbeba0fc6cc804a7f00392c4bf78768d5018ca0afd3dc023dc68001f4fdf3a4c282b83ca001ec9a6e3f3b8e611cbc3262c5ad7b71ca655
7
+ data.tar.gz: b6a802f04746d1e9a18fc1f1f89e6f104801c9b52d4f83e541c96f627e53b16b62cd8d3d413b81e9f852530d39b082c668c8368b88acdb9dfc4884cff964589a
@@ -1,5 +1,6 @@
1
1
  require 'mongo'
2
2
  require 'rack/request'
3
+ require 'rack/utils'
3
4
  begin
4
5
  require 'rack/conditional_get'
5
6
  rescue LoadError => ex
@@ -8,12 +9,12 @@ end
8
9
 
9
10
  class Rack::GridServe
10
11
 
11
- VERSION = '0.0.7'
12
+ VERSION = '0.0.8'
12
13
 
13
14
  def initialize app, opts={}
14
15
  @app = app
15
16
  @db = opts[:db]
16
- @prefix = opts[:prefix] || 'gridfs'
17
+ @prefix = (opts[:prefix] || 'gridfs').gsub(/^\/|\/$/, '')
17
18
  @cache_control = opts[:cache_control] || 'no-cache'
18
19
  end
19
20
 
@@ -51,26 +52,30 @@ class Rack::GridServe
51
52
  private
52
53
 
53
54
  def under_prefix? req
54
- req.path_info =~ %r|/#@prefix/(.*)|
55
+ req.path_info =~ %r|^/#@prefix/(.*)|
55
56
  end
56
57
 
57
58
  def id_or_filename req
58
- str = req.path_info.sub %r|/#@prefix/|, ''
59
+ str = req.path_info.sub %r|^/#@prefix/|, ''
59
60
  if BSON::ObjectId.legal? str
60
61
  BSON::ObjectId.from_string str
61
62
  else
62
- str
63
+ Rack::Utils.unescape str
63
64
  end
64
65
  end
65
66
 
66
67
  def find_file req
67
68
  str = id_or_filename req
68
- @db.fs.find({
69
- '$or' => [
70
- {_id: str},
71
- {filename: str}
72
- ]
73
- }).first
69
+ if str.is_a? BSON::ObjectId
70
+ @db.fs.find({_id: str}).first
71
+ else
72
+ @db.fs.find({
73
+ '$or' => [
74
+ {filename: str},
75
+ {filename: "/#{str}"}
76
+ ]
77
+ }).first
78
+ end
74
79
  end
75
80
 
76
81
  end
data/test.rb CHANGED
@@ -4,19 +4,21 @@ require 'rack/grid_serve'
4
4
  require 'rack/test'
5
5
  require 'minitest/autorun'
6
6
 
7
- class TestRackGridServe < MiniTest::Test
8
-
9
- MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'rack-grid-serve-test')
10
- DB = MONGO.database
11
- DB.drop
12
- FILE = Mongo::Grid::File.new('.cowabunga {}', :filename => 'tmnt.css', content_type: 'text/css')
13
- FILE_ID = DB.fs.insert_one(FILE)
14
-
15
- include Rack::Test::Methods
7
+ MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'rack-grid-serve-test')
8
+ DB = MONGO.database
9
+ DB.drop
10
+ DB.fs.open_upload_stream('tmnt.css', content_type: 'text/css') do |stream|
11
+ stream.write '.cowabunga {}'
12
+ end
13
+ DB.fs.open_upload_stream('amazing tmnt.css', content_type: 'text/css') do |stream|
14
+ stream.write '.cowabunga {}'
15
+ end
16
+ DB.fs.open_upload_stream('/slash-tmnt.css', content_type: 'text/css') do |stream|
17
+ stream.write '.cowabunga {}'
18
+ end
19
+ FILE = DB.fs.find({filename: 'tmnt.css'}).first
16
20
 
17
- def app
18
- Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB))
19
- end
21
+ module Helpers
20
22
 
21
23
  def inner_app
22
24
  lambda {|env|
@@ -26,15 +28,26 @@ class TestRackGridServe < MiniTest::Test
26
28
 
27
29
  def assert_file_found
28
30
  assert_equal 200, last_response.status
31
+ assert_equal FILE['contentType'], last_response.headers['Content-Type']
29
32
  assert_equal 13, last_response.body.size
30
- assert_equal FILE.info.content_type, last_response.headers['Content-Type']
31
- assert_equal FILE.info.md5, last_response.headers['ETag']
32
- assert_equal Time.at(FILE.info.upload_date.to_i).httpdate, last_response.headers['Last-Modified']
33
+ assert_equal FILE['md5'], last_response.headers['ETag']
34
+ assert_equal Time.at(FILE['uploadDate'].to_i).httpdate, last_response.headers['Last-Modified']
33
35
  assert_equal 'no-cache', last_response.headers['Cache-Control']
34
36
  end
35
37
 
38
+ end
39
+
40
+ class TestRackGridServe < MiniTest::Test
41
+
42
+ include Rack::Test::Methods
43
+ include Helpers
44
+
45
+ def app
46
+ Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB))
47
+ end
48
+
36
49
  def test_finds_file_by_id
37
- get "/gridfs/#{FILE_ID}"
50
+ get "/gridfs/#{FILE['_id']}"
38
51
  assert_file_found
39
52
  end
40
53
 
@@ -43,8 +56,18 @@ class TestRackGridServe < MiniTest::Test
43
56
  assert_file_found
44
57
  end
45
58
 
59
+ def test_finds_file_by_name_with_url_encoding
60
+ get '/gridfs/amazing%20tmnt.css'
61
+ assert_file_found
62
+ end
63
+
64
+ def test_finds_file_by_name_with_slash
65
+ get '/gridfs/slash-tmnt.css'
66
+ assert_file_found
67
+ end
68
+
46
69
  def test_uses_conditional_get
47
- get '/gridfs/tmnt.css', {}, {'HTTP_IF_NONE_MATCH'=>FILE.info.md5}
70
+ get '/gridfs/tmnt.css', {}, {'HTTP_IF_NONE_MATCH'=>FILE['md5']}
48
71
  assert_equal 304, last_response.status
49
72
  end
50
73
 
@@ -71,5 +94,28 @@ class TestRackGridServe < MiniTest::Test
71
94
  assert_equal "Inner", last_response.body
72
95
  end
73
96
 
97
+ def test_pass_if_prefix_not_at_the_begining
98
+ get '/before/gridfs/1234'
99
+ assert_equal 200, last_response.status
100
+ assert_equal "Inner", last_response.body
101
+ end
102
+
103
+ end
104
+
105
+ class TestRackGridServePrefix < MiniTest::Test
106
+
107
+ include Rack::Test::Methods
108
+ include Helpers
109
+
110
+ def app
111
+ Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB, prefix: '/attachment/prefix/'))
112
+ end
113
+
114
+ def test_finds_file_with_custom_prefix
115
+ prefix = "/attachment/prefix/"
116
+ get '/attachment/prefix/tmnt.css'
117
+ assert_file_found
118
+ end
119
+
74
120
  end
75
121
 
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-07-04 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.4.5.1
105
+ rubygems_version: 2.6.8
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: Rack middleware for serving files stored in MongoDB GridFS