mutle-rack-uploads 0.0.0 → 0.1.0
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.
- data/README.rdoc +14 -12
- data/VERSION +1 -1
- data/lib/rack/uploads/middleware.rb +23 -8
- data/lib/rack/uploads/uploaded_file.rb +9 -0
- data/rack-uploads.gemspec +52 -0
- data/spec/middleware_spec.rb +9 -10
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
= rack-uploads
|
2
2
|
|
3
3
|
rack-uploads is a middleware which receives uploads and stores them in
|
4
|
-
env
|
4
|
+
in the Rack env for easy access.
|
5
5
|
|
6
6
|
It works with normal HTTP file uploads, as well as with the Nginx Upload
|
7
7
|
Module.
|
8
8
|
|
9
|
+
All multipart params get replaced by a Rack::Uploads::UploadedFile, while
|
10
|
+
still retaining access to the original parameter value.
|
11
|
+
|
9
12
|
== Dependencies
|
10
13
|
|
11
14
|
Development dependencies:
|
@@ -15,8 +18,9 @@ Development dependencies:
|
|
15
18
|
|
16
19
|
== Usage
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
=== Sinatra
|
22
|
+
|
23
|
+
use Rack::Uploads
|
20
24
|
|
21
25
|
post "/uploads" do
|
22
26
|
env['rack.uploads'].each do |upload|
|
@@ -24,11 +28,10 @@ Development dependencies:
|
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
27
|
-
|
28
|
-
#Rails
|
31
|
+
=== Rails
|
29
32
|
|
30
33
|
# config/environment.rb
|
31
|
-
config.middleware.use "Rack::Uploads"
|
34
|
+
config.middleware.use "Rack::Uploads"
|
32
35
|
|
33
36
|
# app/controller/uploads_controller.rb
|
34
37
|
class UploadsController < ApplicationController
|
@@ -44,14 +47,13 @@ Development dependencies:
|
|
44
47
|
There are a few options you can pass to rack-uploads during
|
45
48
|
initializiation:
|
46
49
|
|
47
|
-
|
48
|
-
|
50
|
+
<tt>:session_authorized => lambda { |req| req.params['secret'] == "sekrit"
|
51
|
+
}</tt> -
|
52
|
+
Only allow uploads with the parameter "secret" set to "sekrit"
|
49
53
|
|
50
|
-
:
|
51
|
-
|
54
|
+
<tt>:nginx => [{ :tmp_path => "_tmp_path", :filename => "_file_name" }]</tt> -
|
55
|
+
Sets the suffixes of the nginx upload parameters
|
52
56
|
|
53
|
-
:session_authorized => lambda { |req| req.params['secret'] == "sekrit" } -
|
54
|
-
Only allow uploads with the parameter "secret" set to "sekrit"
|
55
57
|
|
56
58
|
== Copyright
|
57
59
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.1.0
|
@@ -3,21 +3,19 @@ module Rack
|
|
3
3
|
|
4
4
|
def initialize(app, options={})
|
5
5
|
@app = app
|
6
|
-
@path = options[:path] || '/uploads'
|
7
|
-
@file_params = options[:file_params] || ["file", "Filedata"]
|
8
6
|
@session_authorized = options[:session_authorized] || true
|
7
|
+
@nginx = options[:nginx] || [{ :tmp_path => "_tmp_path", :filename => "_file_name" }]
|
9
8
|
end
|
10
9
|
|
11
10
|
def call(env)
|
12
11
|
req = Rack::Request.new(env)
|
13
|
-
if req.
|
12
|
+
if req.post? && req.form_data?
|
14
13
|
return not_authorized unless @session_authorized == true || (@session_authorized.respond_to?(:call) && @session_authorized.call(req) == true)
|
15
14
|
uploads = []
|
16
|
-
|
17
|
-
if
|
18
|
-
uploads <<
|
19
|
-
|
20
|
-
uploads << UploadedNginxFile.new(file_key, {:filename => req.params["#{file_key}_name"], :temp_path => req.params["#{file_key}_path"] })
|
15
|
+
req.params.each do |key,param|
|
16
|
+
if param_file = file(req, key)
|
17
|
+
uploads << param_file
|
18
|
+
req.params[key] = param_file
|
21
19
|
end
|
22
20
|
end
|
23
21
|
env['rack.uploads'] = uploads if uploads.size > 0
|
@@ -29,6 +27,23 @@ module Rack
|
|
29
27
|
resp
|
30
28
|
end
|
31
29
|
|
30
|
+
def multipart?(req)
|
31
|
+
req.media_type == "multipart/form-data"
|
32
|
+
end
|
33
|
+
|
34
|
+
def file(req, key)
|
35
|
+
param = req.params[key]
|
36
|
+
return UploadedFile.new(key, param) if param.instance_of?(Hash) && param[:tempfile]
|
37
|
+
@nginx.each do |nginx|
|
38
|
+
if key =~ %r{^(.+)#{nginx[:tmp_path]}$}
|
39
|
+
tmp_path = param
|
40
|
+
filename = req.params["#{$1}#{nginx[:filename]}"]
|
41
|
+
return UploadedNginxFile.new($1, {:filename => filename, :temp_path => tmp_path}) if ::File.exist?(tmp_path) && filename
|
42
|
+
end
|
43
|
+
end
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
|
32
47
|
def invalid_request
|
33
48
|
[400, {}, 'Invalid Request']
|
34
49
|
end
|
@@ -15,6 +15,11 @@ module Rack
|
|
15
15
|
|
16
16
|
def mv(destination)
|
17
17
|
FileUtils.mv(temp_path, destination)
|
18
|
+
@cleanup_needed = false
|
19
|
+
end
|
20
|
+
|
21
|
+
def cp(destination)
|
22
|
+
FileUtils.cp(temp_path, destination)
|
18
23
|
end
|
19
24
|
|
20
25
|
def rm
|
@@ -25,6 +30,10 @@ module Rack
|
|
25
30
|
::File.size(temp_path)
|
26
31
|
end
|
27
32
|
|
33
|
+
def [](key)
|
34
|
+
@file[key]
|
35
|
+
end
|
36
|
+
|
28
37
|
def method_missing(meth, *args)
|
29
38
|
return @file[meth.to_sym] if @file[meth.to_sym]
|
30
39
|
super(meth, *args)
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{rack-uploads}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Mutwin Kraus"]
|
9
|
+
s.date = %q{2009-06-13}
|
10
|
+
s.email = %q{mutle@blogage.de}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"init.rb",
|
23
|
+
"lib/rack/uploads.rb",
|
24
|
+
"lib/rack/uploads/middleware.rb",
|
25
|
+
"lib/rack/uploads/uploaded_file.rb",
|
26
|
+
"rack-uploads.gemspec",
|
27
|
+
"spec/fixtures/files/test_data.txt",
|
28
|
+
"spec/middleware_spec.rb",
|
29
|
+
"spec/spec_helper.rb",
|
30
|
+
"spec/uploaded_file_spec.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/mutle/rack-uploads}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.3}
|
36
|
+
s.summary = %q{Rack Upload handler with Nginx Upload Module support}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/middleware_spec.rb",
|
39
|
+
"spec/spec_helper.rb",
|
40
|
+
"spec/uploaded_file_spec.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
48
|
+
else
|
49
|
+
end
|
50
|
+
else
|
51
|
+
end
|
52
|
+
end
|
data/spec/middleware_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe Rack::Uploads do
|
|
12
12
|
lambda { |env|
|
13
13
|
req = Rack::Request.new(env)
|
14
14
|
if req.path_info == "/uploads" && req.post? && env['rack.uploads']
|
15
|
-
[200, {}, "Received Files"]
|
15
|
+
[200, {}, "Received #{env['rack.uploads'].size} Files"]
|
16
16
|
else
|
17
17
|
[200, {}, "Hello, World!"]
|
18
18
|
end
|
@@ -30,24 +30,23 @@ describe Rack::Uploads do
|
|
30
30
|
@backend ||= Rack::Uploads.new(hello_world)
|
31
31
|
end
|
32
32
|
|
33
|
-
it "should receive
|
34
|
-
post '/uploads', {:file => multipart_fixture("test_data.txt")}
|
33
|
+
it "should receive a file upload" do
|
34
|
+
post '/uploads', {:file => multipart_fixture("test_data.txt"), "foo[bar]" => multipart_fixture("test_data.txt")}
|
35
35
|
last_response.status.should == 200
|
36
|
-
last_response.body.should == "Received Files"
|
37
|
-
File.exist?("/tmp/blogage_upload").should be_true
|
36
|
+
last_response.body.should == "Received 2 Files"
|
38
37
|
end
|
39
38
|
|
40
|
-
it "should receive
|
39
|
+
it "should receive a flash upload" do
|
41
40
|
post '/uploads', {'Filedata' => multipart_fixture("test_data.txt")}
|
42
41
|
last_response.status.should == 200
|
43
|
-
last_response.body.should == "Received Files"
|
42
|
+
last_response.body.should == "Received 1 Files"
|
44
43
|
end
|
45
44
|
|
46
|
-
it "should receive
|
45
|
+
it "should receive a nginx upload" do
|
47
46
|
file = multipart_fixture("test_data.txt")
|
48
47
|
post '/uploads', nginx_upload_request("file", "test_data.txt")
|
49
48
|
last_response.status.should == 200
|
50
|
-
last_response.body.should == "Received Files"
|
49
|
+
last_response.body.should == "Received 1 Files"
|
51
50
|
end
|
52
51
|
|
53
52
|
it "should cleanup received nginx uploads" do
|
@@ -97,7 +96,7 @@ describe Rack::Uploads do
|
|
97
96
|
private
|
98
97
|
def nginx_upload_request(key, name, temp_path="/tmp/rack_upload_nginx_tmp")
|
99
98
|
FileUtils.cp multipart_file(name), temp_path
|
100
|
-
{"
|
99
|
+
{"#{key}_tmp_path" => temp_path, "#{key}_file_name" => name, "#{key}_content_type" => "text/plain", "#{key}_size" => File.size(multipart_file(name))}
|
101
100
|
end
|
102
101
|
|
103
102
|
def multipart_fixture(name)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mutle-rack-uploads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mutwin Kraus
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-13 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/rack/uploads.rb
|
34
34
|
- lib/rack/uploads/middleware.rb
|
35
35
|
- lib/rack/uploads/uploaded_file.rb
|
36
|
+
- rack-uploads.gemspec
|
36
37
|
- spec/fixtures/files/test_data.txt
|
37
38
|
- spec/middleware_spec.rb
|
38
39
|
- spec/spec_helper.rb
|