Skip to content
This repository
Browse code

init commit

  • Loading branch information...
commit 86804cd1b00dd9accb49198ee45cbcb2b842f888 0 parents
Wayne authored
18 .gitignore
... ... @@ -0,0 +1,18 @@
  1 +.DS_Store
  2 +*.gem
  3 +*.rbc
  4 +.bundle
  5 +.config
  6 +.yardoc
  7 +Gemfile.lock
  8 +InstalledFiles
  9 +_yardoc
  10 +coverage
  11 +doc/
  12 +lib/bundler/man
  13 +pkg
  14 +rdoc
  15 +spec/reports
  16 +test/tmp
  17 +test/version_tmp
  18 +tmp
2  .rspec
... ... @@ -0,0 +1,2 @@
  1 +--color
  2 +--format documentation
6 Gemfile
... ... @@ -0,0 +1,6 @@
  1 +source 'https://rubygems.org'
  2 +
  3 +# Specify your gem's dependencies in s3_direct_upload.gemspec
  4 +gemspec
  5 +gem 'rspec'
  6 +gem 'jquery-fileupload-rails'
22 LICENSE
... ... @@ -0,0 +1,22 @@
  1 +Copyright (c) 2012 Wayne
  2 +
  3 +MIT License
  4 +
  5 +Permission is hereby granted, free of charge, to any person obtaining
  6 +a copy of this software and associated documentation files (the
  7 +"Software"), to deal in the Software without restriction, including
  8 +without limitation the rights to use, copy, modify, merge, publish,
  9 +distribute, sublicense, and/or sell copies of the Software, and to
  10 +permit persons to whom the Software is furnished to do so, subject to
  11 +the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be
  14 +included in all copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20 +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21 +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22 +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 README.md
Source Rendered
... ... @@ -0,0 +1,58 @@
  1 +# S3DirectUpload
  2 +
  3 +TODO: Write a gem description
  4 +
  5 +## Installation
  6 +
  7 +Add this line to your application's Gemfile:
  8 +
  9 + gem 's3_direct_upload'
  10 +
  11 +Then add a new initalizer with this code:
  12 +```
  13 +S3DirectUpload.config do |c|
  14 + c.access_key_id = "" # your access key id
  15 + c.secret_access_key = "" # your secret access key
  16 + c.bucket = "" # your bucket name
  17 +end
  18 +```
  19 +
  20 +S3 Cors Config should look like this:
  21 +```
  22 +<CORSConfiguration>
  23 + <CORSRule>
  24 + <AllowedOrigin>http://0.0.0.0:3000</AllowedOrigin>
  25 + <AllowedMethod>GET</AllowedMethod>
  26 + <AllowedMethod>POST</AllowedMethod>
  27 + <AllowedMethod>PUT</AllowedMethod>
  28 + <MaxAgeSeconds>3000</MaxAgeSeconds>
  29 + <AllowedHeader>*</AllowedHeader>
  30 + </CORSRule>
  31 +</CORSConfiguration>
  32 +```
  33 +
  34 +This gem requires the gem "jquery-fileupload-rails", which is already included by the gem.
  35 +
  36 +You will need to add the following jquery-fileupload assets to your asset pipeline:
  37 +
  38 +application.js
  39 +```
  40 +//= require jquery-fileupload/basic
  41 +//= require jquery-fileupload/vendor/tmpl
  42 +```
  43 +## Usage
  44 +
  45 +Create a new view that uses the helper:
  46 +```
  47 +<%= s3_uploader_form post: paintings_url, as: "painting[image_url]" do %>
  48 + <%= file_field_tag :file, multiple: true %>
  49 +<% end %>
  50 +```
  51 +
  52 +## Contributing
  53 +
  54 +1. Fork it
  55 +2. Create your feature branch (`git checkout -b my-new-feature`)
  56 +3. Commit your changes (`git commit -am 'Added some feature'`)
  57 +4. Push to the branch (`git push origin my-new-feature`)
  58 +5. Create new Pull Request
2  Rakefile
... ... @@ -0,0 +1,2 @@
  1 +#!/usr/bin/env rake
  2 +require "bundler/gem_tasks"
11 lib/s3_direct_upload.rb
... ... @@ -0,0 +1,11 @@
  1 +require "s3_direct_upload/version"
  2 +
  3 +require 'base64'
  4 +require 'openssl'
  5 +require 'digest/sha1'
  6 +
  7 +require 's3_direct_upload/config_aws'
  8 +require 's3_direct_upload/form_helper'
  9 +require 's3_direct_upload/engine'
  10 +
  11 +ActionView::Base.send(:include, S3DirectUpload::UploadHelper) if defined?(ActionView::Base)
18 lib/s3_direct_upload/config_aws.rb
... ... @@ -0,0 +1,18 @@
  1 +require "singleton"
  2 +
  3 +module S3DirectUpload
  4 + class Config
  5 + include Singleton
  6 +
  7 + ATTRIBUTES = [:access_key_id, :secret_access_key, :bucket]
  8 +
  9 + attr_accessor *ATTRIBUTES
  10 + end
  11 +
  12 + def self.config
  13 + if block_given?
  14 + yield Config.instance
  15 + end
  16 + Config.instance
  17 + end
  18 +end
