Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Apr 22, 2009
0 parents commit d9eaa70
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008-2009 Michael Koziarski and Pratik Naik

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 changes: 14 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ModUpload
=========

This Plugin allows your rails applications to transparently take advantage of ModUpload.

Example
=======

class ApplicationController < ActionController::Base
self.mod_porter_secret = "over 9000 times as secure"
end


Copyright (c) 2008-2009 Michael Koziarski and Pratik Naik, released under the MIT license
23 changes: 23 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the mod_upload plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the mod_upload plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ModUpload'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
6 changes: 6 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Include hook code here
require 'mod_porter'

class ActionController::Base
include ModPorter::Filter
end
90 changes: 90 additions & 0 deletions lib/mod_porter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'strscan'

module ModPorter
class InvalidSignature < StandardError
end

class UnknownError < StandardError
end

class UploadedFile
attr_accessor :path, :content_type, :original_filename

def initialize(options)
@path = options[:path]
@original_filename = options[:filename]
@content_type = options[:content_type]
end
end

module ClassMethods
def porter_secret(val)
self.mod_porter_secret = val
end
end

module Filter
def self.included(base)
base.superclass_delegating_accessor :mod_porter_secret
base.before_filter :normalize_mod_porters
base.extend ModPorter::ClassMethods
end

def normalize_mod_porters
return if request.headers["X-Uploads"].blank?

porter_params = request.headers["X-Uploads"].split(",").uniq
logger.info("Processing #{porter_params.inspect}")

porter_params.each do |file_param|
s = StringScanner.new(file_param)

path = []
path << s.scan(/\w+/).to_sym

while !s.eos?
if arr = s.scan(/\[\]/)
path << [] # We have an array
elsif key = s.scan(/\[(\w+)\]/)
path << s[1].to_sym
else
raise ModPorter::UnknownError.new("Something went wrong when scaling the file uploads")
end
end

last = path.pop

h = path.inject(params) do |hash, value|
if value.is_a?(Array)
hash
else
hash[value]
end
end

if last.is_a?(Array)
h.map! do |e|
check_signature!(e)
UploadedFile.new(e)
end
else
while h.is_a?(Array)
h = h.first # WTF. file[][some1], file[][some2]. Maybe this work.
end

check_signature!(h[last])
h[last] = UploadedFile.new(h[last])
end
end
end

def check_signature!(options)
expected_digest = Digest::SHA1.digest("#{options[:path]}#{self.class.mod_porter_secret}")
base64_encoded_digest = ActiveSupport::Base64.encode64(expected_digest).chomp

if options[:signature] != base64_encoded_digest
raise ModPorter::InvalidSignature.new("#{options[:signature]} != #{base64_encoded_digest}")
end
end
end
end

0 comments on commit d9eaa70

Please sign in to comment.