├── log └── .keep ├── app ├── mailers │ └── .keep ├── models │ ├── .keep │ ├── concerns │ │ └── .keep │ └── document.rb ├── assets │ ├── images │ │ └── .keep │ ├── stylesheets │ │ └── application.css.scss │ └── javascripts │ │ ├── application.js │ │ └── documents.js ├── controllers │ ├── concerns │ │ └── .keep │ ├── application_controller.rb │ └── documents_controller.rb ├── helpers │ └── application_helper.rb └── views │ ├── documents │ ├── _document.html.erb │ ├── create.js.erb │ └── new.html.erb │ └── layouts │ └── application.html.erb ├── lib ├── assets │ └── .keep └── tasks │ └── .keep ├── public ├── favicon.ico ├── robots.txt ├── 500.html ├── 422.html └── 404.html ├── test ├── helpers │ └── .keep ├── mailers │ └── .keep ├── models │ ├── .keep │ └── document_test.rb ├── controllers │ ├── .keep │ └── documents_controller_test.rb ├── fixtures │ ├── .keep │ └── documents.yml ├── integration │ └── .keep └── test_helper.rb ├── vendor └── assets │ ├── javascripts │ └── .keep │ └── stylesheets │ └── .keep ├── config ├── initializers │ ├── delayed_job.rb │ ├── cookies_serializer.rb │ ├── mime_types.rb │ ├── session_store.rb │ ├── filter_parameter_logging.rb │ ├── aws.rb │ ├── s3_direct_upload.rb │ ├── paperclip.rb │ ├── backtrace_silencers.rb │ ├── wrap_parameters.rb │ └── inflections.rb ├── environment.rb ├── boot.rb ├── routes.rb ├── database.yml ├── secrets.yml ├── locales │ └── en.yml ├── application.rb └── environments │ ├── development.rb │ ├── test.rb │ └── production.rb ├── README.rdoc ├── bin ├── bundle ├── rake ├── delayed_job ├── rails └── spring ├── config.ru ├── Rakefile ├── Gemfile ├── db ├── seeds.rb ├── migrate │ ├── 20140511152059_create_documents.rb │ └── 20140511160943_create_delayed_jobs.rb └── schema.rb ├── .gitignore └── Gemfile.lock /log/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/mailers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/models/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/assets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/helpers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/mailers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/models/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/assets/images/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/controllers/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/integration/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/assets/javascripts/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/assets/stylesheets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/assets/stylesheets/application.css.scss: -------------------------------------------------------------------------------- 1 | @import "s3_direct_upload_progress_bars"; -------------------------------------------------------------------------------- /config/initializers/delayed_job.rb: -------------------------------------------------------------------------------- 1 | Delayed::Worker.delay_jobs = Rails.env.production? -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | Code example for http://blog.littleblimp.com/post/53942611764/direct-uploads-to-s3-with-rails-paperclip-and -------------------------------------------------------------------------------- /app/assets/javascripts/application.js: -------------------------------------------------------------------------------- 1 | //= require jquery 2 | //= require jquery_ujs 3 | //= require s3_direct_upload 4 | //= require_tree . -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /config/initializers/cookies_serializer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.action_dispatch.cookies_serializer = :json -------------------------------------------------------------------------------- /test/models/document_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class DocumentTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Rails.application 5 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | # Set up gems listed in the Gemfile. 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | 4 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path("../spring", __FILE__) 4 | rescue LoadError 5 | end 6 | require_relative '../config/boot' 7 | require 'rake' 8 | Rake.application.run 9 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | -------------------------------------------------------------------------------- /config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.session_store :cookie_store, key: '_s3_direct_upload_example_session' 4 | -------------------------------------------------------------------------------- /bin/delayed_job: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) 4 | require 'delayed/command' 5 | Delayed::Command.new(ARGV).daemonize 6 | -------------------------------------------------------------------------------- /test/controllers/documents_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class DocumentsControllerTest < ActionController::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path("../spring", __FILE__) 4 | rescue LoadError 5 | end 6 | APP_PATH = File.expand_path('../../config/application', __FILE__) 7 | require_relative '../config/boot' 8 | require 'rails/commands' 9 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /config/initializers/aws.rb: -------------------------------------------------------------------------------- 1 | AWS.config( 2 | access_key_id: Rails.application.secrets.aws['access_key_id'], 3 | secret_access_key: Rails.application.secrets.aws['secret_access_key'], 4 | bucket: Rails.application.secrets.aws['s3_bucket_name'] 5 | ) -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | resources :documents, only: [:new, :create] 3 | get 'documents/:id/download/:filename', to: 'documents#download', constraints: { filename: /.+/ }, as: 'download_document' 4 | root to: 'documents#new' 5 | end 6 | -------------------------------------------------------------------------------- /app/views/documents/_document.html.erb: -------------------------------------------------------------------------------- 1 |
2 | <%= document.upload_file_name %>
3 | <%= number_to_human_size(document.upload_file_size) %>
4 | <%= link_to "Download", download_document_path(id: document.id, filename: document.upload_file_name) %> 5 |
-------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: postgresql 3 | encoding: unicode 4 | database: s3_direct_upload_example_development 5 | host: localhost 6 | pool: 5 7 | 8 | test: 9 | adapter: postgresql 10 | encoding: unicode 11 | database: s3_direct_upload_example_test 12 | host: localhost 13 | pool: 5 -------------------------------------------------------------------------------- /app/views/documents/create.js.erb: -------------------------------------------------------------------------------- 1 | <% if @document.persisted? %> 2 | $('#upload_<%=params[:unique_id]%>').remove(); 3 | $('#uploads_container').append("<%= j(render @document) %>"); 4 | <% else %> 5 | $('#upload_<%=params[:unique_id]%> div.progress').removeClass('active progress-striped').addClass('progress-danger'); 6 | <% end %> -------------------------------------------------------------------------------- /config/initializers/s3_direct_upload.rb: -------------------------------------------------------------------------------- 1 | S3DirectUpload.config do |c| 2 | c.access_key_id = Rails.application.secrets.aws['access_key_id'] 3 | c.secret_access_key = Rails.application.secrets.aws['secret_access_key'] 4 | c.bucket = Rails.application.secrets.aws['s3_bucket_name'] 5 | c.region = "s3" 6 | end -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | S3DirectUploadExample 5 | <%= stylesheet_link_tag 'application', media: 'all' %> 6 | <%= javascript_include_tag 'application' %> 7 | <%= csrf_meta_tags %> 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'aws-sdk' 4 | gem 'delayed_job_active_record' 5 | gem 'jquery-rails' 6 | gem 'jquery-ui-rails' 7 | gem 'paperclip', '~> 3.5' 8 | gem 'pg' 9 | gem 'rails', '4.1.1' 10 | gem 's3_direct_upload' 11 | gem 'sass-rails', '~> 4.0.3' 12 | gem 'uglifier', '>= 1.3.0' 13 | 14 | group :development do 15 | gem 'annotate' 16 | end -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) 7 | # Mayor.create(name: 'Emanuel', city: cities.first) 8 | -------------------------------------------------------------------------------- /db/migrate/20140511152059_create_documents.rb: -------------------------------------------------------------------------------- 1 | class CreateDocuments < ActiveRecord::Migration 2 | def change 3 | create_table :documents do |t| 4 | t.string :direct_upload_url, null: false 5 | t.attachment :upload 6 | t.boolean :processed, default: false, null: false 7 | t.timestamps 8 | end 9 | add_index :documents, :processed 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /config/initializers/paperclip.rb: -------------------------------------------------------------------------------- 1 | Paperclip::Attachment.default_options.merge!( 2 | storage: :s3, 3 | s3_credentials: { 4 | access_key_id: Rails.application.secrets.aws['access_key_id'], 5 | secret_access_key: Rails.application.secrets.aws['secret_access_key'], 6 | bucket: Rails.application.secrets.aws['s3_bucket_name'] 7 | }, 8 | s3_permissions: :public_read, 9 | s3_protocol: 'https' 10 | ) -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | require File.expand_path('../../config/environment', __FILE__) 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 7 | # 8 | # Note: You'll currently still have to declare fixtures explicitly in integration tests 9 | # -- they do not yet inherit this setting 10 | fixtures :all 11 | 12 | # Add more helper methods to be used by all tests here... 13 | end 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-journal 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log/*.log 16 | /tmp 17 | -------------------------------------------------------------------------------- /app/controllers/documents_controller.rb: -------------------------------------------------------------------------------- 1 | class DocumentsController < ApplicationController 2 | 3 | before_action :set_document, only: [:download] 4 | 5 | def create 6 | @document = Document.create(document_params) 7 | end 8 | 9 | def download 10 | redirect_to @document.upload.expiring_url(30.seconds, :original) 11 | end 12 | 13 | private 14 | 15 | def set_document 16 | @document = Document.find(params[:id]) 17 | end 18 | 19 | def document_params 20 | params.require(:document).permit(:direct_upload_url) 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast 4 | # It gets overwritten when you run the `spring binstub` command 5 | 6 | unless defined?(Spring) 7 | require "rubygems" 8 | require "bundler" 9 | 10 | if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ spring \((.*?)\)$.*?^$/m) 11 | ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR) 12 | ENV["GEM_HOME"] = "" 13 | Gem.paths = ENV 14 | 15 | gem "spring", match[1] 16 | require "spring/binstub" 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /app/views/documents/new.html.erb: -------------------------------------------------------------------------------- 1 | <%= s3_uploader_form id: "s3-uploader", 2 | callback_url: documents_url, 3 | callback_param: "document[direct_upload_url]", 4 | expiration: 24.hours.from_now.utc.iso8601, 5 | max_file_size: 10.megabytes do %> 6 | <%= file_field_tag :file, multiple: true %> 7 | <% end %> 8 | 9 |
10 | 11 | -------------------------------------------------------------------------------- /config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] if respond_to?(:wrap_parameters) 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /app/assets/javascripts/documents.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | $('#s3-uploader').S3Uploader( 3 | { 4 | remove_completed_progress_bar: false, 5 | progress_bar_target: $('#uploads_container'), 6 | before_add: function(file) { 7 | if (file.size > 10485760) { 8 | alert('Maximum file size is 10 MB'); 9 | return false; 10 | } else { 11 | return true; 12 | } 13 | } 14 | } 15 | ); 16 | 17 | // error handling 18 | $('#s3-uploader').bind('s3_upload_failed', function(e, content) { 19 | return alert(content.filename + ' failed to upload.'); 20 | }); 21 | }); -------------------------------------------------------------------------------- /config/secrets.yml: -------------------------------------------------------------------------------- 1 | development: 2 | secret_key_base: secret_key_base 3 | aws: 4 | access_key_id: "access_key_id" 5 | secret_access_key: "secret_access_key" 6 | s3_bucket_name: "myapp-development" 7 | test: 8 | secret_key_base: secret_key_base 9 | aws: 10 | access_key_id: "access_key_id" 11 | secret_access_key: "secret_access_key" 12 | s3_bucket_name: "myapp-development" 13 | production: 14 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 15 | aws: 16 | access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %> 17 | secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %> 18 | s3_bucket_name: <%= ENV["S3_BUCKET_NAME"] %> -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /test/fixtures/documents.yml: -------------------------------------------------------------------------------- 1 | # == Schema Information 2 | # 3 | # Table name: documents 4 | # 5 | # id :integer not null, primary key 6 | # direct_upload_url :string(255) not null 7 | # upload_file_name :string(255) 8 | # upload_content_type :string(255) 9 | # upload_file_size :integer 10 | # upload_updated_at :datetime 11 | # processed :boolean default(FALSE), not null 12 | # created_at :datetime 13 | # updated_at :datetime 14 | # 15 | 16 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 17 | 18 | # This model initially had no columns defined. If you add columns to the 19 | # model remove the '{}' from the fixture names and add the columns immediately 20 | # below each fixture, per the syntax in the comments below 21 | # 22 | one: {} 23 | # column: value 24 | # 25 | two: {} 26 | # column: value 27 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require 'rails/all' 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module S3DirectUploadExample 10 | class Application < Rails::Application 11 | # Settings in config/environments/* take precedence over those specified here. 12 | # Application configuration should go into files in config/initializers 13 | # -- all .rb files in that directory are automatically loaded. 14 | 15 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 16 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 17 | # config.time_zone = 'Central Time (US & Canada)' 18 | 19 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 20 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 21 | # config.i18n.default_locale = :de 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /db/migrate/20140511160943_create_delayed_jobs.rb: -------------------------------------------------------------------------------- 1 | class CreateDelayedJobs < ActiveRecord::Migration 2 | def self.up 3 | create_table :delayed_jobs, :force => true do |table| 4 | table.integer :priority, :default => 0, :null => false # Allows some jobs to jump to the front of the queue 5 | table.integer :attempts, :default => 0, :null => false # Provides for retries, but still fail eventually. 6 | table.text :handler, :null => false # YAML-encoded string of the object that will do work 7 | table.text :last_error # reason for last failure (See Note below) 8 | table.datetime :run_at # When to run. Could be Time.zone.now for immediately, or sometime in the future. 9 | table.datetime :locked_at # Set when a client is working on this object 10 | table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) 11 | table.string :locked_by # Who is working on this object (if locked) 12 | table.string :queue # The name of the queue this job is in 13 | table.timestamps 14 | end 15 | 16 | add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority' 17 | end 18 | 19 | def self.down 20 | drop_table :delayed_jobs 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports and disable caching. 13 | config.consider_all_requests_local = true 14 | config.action_controller.perform_caching = false 15 | 16 | # Don't care if the mailer can't send. 17 | config.action_mailer.raise_delivery_errors = false 18 | 19 | # Print deprecation notices to the Rails logger. 20 | config.active_support.deprecation = :log 21 | 22 | # Raise an error on page load if there are pending migrations. 23 | config.active_record.migration_error = :page_load 24 | 25 | # Debug mode disables concatenation and preprocessing of assets. 26 | # This option may cause significant delays in view rendering with a large 27 | # number of complex assets. 28 | config.assets.debug = false 29 | 30 | # Adds additional error checking when serving assets at runtime. 31 | # Checks for improperly declared sprockets dependencies. 32 | # Raises helpful error messages. 33 | config.assets.raise_runtime_errors = true 34 | 35 | # Raises error for missing translations 36 | # config.action_view.raise_on_missing_translations = true 37 | end 38 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure static asset server for tests with Cache-Control for performance. 16 | config.serve_static_assets = true 17 | config.static_cache_control = 'public, max-age=3600' 18 | 19 | # Show full error reports and disable caching. 20 | config.consider_all_requests_local = true 21 | config.action_controller.perform_caching = false 22 | 23 | # Raise exceptions instead of rendering exception templates. 24 | config.action_dispatch.show_exceptions = false 25 | 26 | # Disable request forgery protection in test environment. 27 | config.action_controller.allow_forgery_protection = false 28 | 29 | # Tell Action Mailer not to deliver emails to the real world. 30 | # The :test delivery method accumulates sent emails in the 31 | # ActionMailer::Base.deliveries array. 32 | config.action_mailer.delivery_method = :test 33 | 34 | # Print deprecation notices to the stderr. 35 | config.active_support.deprecation = :stderr 36 | 37 | # Raises error for missing translations 38 | # config.action_view.raise_on_missing_translations = true 39 | end 40 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20140511160943) do 15 | 16 | # These are extensions that must be enabled in order to support this database 17 | enable_extension "plpgsql" 18 | 19 | create_table "delayed_jobs", force: true do |t| 20 | t.integer "priority", default: 0, null: false 21 | t.integer "attempts", default: 0, null: false 22 | t.text "handler", null: false 23 | t.text "last_error" 24 | t.datetime "run_at" 25 | t.datetime "locked_at" 26 | t.datetime "failed_at" 27 | t.string "locked_by" 28 | t.string "queue" 29 | t.datetime "created_at" 30 | t.datetime "updated_at" 31 | end 32 | 33 | add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree 34 | 35 | create_table "documents", force: true do |t| 36 | t.string "direct_upload_url", null: false 37 | t.string "upload_file_name" 38 | t.string "upload_content_type" 39 | t.integer "upload_file_size" 40 | t.datetime "upload_updated_at" 41 | t.boolean "processed", default: false, null: false 42 | t.datetime "created_at" 43 | t.datetime "updated_at" 44 | end 45 | 46 | add_index "documents", ["processed"], name: "index_documents_on_processed", using: :btree 47 | 48 | end 49 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both threaded web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Enable Rack::Cache to put a simple HTTP cache in front of your application 18 | # Add `rack-cache` to your Gemfile before enabling this. 19 | # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid. 20 | # config.action_dispatch.rack_cache = true 21 | 22 | # Disable Rails's static asset server (Apache or nginx will already do this). 23 | config.serve_static_assets = false 24 | 25 | # Compress JavaScripts and CSS. 26 | config.assets.js_compressor = :uglifier 27 | # config.assets.css_compressor = :sass 28 | 29 | # Do not fallback to assets pipeline if a precompiled asset is missed. 30 | config.assets.compile = false 31 | 32 | # Generate digests for assets URLs. 33 | config.assets.digest = true 34 | 35 | # Version of your assets, change this if you want to expire all your assets. 36 | config.assets.version = '1.0' 37 | 38 | # Specifies the header that your server uses for sending files. 39 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache 40 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Set to :debug to see everything in the log. 46 | config.log_level = :info 47 | 48 | # Prepend all log lines with the following tags. 49 | # config.log_tags = [ :subdomain, :uuid ] 50 | 51 | # Use a different logger for distributed setups. 52 | # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) 53 | 54 | # Use a different cache store in production. 55 | # config.cache_store = :mem_cache_store 56 | 57 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 58 | # config.action_controller.asset_host = "http://assets.example.com" 59 | 60 | # Precompile additional assets. 61 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 62 | # config.assets.precompile += %w( search.js ) 63 | 64 | # Ignore bad email addresses and do not raise email delivery errors. 65 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 66 | # config.action_mailer.raise_delivery_errors = false 67 | 68 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 69 | # the I18n.default_locale when a translation cannot be found). 70 | config.i18n.fallbacks = true 71 | 72 | # Send deprecation notices to registered listeners. 73 | config.active_support.deprecation = :notify 74 | 75 | # Disable automatic flushing of the log to improve performance. 76 | # config.autoflush_log = false 77 | 78 | # Use default logging formatter so that PID and timestamp are not suppressed. 79 | config.log_formatter = ::Logger::Formatter.new 80 | 81 | # Do not dump schema after migrations. 82 | config.active_record.dump_schema_after_migration = false 83 | end 84 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actionmailer (4.1.1) 5 | actionpack (= 4.1.1) 6 | actionview (= 4.1.1) 7 | mail (~> 2.5.4) 8 | actionpack (4.1.1) 9 | actionview (= 4.1.1) 10 | activesupport (= 4.1.1) 11 | rack (~> 1.5.2) 12 | rack-test (~> 0.6.2) 13 | actionview (4.1.1) 14 | activesupport (= 4.1.1) 15 | builder (~> 3.1) 16 | erubis (~> 2.7.0) 17 | activemodel (4.1.1) 18 | activesupport (= 4.1.1) 19 | builder (~> 3.1) 20 | activerecord (4.1.1) 21 | activemodel (= 4.1.1) 22 | activesupport (= 4.1.1) 23 | arel (~> 5.0.0) 24 | activesupport (4.1.1) 25 | i18n (~> 0.6, >= 0.6.9) 26 | json (~> 1.7, >= 1.7.7) 27 | minitest (~> 5.1) 28 | thread_safe (~> 0.1) 29 | tzinfo (~> 1.1) 30 | annotate (2.5.0) 31 | rake 32 | arel (5.0.1.20140414130214) 33 | aws-sdk (1.14.1) 34 | json (~> 1.4) 35 | nokogiri (< 1.6.0) 36 | uuidtools (~> 2.1) 37 | builder (3.2.2) 38 | climate_control (0.0.3) 39 | activesupport (>= 3.0) 40 | cocaine (0.5.4) 41 | climate_control (>= 0.0.3, < 1.0) 42 | coffee-rails (4.0.1) 43 | coffee-script (>= 2.2.0) 44 | railties (>= 4.0.0, < 5.0) 45 | coffee-script (2.2.0) 46 | coffee-script-source 47 | execjs 48 | coffee-script-source (1.7.0) 49 | delayed_job (4.0.1) 50 | activesupport (>= 3.0, < 4.2) 51 | delayed_job_active_record (4.0.1) 52 | activerecord (>= 3.0, < 4.2) 53 | delayed_job (>= 3.0, < 4.1) 54 | erubis (2.7.0) 55 | execjs (2.0.2) 56 | hike (1.2.3) 57 | i18n (0.6.9) 58 | jquery-fileupload-rails (0.4.1) 59 | actionpack (>= 3.1) 60 | railties (>= 3.1) 61 | jquery-rails (3.1.0) 62 | railties (>= 3.0, < 5.0) 63 | thor (>= 0.14, < 2.0) 64 | jquery-ui-rails (4.0.4) 65 | jquery-rails 66 | railties (>= 3.1.0) 67 | json (1.8.1) 68 | mail (2.5.4) 69 | mime-types (~> 1.16) 70 | treetop (~> 1.4.8) 71 | mime-types (1.25.1) 72 | minitest (5.3.3) 73 | multi_json (1.10.0) 74 | nokogiri (1.5.10) 75 | paperclip (3.5.4) 76 | activemodel (>= 3.0.0) 77 | activesupport (>= 3.0.0) 78 | cocaine (~> 0.5.3) 79 | mime-types 80 | pg (0.17.1) 81 | polyglot (0.3.4) 82 | rack (1.5.2) 83 | rack-test (0.6.2) 84 | rack (>= 1.0) 85 | rails (4.1.1) 86 | actionmailer (= 4.1.1) 87 | actionpack (= 4.1.1) 88 | actionview (= 4.1.1) 89 | activemodel (= 4.1.1) 90 | activerecord (= 4.1.1) 91 | activesupport (= 4.1.1) 92 | bundler (>= 1.3.0, < 2.0) 93 | railties (= 4.1.1) 94 | sprockets-rails (~> 2.0) 95 | railties (4.1.1) 96 | actionpack (= 4.1.1) 97 | activesupport (= 4.1.1) 98 | rake (>= 0.8.7) 99 | thor (>= 0.18.1, < 2.0) 100 | rake (10.3.1) 101 | s3_direct_upload (0.1.6) 102 | coffee-rails (>= 3.2.1) 103 | jquery-fileupload-rails (~> 0.4.1) 104 | rails (>= 3.2) 105 | sass-rails (>= 3.2.5) 106 | sass (3.2.19) 107 | sass-rails (4.0.3) 108 | railties (>= 4.0.0, < 5.0) 109 | sass (~> 3.2.0) 110 | sprockets (~> 2.8, <= 2.11.0) 111 | sprockets-rails (~> 2.0) 112 | sprockets (2.11.0) 113 | hike (~> 1.2) 114 | multi_json (~> 1.0) 115 | rack (~> 1.0) 116 | tilt (~> 1.1, != 1.3.0) 117 | sprockets-rails (2.1.3) 118 | actionpack (>= 3.0) 119 | activesupport (>= 3.0) 120 | sprockets (~> 2.8) 121 | thor (0.19.1) 122 | thread_safe (0.3.3) 123 | tilt (1.4.1) 124 | treetop (1.4.15) 125 | polyglot 126 | polyglot (>= 0.3.1) 127 | tzinfo (1.1.0) 128 | thread_safe (~> 0.1) 129 | uglifier (2.5.0) 130 | execjs (>= 0.3.0) 131 | json (>= 1.8.0) 132 | uuidtools (2.1.4) 133 | 134 | PLATFORMS 135 | ruby 136 | 137 | DEPENDENCIES 138 | annotate 139 | aws-sdk 140 | delayed_job_active_record 141 | jquery-rails 142 | jquery-ui-rails 143 | paperclip (~> 3.5) 144 | pg 145 | rails (= 4.1.1) 146 | s3_direct_upload 147 | sass-rails (~> 4.0.3) 148 | uglifier (>= 1.3.0) 149 | -------------------------------------------------------------------------------- /app/models/document.rb: -------------------------------------------------------------------------------- 1 | # == Schema Information 2 | # 3 | # Table name: documents 4 | # 5 | # id :integer not null, primary key 6 | # direct_upload_url :string(255) not null 7 | # upload_file_name :string(255) 8 | # upload_content_type :string(255) 9 | # upload_file_size :integer 10 | # upload_updated_at :datetime 11 | # processed :boolean default(FALSE), not null 12 | # created_at :datetime 13 | # updated_at :datetime 14 | # 15 | 16 | class Document < ActiveRecord::Base 17 | 18 | BUCKET_NAME = Rails.application.secrets.aws['s3_bucket_name'] 19 | 20 | # Environment-specific direct upload url verifier screens for malicious posted upload locations. 21 | # 22 | # @note CORS required on s3 buckets to facilitate direct upload. 23 | # 24 | # @note Format matches that specified in s3-uploader_form: 25 | # "uploads/{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}" 26 | # 27 | # @example Valid URL 28 | # "https://s3.amazonaws.com/myapp-development/uploads/92a7d2c5-83de-47ad-981c-c6e391531a0e/foo.jpg" 29 | # 30 | # @example Invalid URL 31 | # "http://haxor.com/virus.exe" 32 | # 33 | # @example CORS config 34 | # 35 | # 36 | # 37 | # * 38 | # POST 39 | # 3000 40 | # * 41 | # 42 | # 43 | # 44 | DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/s3\.amazonaws\.com\/#{BUCKET_NAME}\/(?uploads\/.+\/(?.+))\z}.freeze 45 | 46 | has_attached_file :upload 47 | 48 | validates :direct_upload_url, presence: true, format: { with: DIRECT_UPLOAD_URL_FORMAT } 49 | 50 | before_create :set_upload_attributes 51 | after_create :queue_finalize_and_cleanup 52 | 53 | # Store an unescaped version of the escaped URL that Amazon returns from direct upload. 54 | def direct_upload_url=(escaped_url) 55 | write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil)) 56 | end 57 | 58 | # Update the document upload and manually re-process 59 | def update_file(params) 60 | self.processed = false 61 | self.attributes = params 62 | set_upload_attributes 63 | save! 64 | Document.delay.finalize_and_cleanup(id) 65 | end 66 | 67 | # Determines if file requires post-processing (image resizing, etc) 68 | def post_process_required? 69 | %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}.match(upload_content_type).present? 70 | end 71 | 72 | # Final upload processing step: 73 | # 74 | # 1) If the file does not require processing, manually copy direct uplaod to 75 | # the location that Paperclip expects, saving transfer time/cost. 76 | # If the file requires post-processing, set the direct_upload_url as the document's remote source, 77 | # which instantiates download, process, and re-upload of the file via Paperclip callbacks. 78 | # 2) Flag document as processed 79 | # 3) Delete the temp upload from s3. 80 | # 81 | # @see http://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjectUsingRuby.html 82 | def self.finalize_and_cleanup(id) 83 | document = Document.find(id) 84 | direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(document.direct_upload_url) 85 | s3 = AWS::S3.new 86 | 87 | if document.post_process_required? 88 | document.upload = URI.parse(URI.escape(document.direct_upload_url)) 89 | else 90 | paperclip_file_path = "documents/uploads/#{id}/original/#{direct_upload_url_data[:filename]}" 91 | s3.buckets[BUCKET_NAME].objects[paperclip_file_path].copy_from(direct_upload_url_data[:path]) 92 | end 93 | 94 | document.processed = true 95 | document.save 96 | 97 | s3.buckets[BUCKET_NAME].objects[direct_upload_url_data[:path]].delete 98 | end 99 | 100 | protected 101 | 102 | # Optional: Set attachment attributes from the direct upload instead of original upload callback params 103 | # @note Retry logic handles occasional S3 "eventual consistency" lag. 104 | def set_upload_attributes 105 | tries ||= 5 106 | direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(direct_upload_url) 107 | s3 = AWS::S3.new 108 | direct_upload_head = s3.buckets[BUCKET_NAME].objects[direct_upload_url_data[:path]].head 109 | 110 | self.upload_file_name = direct_upload_url_data[:filename] 111 | self.upload_file_size = direct_upload_head.content_length 112 | self.upload_content_type = direct_upload_head.content_type 113 | self.upload_updated_at = direct_upload_head.last_modified 114 | rescue AWS::S3::Errors::NoSuchKey => e 115 | tries -= 1 116 | if tries > 0 117 | sleep(3) 118 | retry 119 | else 120 | raise e 121 | end 122 | end 123 | 124 | # Queue final file processing 125 | def queue_finalize_and_cleanup 126 | Document.delay.finalize_and_cleanup(id) 127 | end 128 | 129 | end 130 | --------------------------------------------------------------------------------