Code Monkey home page Code Monkey logo

rack-large-uploads's Introduction

Rack::LargeUploads

Rack middleware for handling large file uploads.Integrates nicely with the Nginx upload module: http://www.grid.net.ru/nginx/upload.en.html

Includes Rack::LargeUploads::UploadedFile, which matches the definition of ActionDispatch::Http::UploadedFile. So, little-to-no change should be required when using, e.g., Rails.

Based largely on the Rack::Uploads middleware, but with greater expectations regarding conventions, and specific support for large files (dealing with memory issues).

Implements (experimental) handling of chunked uploads, only passing control to, e.g., Rails once all uploads for the request are complete. This is very experimental, because it is currently:

  • dependent upon headers and filename value as set by blueimp's jQuery fileupload plugin
  • dependent on the presence of uploader request param, which is a UUID or similar, and is used to collect the upload chunks
  • untested

Installation

Add this line to your application's Gemfile:

gem 'rack-large-uploads'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-large-uploads

Usage

example middleware configuration:

use Rack::LargeUploads do
  before do |files|
    # process uploaded files *before* application handling
  end

  after do |files|
    # process uploaded files *after* application handling
  end
end

example nginx configuration:

location / {
  try_files $document_root/system/maintenance.html $uri $uri/index.html $uri.html @application;
}

location = /uploads {
  # depends on the nginx upload module (http://www.grid.net.ru/nginx/upload.en.html)
  upload_pass         @application;

  # NOTE: if the request is something other than POST, nginx generates a 405.
  # use that to pass the request on to the endpoint, instead of try_files
  # which fails to play nicely with upload_pass.
  #
  # credit:
  # http://www.nickager.com/blog/File-upload-using-Nginx-and-Seaside---step-1
  error_page          405 415 = @application;

  # NOTE: cleaning up 201 & 202 means we need to be sure our app is doing
  # some processing right away. Rack::LargeUploads sends 202 responses for
  # each chunk received during chunked uploads, and the chunk has been
  # appended to the combined file by the time we're back with the response.
  upload_cleanup      201 202 400 404 499 500-505;
  upload_store        /path/to/app/tmp/uploads 1;
  upload_store_access user:rw group:rw all:rw;

  upload_pass_args       on;          # NOTE: handles URI params, not form content.
  upload_pass_form_field "^[a-z_].*"; # ...and here is how we resolve that last.

  # match the request params expected by ActionDispatch
  upload_set_form_field "$upload_field_name[filename]"   "$upload_file_name";
  upload_set_form_field "$upload_field_name[type]"       "$upload_content_type";
  upload_set_form_field "$upload_field_name[tempfile]"   "$upload_tmp_path";

  upload_aggregate_form_field "$upload_field_name[md5]"  "$upload_file_md5";
  upload_aggregate_form_field "$upload_field_name[size]" "$upload_file_size";
}

location @application {
  proxy_pass  http://upstream_application;

  # more config...
}

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

rack-large-uploads's People

Watchers

James Cloos avatar Thayne Trevenen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.