rack-fix-content-type 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails", "3.0.6"
4
+ gem "capybara", ">= 0.4.0"
5
+ gem "sqlite3"
6
+
7
+ # To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
8
+ # gem 'ruby-debug'
9
+ # gem 'ruby-debug19'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Gregor Melhorn and Michael Raidel with permission of autohaus24 GmbH
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,20 @@
1
+ = RackFixContentType
2
+
3
+ Fixes the content type of form uploads by using the file-utility. A lot of browsers screw up the content type so we don't trust the given value in the header.
4
+
5
+ ==Usage
6
+
7
+ For rails, in environment.rb, just add
8
+
9
+ config.middleware.use "RackFixContentType::Middleware"
10
+
11
+ For sinatra, add
12
+
13
+ use RackFixContentType::Middleware
14
+
15
+ Other rack applications need to use the middleware accordingly.
16
+
17
+
18
+ ==License
19
+
20
+ Uses the MIT-LICENSE
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rake/rdoctask'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'RackFixContentType'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1 @@
1
+ require 'rack_fix_content_type/middleware'
@@ -0,0 +1,41 @@
1
+ module RackFixContentType
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ fix_content_type(env["rack.request.form_hash"]) if env["REQUEST_METHOD"] == "POST"
9
+ @app.call(env)
10
+ end
11
+
12
+ protected
13
+
14
+ def fix_content_type(params)
15
+ params.each do |key,param|
16
+ next unless param.instance_of?(Hash)
17
+ next fix_content_type(param) unless param[:tempfile] && param[:tempfile].respond_to?(:path)
18
+ param[:type] = content_type(param[:tempfile].path, param[:filename])
19
+ end
20
+ end
21
+
22
+ # blatantly stolen from lib/paperclip/upfile.rb
23
+ def content_type(path, filename)
24
+ type = (filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
25
+ case type
26
+ when %r"jp(e|g|eg)" then "image/jpeg"
27
+ when %r"tiff?" then "image/tiff"
28
+ when %r"png", "gif", "bmp" then "image/#{type}"
29
+ when "txt" then "text/plain"
30
+ when %r"html?" then "text/html"
31
+ when "js" then "application/js"
32
+ when "csv", "xml", "css" then "text/#{type}"
33
+ else
34
+ # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
35
+ content_type = (`file -b --mime-type #{path}`.split(':').last.strip rescue "application/x-#{type}")
36
+ content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
37
+ content_type
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-fix-content-type
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - Gregor Melhorn
13
+ - Michael Raidel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-11 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: fix the content type of form uploads by using the file-utility. A lot of browsers screw up the content type so we don't trust the given value in the header.
23
+ email:
24
+ - g.melhorn@autohaus24.de
25
+ - raidel@induktiv.at
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/rack_fix_content_type/middleware.rb
34
+ - lib/rack-fix-content-type.rb
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - Gemfile
38
+ - README.rdoc
39
+ has_rdoc: true
40
+ homepage: ""
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.5.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: fix the content type of form uploads by using the file-utility.
73
+ test_files: []
74
+