4 lib/s3_direct_upload/engine.rb
... ... @@ -0,0 +1,4 @@
  1 +module S3DirectUpload
  2 + class Engine < ::Rails::Engine
  3 + end
  4 +end
84 lib/s3_direct_upload/form_helper.rb
... ... @@ -0,0 +1,84 @@
  1 +module S3DirectUpload
  2 + module UploadHelper
  3 + def s3_uploader_form(options = {}, &block)
  4 + uploader = S3Uploader.new(options)
  5 + form_tag(uploader.url, uploader.form_options) do
  6 + uploader.fields.map do |name, value|
  7 + hidden_field_tag(name, value)
  8 + end.join.html_safe + capture(&block)
  9 + end
  10 + end
  11 +
  12 + class S3Uploader
  13 + def initialize(options)
  14 + @options = options.reverse_merge(
  15 + id: "fileupload",
  16 + aws_access_key_id: S3DirectUpload.config.access_key_id,
  17 + aws_secret_access_key: S3DirectUpload.config.secret_access_key,
  18 + bucket: S3DirectUpload.config.bucket,
  19 + acl: "public-read",
  20 + expiration: 10.hours.from_now,
  21 + max_file_size: 500.megabytes,
  22 + as: "file"
  23 + )
  24 + end
  25 +
  26 + def form_options
  27 + {
  28 + id: @options[:id],
  29 + method: "post",
  30 + authenticity_token: false,
  31 + multipart: true,
  32 + data: {
  33 + post: @options[:post],
  34 + as: @options[:as]
  35 + }
  36 + }
  37 + end
  38 +
  39 + def fields
  40 + {
  41 + :key => key,
  42 + :acl => @options[:acl],
  43 + :policy => policy,
  44 + :signature => signature,
  45 + "AWSAccessKeyId" => @options[:aws_access_key_id],
  46 + }
  47 + end
  48 +
  49 + def key
  50 + @key ||= "uploads/#{SecureRandom.hex}/${filename}"
  51 + end
  52 +
  53 + def url
  54 + "https://#{@options[:bucket]}.s3.amazonaws.com/"
  55 + end
  56 +
  57 + def policy
  58 + Base64.encode64(policy_data.to_json).gsub("\n", "")
  59 + end
  60 +
  61 + def policy_data
  62 + {
  63 + expiration: @options[:expiration],
  64 + conditions: [
  65 + ["starts-with", "$utf8", ""],
  66 + ["starts-with", "$key", ""],
  67 + ["content-length-range", 0, @options[:max_file_size]],
  68 + {bucket: @options[:bucket]},
  69 + {acl: @options[:acl]}
  70 + ]
  71 + }
  72 + end
  73 +
  74 + def signature
  75 + Base64.encode64(
  76 + OpenSSL::HMAC.digest(
  77 + OpenSSL::Digest::Digest.new('sha1'),
  78 + @options[:aws_secret_access_key], policy
  79 + )
  80 + ).gsub("\n", "")
  81 + end
  82 + end
  83 + end
  84 +end
3  lib/s3_direct_upload/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module S3DirectUpload
  2 + VERSION = "0.0.1"
  3 +end
20 s3_direct_upload.gemspec
... ... @@ -0,0 +1,20 @@
  1 +# -*- encoding: utf-8 -*-
  2 +require File.expand_path('../lib/s3_direct_upload/version', __FILE__)
  3 +
  4 +Gem::Specification.new do |gem|
  5 + gem.authors = ["Wayne"]
  6 + gem.email = ["wayne@blissofbeing.com"]
  7 + gem.description = %q{Direct Upload to Amazon S3 With CORS}
  8 + gem.summary = %q{Gives a form helper for Rails which allows direct uploads to s3}
  9 + gem.homepage = ""
  10 +
  11 + gem.files = `git ls-files`.split($\)
  12 + gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
  13 + gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
  14 + gem.name = "s3_direct_upload"
  15 + gem.require_paths = ["lib"]
  16 + gem.version = S3DirectUpload::VERSION
  17 +
  18 + gem.add_dependency 'rails', '~> 3.2'
  19 + gem.add_dependency 'jquery-fileupload-rails', '~> 0.3.4'
  20 +end
18 spec/existance_spec.rb
... ... @@ -0,0 +1,18 @@
  1 +require 'spec_helper'
  2 +describe S3DirectUpload do
  3 + it "version must be defined" do
  4 + S3DirectUpload::VERSION.should be_true
  5 + end
  6 +
  7 + it "config must be defined" do
  8 + S3DirectUpload.config.should be_true
  9 + end
  10 +
  11 + it "engine must be defined"
  12 + pending
  13 + end
  14 +
  15 + it "form helper must be defined"
  16 + pending
  17 + end
  18 +end
9 spec/spec_helper.rb
... ... @@ -0,0 +1,9 @@
  1 +require 's3_direct_upload'
  2 +
  3 +RSpec.configure do |config|
  4 + config.treat_symbols_as_metadata_keys_with_true_values = true
  5 + config.run_all_when_everything_filtered = true
  6 + config.filter_run :focus
  7 +
  8 + config.order = 'random'
  9 +end

0 comments on commit 86804cd

Please sign in to comment.
Something went wrong with that request. Please try again.