mutle-rack-uploads 0.0.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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mutwin Kraus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ = rack-uploads
2
+
3
+ rack-uploads is a middleware which receives uploads and stores them in
4
+ env['rack.uploads'] for easy access.
5
+
6
+ It works with normal HTTP file uploads, as well as with the Nginx Upload
7
+ Module.
8
+
9
+ == Dependencies
10
+
11
+ Development dependencies:
12
+
13
+ * rspec
14
+ * rack-test
15
+
16
+ == Usage
17
+
18
+ # Sinatra
19
+ use Rack::Uploads, :path => "/uploads"
20
+
21
+ post "/uploads" do
22
+ env['rack.uploads'].each do |upload|
23
+ upload.mv('/some/path/#{upload.filename}')
24
+ end
25
+ end
26
+
27
+
28
+ #Rails
29
+
30
+ # config/environment.rb
31
+ config.middleware.use "Rack::Uploads", :path => "/uploads"
32
+
33
+ # app/controller/uploads_controller.rb
34
+ class UploadsController < ApplicationController
35
+ def create
36
+ request['rack.uploads'].each do |upload|
37
+ upload.mv("#{RAILS_ROOT}/public/uploads/#{upload.filename}")
38
+ end
39
+ end
40
+ end
41
+
42
+ == Options
43
+
44
+ There are a few options you can pass to rack-uploads during
45
+ initializiation:
46
+
47
+ :path => "/files" - This will check for file uploads only on POST requests
48
+ to /files.
49
+
50
+ :file_params => ["file", "Filedata"] - Sets which parameters to check for uploads. Default is
51
+ "file" and "Filedata" (used by Flash uploaders)
52
+
53
+ :session_authorized => lambda { |req| req.params['secret'] == "sekrit" } -
54
+ Only allow uploads with the parameter "secret" set to "sekrit"
55
+
56
+ == Copyright
57
+
58
+ Copyright (c) 2009 Mutwin Kraus. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-uploads"
8
+ gem.summary = %Q{Rack Upload handler with Nginx Upload Module support}
9
+ gem.email = "mutle@blogage.de"
10
+ gem.homepage = "http://github.com/mutle/rack-uploads"
11
+ gem.authors = ["Mutwin Kraus"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "rack-uploads #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "rack/uploads"
@@ -0,0 +1,41 @@
1
+ module Rack
2
+ class Uploads
3
+
4
+ def initialize(app, options={})
5
+ @app = app
6
+ @path = options[:path] || '/uploads'
7
+ @file_params = options[:file_params] || ["file", "Filedata"]
8
+ @session_authorized = options[:session_authorized] || true
9
+ end
10
+
11
+ def call(env)
12
+ req = Rack::Request.new(env)
13
+ if req.path_info == @path && req.post?
14
+ return not_authorized unless @session_authorized == true || (@session_authorized.respond_to?(:call) && @session_authorized.call(req) == true)
15
+ uploads = []
16
+ @file_params.each do |file_key|
17
+ if file = req.params[file_key]
18
+ uploads << UploadedFile.new(file_key, file)
19
+ elsif req.params["#{file_key}_name"] && req.params["#{file_key}_path"]
20
+ uploads << UploadedNginxFile.new(file_key, {:filename => req.params["#{file_key}_name"], :temp_path => req.params["#{file_key}_path"] })
21
+ end
22
+ end
23
+ env['rack.uploads'] = uploads if uploads.size > 0
24
+ end
25
+ resp = @app.call(env)
26
+ if uploads && uploads.size > 0
27
+ uploads.each { |upload| upload.cleanup }
28
+ end
29
+ resp
30
+ end
31
+
32
+ def invalid_request
33
+ [400, {}, 'Invalid Request']
34
+ end
35
+
36
+ def not_authorized
37
+ [403, {}, 'Not Authorized']
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,49 @@
1
+ module Rack
2
+ class Uploads
3
+
4
+ class UploadedFile
5
+ attr_reader :key, :file
6
+ def initialize(key, file)
7
+ @key = key
8
+ @file = file
9
+ @cleanup_needed = false
10
+ end
11
+
12
+ def temp_path
13
+ @file[:tempfile].path
14
+ end
15
+
16
+ def mv(destination)
17
+ FileUtils.mv(temp_path, destination)
18
+ end
19
+
20
+ def rm
21
+ FileUtils.rm(temp_path)
22
+ end
23
+
24
+ def size
25
+ ::File.size(temp_path)
26
+ end
27
+
28
+ def method_missing(meth, *args)
29
+ return @file[meth.to_sym] if @file[meth.to_sym]
30
+ super(meth, *args)
31
+ end
32
+
33
+ def cleanup
34
+ rm if @cleanup_needed
35
+ end
36
+ end
37
+
38
+ class UploadedNginxFile < UploadedFile
39
+ def initialize(key, file)
40
+ super(key, file)
41
+ @cleanup_needed = true
42
+ end
43
+ def temp_path
44
+ @file[:temp_path]
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1 @@
1
+ %w(middleware uploaded_file).each { |f| require File.join(File.dirname(__FILE__), "uploads", f) }
@@ -0,0 +1,3 @@
1
+ test test test
2
+ test test test
3
+ test test test
@@ -0,0 +1,112 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ require 'rack/test'
4
+ require 'rack/utils'
5
+ require 'rack/mock'
6
+
7
+ describe Rack::Uploads do
8
+
9
+ include Rack::Test::Methods
10
+
11
+ def hello_world
12
+ lambda { |env|
13
+ req = Rack::Request.new(env)
14
+ if req.path_info == "/uploads" && req.post? && env['rack.uploads']
15
+ [200, {}, "Received Files"]
16
+ else
17
+ [200, {}, "Hello, World!"]
18
+ end
19
+ }
20
+ end
21
+
22
+
23
+ def user_session
24
+ {"rack.session" => {:user_id => 100}}
25
+ end
26
+
27
+ context "file uploads" do
28
+
29
+ def app
30
+ @backend ||= Rack::Uploads.new(hello_world)
31
+ end
32
+
33
+ it "should receive and store a file upload" do
34
+ post '/uploads', {:file => multipart_fixture("test_data.txt")}
35
+ last_response.status.should == 200
36
+ last_response.body.should == "Received Files"
37
+ File.exist?("/tmp/blogage_upload").should be_true
38
+ end
39
+
40
+ it "should receive and store a flash upload" do
41
+ post '/uploads', {'Filedata' => multipart_fixture("test_data.txt")}
42
+ last_response.status.should == 200
43
+ last_response.body.should == "Received Files"
44
+ end
45
+
46
+ it "should receive and move a nginx upload" do
47
+ file = multipart_fixture("test_data.txt")
48
+ post '/uploads', nginx_upload_request("file", "test_data.txt")
49
+ last_response.status.should == 200
50
+ last_response.body.should == "Received Files"
51
+ end
52
+
53
+ it "should cleanup received nginx uploads" do
54
+ file = multipart_fixture("test_data.txt")
55
+ post '/uploads', nginx_upload_request("file", "test_data.txt")
56
+ File.exist?("/tmp/rack_upload_nginx_tmp").should be_false
57
+ end
58
+
59
+ end
60
+
61
+ context "authorization" do
62
+ def app
63
+ Rack::Uploads.new(hello_world, {:session_authorized => lambda { |req| (req.env['rack.session'] && req.env['rack.session'][:user_id] && req.env['rack.session'][:user_id].to_i > 0) || (req.params['flash_token'] && req.params['flash_token'] == '123') }})
64
+ end
65
+
66
+ it "should not respond to non-post requests" do
67
+ get '/uploads'
68
+ last_response.status.should == 200
69
+ last_response.body.should == "Hello, World!"
70
+ put '/uploads'
71
+ last_response.status.should == 200
72
+ last_response.body.should == "Hello, World!"
73
+ delete '/uploads'
74
+ last_response.status.should == 200
75
+ last_response.body.should == "Hello, World!"
76
+ end
77
+
78
+ it "should not allow unauthorized uploads" do
79
+ post '/uploads'
80
+ last_response.status.should == 403
81
+ end
82
+
83
+ it "should authorize logged in users to upload" do
84
+ response = post '/uploads', {}, user_session
85
+ last_response.status.should_not == 400
86
+ last_response.status.should_not == 403
87
+ end
88
+
89
+ it "should authorize flash uploaders to upload" do
90
+ response = post '/uploads?flash_token=123'
91
+ last_response.status.should_not == 400
92
+ last_response.status.should_not == 403
93
+ end
94
+
95
+ end
96
+
97
+ private
98
+ def nginx_upload_request(key, name, temp_path="/tmp/rack_upload_nginx_tmp")
99
+ FileUtils.cp multipart_file(name), temp_path
100
+ {"file_path" => temp_path, "#{key}_name" => name, "#{key}_content_type" => "text/plain", "#{key}_size" => File.size(multipart_file(name))}
101
+ end
102
+
103
+ def multipart_fixture(name)
104
+ Rack::Test::UploadedFile.new(multipart_file(name))
105
+ end
106
+
107
+ def multipart_file(name)
108
+ File.join(File.dirname(__FILE__), "fixtures/files", name)
109
+ end
110
+
111
+ end
112
+
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rack'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'rack/uploads'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ require 'rack/test'
4
+ require 'rack/utils'
5
+ require 'rack/mock'
6
+
7
+ describe Rack::Uploads::UploadedFile do
8
+
9
+ it "should pass through file attributes" do
10
+ file = Rack::Uploads::UploadedFile.new("foo", {:foo => "bar"})
11
+ file.foo.should == "bar"
12
+ lambda { file.bar }.should raise_error(NoMethodError)
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mutle-rack-uploads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mutwin Kraus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mutle@blogage.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - init.rb
33
+ - lib/rack/uploads.rb
34
+ - lib/rack/uploads/middleware.rb
35
+ - lib/rack/uploads/uploaded_file.rb
36
+ - spec/fixtures/files/test_data.txt
37
+ - spec/middleware_spec.rb
38
+ - spec/spec_helper.rb
39
+ - spec/uploaded_file_spec.rb
40
+ has_rdoc: false
41
+ homepage: http://github.com/mutle/rack-uploads
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Rack Upload handler with Nginx Upload Module support
66
+ test_files:
67
+ - spec/middleware_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/uploaded_file_spec.rb