rack-uploads 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/.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,60 @@
1
+ = rack-uploads
2
+
3
+ rack-uploads is a middleware which receives uploads and stores them in
4
+ in the Rack env for easy access.
5
+
6
+ It works with normal HTTP file uploads, as well as with the Nginx Upload
7
+ Module.
8
+
9
+ All multipart params get replaced by a Rack::Uploads::UploadedFile, while
10
+ still retaining access to the original parameter value.
11
+
12
+ == Dependencies
13
+
14
+ Development dependencies:
15
+
16
+ * rspec
17
+ * rack-test
18
+
19
+ == Usage
20
+
21
+ === Sinatra
22
+
23
+ use Rack::Uploads
24
+
25
+ post "/uploads" do
26
+ env['rack.uploads'].each do |upload|
27
+ upload.mv('/some/path/#{upload.filename}')
28
+ end
29
+ end
30
+
31
+ === Rails
32
+
33
+ # config/environment.rb
34
+ config.middleware.use "Rack::Uploads"
35
+
36
+ # app/controller/uploads_controller.rb
37
+ class UploadsController < ApplicationController
38
+ def create
39
+ request['rack.uploads'].each do |upload|
40
+ upload.mv("#{RAILS_ROOT}/public/uploads/#{upload.filename}")
41
+ end
42
+ end
43
+ end
44
+
45
+ == Options
46
+
47
+ There are a few options you can pass to rack-uploads during
48
+ initializiation:
49
+
50
+ <tt>:session_authorized => lambda { |req| req.params['secret'] == "sekrit"
51
+ }</tt> -
52
+ Only allow uploads with the parameter "secret" set to "sekrit"
53
+
54
+ <tt>:nginx => [{ :tmp_path => "_tmp_path", :filename => "_file_name" }]</tt> -
55
+ Sets the suffixes of the nginx upload parameters
56
+
57
+
58
+ == Copyright
59
+
60
+ 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.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "rack/uploads"
@@ -0,0 +1,56 @@
1
+ module Rack
2
+ class Uploads
3
+
4
+ def initialize(app, options={})
5
+ @app = app
6
+ @session_authorized = options[:session_authorized] || true
7
+ @nginx = options[:nginx] || [{ :tmp_path => "_tmp_path", :filename => "_file_name" }]
8
+ end
9
+
10
+ def call(env)
11
+ req = Rack::Request.new(env)
12
+ if req.post? && req.form_data?
13
+ return not_authorized unless @session_authorized == true || (@session_authorized.respond_to?(:call) && @session_authorized.call(req) == true)
14
+ uploads = []
15
+ req.params.each do |key,param|
16
+ if param_file = file(req, key)
17
+ uploads << param_file
18
+ req.params[key] = param_file
19
+ end
20
+ end
21
+ env['rack.uploads'] = uploads if uploads.size > 0
22
+ end
23
+ resp = @app.call(env)
24
+ if uploads && uploads.size > 0
25
+ uploads.each { |upload| upload.cleanup }
26
+ end
27
+ resp
28
+ end
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
+
47
+ def invalid_request
48
+ [400, {}, 'Invalid Request']
49
+ end
50
+
51
+ def not_authorized
52
+ [403, {}, 'Not Authorized']
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,58 @@
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
+ @cleanup_needed = false
19
+ end
20
+
21
+ def cp(destination)
22
+ FileUtils.cp(temp_path, destination)
23
+ end
24
+
25
+ def rm
26
+ FileUtils.rm(temp_path)
27
+ end
28
+
29
+ def size
30
+ ::File.size(temp_path)
31
+ end
32
+
33
+ def [](key)
34
+ @file[key]
35
+ end
36
+
37
+ def method_missing(meth, *args)
38
+ return @file[meth.to_sym] if @file[meth.to_sym]
39
+ super(meth, *args)
40
+ end
41
+
42
+ def cleanup
43
+ rm if @cleanup_needed
44
+ end
45
+ end
46
+
47
+ class UploadedNginxFile < UploadedFile
48
+ def initialize(key, file)
49
+ super(key, file)
50
+ @cleanup_needed = true
51
+ end
52
+ def temp_path
53
+ @file[:temp_path]
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1 @@
1
+ %w(middleware uploaded_file).each { |f| require File.join(File.dirname(__FILE__), "uploads", f) }
@@ -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
@@ -0,0 +1,3 @@
1
+ test test test
2
+ test test test
3
+ test test test
@@ -0,0 +1,111 @@
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 #{env['rack.uploads'].size} 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 a file upload" do
34
+ post '/uploads', {:file => multipart_fixture("test_data.txt"), "foo[bar]" => multipart_fixture("test_data.txt")}
35
+ last_response.status.should == 200
36
+ last_response.body.should == "Received 2 Files"
37
+ end
38
+
39
+ it "should receive a flash upload" do
40
+ post '/uploads', {'Filedata' => multipart_fixture("test_data.txt")}
41
+ last_response.status.should == 200
42
+ last_response.body.should == "Received 1 Files"
43
+ end
44
+
45
+ it "should receive a nginx upload" do
46
+ file = multipart_fixture("test_data.txt")
47
+ post '/uploads', nginx_upload_request("file", "test_data.txt")
48
+ last_response.status.should == 200
49
+ last_response.body.should == "Received 1 Files"
50
+ end
51
+
52
+ it "should cleanup received nginx uploads" do
53
+ file = multipart_fixture("test_data.txt")
54
+ post '/uploads', nginx_upload_request("file", "test_data.txt")
55
+ File.exist?("/tmp/rack_upload_nginx_tmp").should be_false
56
+ end
57
+
58
+ end
59
+
60
+ context "authorization" do
61
+ def app
62
+ 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') }})
63
+ end
64
+
65
+ it "should not respond to non-post requests" do
66
+ get '/uploads'
67
+ last_response.status.should == 200
68
+ last_response.body.should == "Hello, World!"
69
+ put '/uploads'
70
+ last_response.status.should == 200
71
+ last_response.body.should == "Hello, World!"
72
+ delete '/uploads'
73
+ last_response.status.should == 200
74
+ last_response.body.should == "Hello, World!"
75
+ end
76
+
77
+ it "should not allow unauthorized uploads" do
78
+ post '/uploads'
79
+ last_response.status.should == 403
80
+ end
81
+
82
+ it "should authorize logged in users to upload" do
83
+ response = post '/uploads', {}, user_session
84
+ last_response.status.should_not == 400
85
+ last_response.status.should_not == 403
86
+ end
87
+
88
+ it "should authorize flash uploaders to upload" do
89
+ response = post '/uploads?flash_token=123'
90
+ last_response.status.should_not == 400
91
+ last_response.status.should_not == 403
92
+ end
93
+
94
+ end
95
+
96
+ private
97
+ def nginx_upload_request(key, name, temp_path="/tmp/rack_upload_nginx_tmp")
98
+ FileUtils.cp multipart_file(name), temp_path
99
+ {"#{key}_tmp_path" => temp_path, "#{key}_file_name" => name, "#{key}_content_type" => "text/plain", "#{key}_size" => File.size(multipart_file(name))}
100
+ end
101
+
102
+ def multipart_fixture(name)
103
+ Rack::Test::UploadedFile.new(multipart_file(name))
104
+ end
105
+
106
+ def multipart_file(name)
107
+ File.join(File.dirname(__FILE__), "fixtures/files", name)
108
+ end
109
+
110
+ end
111
+
@@ -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,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-uploads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mutwin Kraus
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-13 00:00:00 +02: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
+ - rack-uploads.gemspec
37
+ - spec/fixtures/files/test_data.txt
38
+ - spec/middleware_spec.rb
39
+ - spec/spec_helper.rb
40
+ - spec/uploaded_file_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/mutle/rack-uploads
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.3
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Rack Upload handler with Nginx Upload Module support
69
+ test_files:
70
+ - spec/middleware_spec.rb
71
+ - spec/spec_helper.rb
72
+ - spec/uploaded_file_spec.rb