rack-uploads

rack-uploads is a middleware which receives uploads and stores them in in the Rack env for easy access.

It works with normal HTTP file uploads, as well as with the Nginx Upload Module.

All multipart params get replaced by a Rack::Uploads::UploadedFile, while still retaining access to the original parameter value.

Dependencies

Development dependencies:

  • rspec
  • rack-test

Usage

Sinatra

use Rack::Uploads

post "/uploads" do
env['rack.uploads'].each do |upload|
  upload.mv('/some/path/#{upload.filename}')
end
end

Rails

# config/environment.rb
config.middleware.use "Rack::Uploads"

# app/controller/uploads_controller.rb
class UploadsController < ApplicationController
def create
  request['rack.uploads'].each do |upload|
    upload.mv("#{RAILS_ROOT}/public/uploads/#{upload.filename}")
  end
end
end

Options

There are a few options you can pass to rack-uploads during initializiation:

:session_authorized => lambda { |req| req.params == "sekrit" } - Only allow uploads with the parameter "secret" set to "sekrit"

:nginx => [{ :tmp_path => "_tmp_path", :filename => "_file_name" }] - Sets the suffixes of the nginx upload parameters

Copyright (c) 2009 Mutwin Kraus. See LICENSE for details.