rack-raw-upload 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -3
- data/lib/rack/raw_upload.rb +4 -3
- data/test/apps/example/lib/simple_app.rb +28 -1
- data/test/raw_upload_test.rb +8 -3
- metadata +104 -106
- data/test/apps/example/lib/tfl_countdown.rb +0 -44
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
|
5
|
-
json (1.4.6-java)
|
4
|
+
multi_json (1.3.2)
|
6
5
|
rack (1.2.1)
|
7
6
|
rack-test (0.5.6)
|
8
7
|
rack (>= 1.0)
|
@@ -15,7 +14,7 @@ PLATFORMS
|
|
15
14
|
ruby
|
16
15
|
|
17
16
|
DEPENDENCIES
|
18
|
-
|
17
|
+
multi_json
|
19
18
|
rack-test
|
20
19
|
rake
|
21
20
|
rr
|
data/lib/rack/raw_upload.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'tmpdir' # Needed in 1.8.7 to access Dir::tmpdir
|
2
|
+
require 'multi_json'
|
2
3
|
|
3
4
|
module Rack
|
4
5
|
class RawUpload
|
5
6
|
|
6
|
-
VERSION = '1.1.
|
7
|
+
VERSION = '1.1.1'
|
7
8
|
|
8
9
|
def initialize(app, opts = {})
|
9
10
|
@app = app
|
@@ -43,6 +44,7 @@ module Rack
|
|
43
44
|
tempfile << chunk
|
44
45
|
end
|
45
46
|
end
|
47
|
+
env['rack.input'].rewind
|
46
48
|
|
47
49
|
tempfile.flush
|
48
50
|
tempfile.rewind
|
@@ -107,8 +109,7 @@ module Rack
|
|
107
109
|
end
|
108
110
|
|
109
111
|
def inject_json_params!(env, params)
|
110
|
-
|
111
|
-
hsh = JSON.parse(params)
|
112
|
+
hsh = MultiJson.load(params)
|
112
113
|
env['rack.request.form_hash'].merge!(hsh)
|
113
114
|
env['rack.request.query_hash'].merge!(hsh)
|
114
115
|
end
|
@@ -1,17 +1,44 @@
|
|
1
1
|
require 'pp'
|
2
2
|
require 'sinatra/base'
|
3
|
+
require 'multi_json'
|
3
4
|
|
4
5
|
class SimpleApp < Sinatra::Base
|
5
6
|
|
6
7
|
set :root, APP_ROOT
|
7
8
|
set :static, true
|
9
|
+
set :public_folder, Proc.new{ File.join(root, 'public') }
|
10
|
+
set :upload_dir, Proc.new{ File.join(public_folder, 'uploads') }
|
11
|
+
|
12
|
+
configure do
|
13
|
+
FileUtils.mkdir_p(settings.upload_dir)
|
14
|
+
end
|
8
15
|
|
9
16
|
get '/' do
|
10
17
|
erb :index
|
11
18
|
end
|
12
19
|
|
13
20
|
post '/' do
|
14
|
-
|
21
|
+
content_type :json
|
22
|
+
dump = Rack::Utils.escape_html(PP.pp(params, ''))
|
23
|
+
download_url = file_url(store_file(params[:file]))
|
24
|
+
MultiJson.dump({
|
25
|
+
:dump => dump,
|
26
|
+
:download_url => download_url,
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def store_file(file_param)
|
34
|
+
dirpath = Dir.mktmpdir(nil, settings.upload_dir)
|
35
|
+
filepath = File.join(dirpath, file_param[:filename])
|
36
|
+
FileUtils.mv(file_param[:tempfile], filepath)
|
37
|
+
filepath
|
38
|
+
end
|
39
|
+
|
40
|
+
def file_url(path)
|
41
|
+
path.gsub(Regexp.new('^' + settings.public_folder), '').tap{|x| pp x}
|
15
42
|
end
|
16
43
|
|
17
44
|
end
|
data/test/raw_upload_test.rb
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rack/test'
|
3
3
|
require 'shoulda'
|
4
4
|
require 'rack-raw-upload'
|
5
|
-
require '
|
5
|
+
require 'multi_json'
|
6
6
|
require 'digest'
|
7
7
|
require 'rr'
|
8
8
|
|
@@ -120,6 +120,11 @@ class RawUploadTest < Test::Unit::TestCase
|
|
120
120
|
assert_successful_non_upload
|
121
121
|
end
|
122
122
|
|
123
|
+
should "leave rack.input in a state readable by other middlewares" do
|
124
|
+
upload
|
125
|
+
assert !last_request.env['rack.input'].eof?, "rack.input should be rewind'd"
|
126
|
+
end
|
127
|
+
|
123
128
|
context "when garbage collection runs (Ruby 1.9)" do
|
124
129
|
context "and the file is received as a Tempfile" do
|
125
130
|
should "ensure that the uploaded file remains" do
|
@@ -198,7 +203,7 @@ class RawUploadTest < Test::Unit::TestCase
|
|
198
203
|
|
199
204
|
context "with JSON parameters" do
|
200
205
|
setup do
|
201
|
-
upload('HTTP_X_JSON_PARAMS' =>
|
206
|
+
upload('HTTP_X_JSON_PARAMS' => MultiJson.dump({
|
202
207
|
:argument => 'value1',
|
203
208
|
'argument with spaces' => 'value 2'
|
204
209
|
}))
|
@@ -223,7 +228,7 @@ class RawUploadTest < Test::Unit::TestCase
|
|
223
228
|
|
224
229
|
context "with query parameters, deprecated style" do
|
225
230
|
setup do
|
226
|
-
json_params =
|
231
|
+
json_params = MultiJson.dump({
|
227
232
|
:argument => 'value1',
|
228
233
|
'argument with spaces' => 'value 2'
|
229
234
|
})
|
metadata
CHANGED
@@ -1,70 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-raw-upload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
version: 1.1.0
|
4
|
+
prerelease:
|
5
|
+
version: 1.1.1
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
|
-
- Pablo Brasero
|
8
|
+
- Pablo Brasero
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
18
|
-
default_executable:
|
13
|
+
date: 2013-01-06 00:00:00 Z
|
19
14
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: multi_json
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rack-test
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: shoulda
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rr
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id005
|
68
70
|
description: Middleware that converts files uploaded with mimetype application/octet-stream into normal form input, so Rack applications can read these as normal, rather than as raw input.
|
69
71
|
email: pablobm@gmail.com
|
70
72
|
executables: []
|
@@ -72,70 +74,66 @@ executables: []
|
|
72
74
|
extensions: []
|
73
75
|
|
74
76
|
extra_rdoc_files:
|
75
|
-
- LICENSE
|
76
|
-
- README.md
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
77
79
|
files:
|
78
|
-
- lib/rack
|
79
|
-
- lib/rack
|
80
|
-
- test/
|
81
|
-
- test/apps/example/
|
82
|
-
- test/apps/example/lib/
|
83
|
-
- test/apps/undefined_conversion_error/app/controllers/application_controller.rb
|
84
|
-
- test/apps/undefined_conversion_error/app/controllers/test_controller.rb
|
85
|
-
- test/apps/undefined_conversion_error/app/helpers/application_helper.rb
|
86
|
-
- test/apps/undefined_conversion_error/config/application.rb
|
87
|
-
- test/apps/undefined_conversion_error/config/boot.rb
|
88
|
-
- test/apps/undefined_conversion_error/config/environment.rb
|
89
|
-
- test/apps/undefined_conversion_error/config/
|
90
|
-
- test/apps/undefined_conversion_error/config/environments/
|
91
|
-
- test/apps/undefined_conversion_error/config/environments/
|
92
|
-
- test/apps/undefined_conversion_error/config/
|
93
|
-
- test/apps/undefined_conversion_error/config/initializers/
|
94
|
-
- test/apps/undefined_conversion_error/config/initializers/
|
95
|
-
- test/apps/undefined_conversion_error/config/initializers/
|
96
|
-
- test/apps/undefined_conversion_error/config/initializers/
|
97
|
-
- test/apps/undefined_conversion_error/config/initializers/
|
98
|
-
- test/apps/undefined_conversion_error/config/
|
99
|
-
- test/apps/undefined_conversion_error/db/seeds.rb
|
100
|
-
- test/apps/undefined_conversion_error/test/
|
101
|
-
- test/apps/undefined_conversion_error/test/
|
102
|
-
-
|
103
|
-
-
|
104
|
-
-
|
105
|
-
- Gemfile
|
106
|
-
- Gemfile.lock
|
107
|
-
has_rdoc: true
|
80
|
+
- lib/rack-raw-upload.rb
|
81
|
+
- lib/rack/raw_upload.rb
|
82
|
+
- test/raw_upload_test.rb
|
83
|
+
- test/apps/example/app.rb
|
84
|
+
- test/apps/example/lib/simple_app.rb
|
85
|
+
- test/apps/undefined_conversion_error/app/controllers/application_controller.rb
|
86
|
+
- test/apps/undefined_conversion_error/app/controllers/test_controller.rb
|
87
|
+
- test/apps/undefined_conversion_error/app/helpers/application_helper.rb
|
88
|
+
- test/apps/undefined_conversion_error/config/application.rb
|
89
|
+
- test/apps/undefined_conversion_error/config/boot.rb
|
90
|
+
- test/apps/undefined_conversion_error/config/environment.rb
|
91
|
+
- test/apps/undefined_conversion_error/config/routes.rb
|
92
|
+
- test/apps/undefined_conversion_error/config/environments/development.rb
|
93
|
+
- test/apps/undefined_conversion_error/config/environments/production.rb
|
94
|
+
- test/apps/undefined_conversion_error/config/environments/test.rb
|
95
|
+
- test/apps/undefined_conversion_error/config/initializers/backtrace_silencers.rb
|
96
|
+
- test/apps/undefined_conversion_error/config/initializers/inflections.rb
|
97
|
+
- test/apps/undefined_conversion_error/config/initializers/mime_types.rb
|
98
|
+
- test/apps/undefined_conversion_error/config/initializers/secret_token.rb
|
99
|
+
- test/apps/undefined_conversion_error/config/initializers/session_store.rb
|
100
|
+
- test/apps/undefined_conversion_error/config/initializers/wrap_parameters.rb
|
101
|
+
- test/apps/undefined_conversion_error/db/seeds.rb
|
102
|
+
- test/apps/undefined_conversion_error/test/test_helper.rb
|
103
|
+
- test/apps/undefined_conversion_error/test/performance/browsing_test.rb
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
108
|
homepage: https://github.com/newbamboo/rack-raw-upload
|
109
109
|
licenses: []
|
110
110
|
|
111
111
|
post_install_message:
|
112
112
|
rdoc_options:
|
113
|
-
- --charset=UTF-8
|
114
|
-
- --main
|
115
|
-
- README.rdoc
|
113
|
+
- --charset=UTF-8
|
114
|
+
- --main
|
115
|
+
- README.rdoc
|
116
116
|
require_paths:
|
117
|
-
- lib
|
117
|
+
- lib
|
118
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
119
120
|
requirements:
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
- 0
|
124
|
-
version: "0"
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: "0"
|
125
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
126
|
requirements:
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
- 0
|
131
|
-
version: "0"
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: "0"
|
132
130
|
requirements: []
|
133
131
|
|
134
132
|
rubyforge_project:
|
135
|
-
rubygems_version: 1.
|
133
|
+
rubygems_version: 1.8.15
|
136
134
|
signing_key:
|
137
135
|
specification_version: 3
|
138
136
|
summary: Rack Raw Upload middleware
|
139
137
|
test_files:
|
140
|
-
- test/apps/undefined_conversion_error/app/controllers/test_controller.rb
|
141
|
-
- test/apps/undefined_conversion_error/test/test_helper.rb
|
138
|
+
- test/apps/undefined_conversion_error/app/controllers/test_controller.rb
|
139
|
+
- test/apps/undefined_conversion_error/test/test_helper.rb
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'httparty'
|
2
|
-
|
3
|
-
class TflCountdown
|
4
|
-
include HTTParty
|
5
|
-
base_uri 'http://countdown.tfl.gov.uk'
|
6
|
-
|
7
|
-
def initialize(status = {})
|
8
|
-
@cookies = status['cookies'] if status['cookies']
|
9
|
-
end
|
10
|
-
|
11
|
-
def search(q)
|
12
|
-
get_json('/search', 'searchTerm' => q)
|
13
|
-
end
|
14
|
-
|
15
|
-
def markers(swlat, swlng, nelat, nelng)
|
16
|
-
path = "/markers/swLat/#{swlat}/swLng/#{swlng}/neLat/#{nelat}/neLng/#{nelng}/"
|
17
|
-
get_json(path)
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
def get_json(path, query = {})
|
22
|
-
query_params = {
|
23
|
-
'_dc' => Time.now.to_i,
|
24
|
-
}.merge(query)
|
25
|
-
|
26
|
-
headers = {
|
27
|
-
'X-Requested-With' => 'XMLHttpRequest',
|
28
|
-
'Referer' => 'http://countdown.tfl.gov.uk/',
|
29
|
-
'User-Agent' => 'Pablito\'s own',
|
30
|
-
}
|
31
|
-
headers['Cookie'] = @cookies if @cookies
|
32
|
-
|
33
|
-
res = self.class.get(path, :query => query_params, :headers => headers, :format => :json)
|
34
|
-
@cookies = res.headers['set-cookie']
|
35
|
-
res.body
|
36
|
-
end
|
37
|
-
|
38
|
-
def to_hash
|
39
|
-
ret = {}
|
40
|
-
ret['cookies'] = @cookies if @cookies
|
41
|
-
ret
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|