s3_direct_rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +8 -0
- data/Rakefile +2 -0
- data/lib/s3_direct_rails/railtie.rb +13 -0
- data/lib/s3_direct_rails/uploader_helper.rb +58 -0
- data/lib/s3_direct_rails/version.rb +3 -0
- data/lib/s3_direct_rails.rb +20 -0
- data/lib/tasks/s3_direct_rails.rake +36 -0
- data/s3_direct_rails.gemspec +15 -0
- data/vendor/assets/javascripts/s3_uploader/jquery.fileupload.js +949 -0
- data/vendor/assets/javascripts/s3_uploader/jquery.iframe-transport.js +171 -0
- data/vendor/assets/javascripts/s3_uploader/s3_uploader.js +88 -0
- data/vendor/assets/javascripts/uploads.js.coffee.erb +59 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Cory Kaufman-Schofield
|
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.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module S3DirectRails
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
railtie_name :s3_direct_rails
|
4
|
+
|
5
|
+
initializer 's3_direct_rails.uploader_helper' do
|
6
|
+
ActionView::Base.send :include, UploaderHelper
|
7
|
+
end
|
8
|
+
|
9
|
+
rake_tasks do
|
10
|
+
load 'tasks/s3_direct_rails.rake'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module S3DirectRails
|
2
|
+
module UploaderHelper
|
3
|
+
|
4
|
+
def s3_uploader(options = {})
|
5
|
+
options[:uploader_path] ||= File.join(S3DirectRails.s3_uploader_folder,'uploader.html')
|
6
|
+
options[:uploaded_files_path] ||= "uploads/#{controller_name}/:uuid"
|
7
|
+
options[:create_resource_url] ||= url_for(only_path: false)
|
8
|
+
options[:resource_name] ||= controller_name.singularize
|
9
|
+
|
10
|
+
upload_params = { key: s3_key(options[:uploaded_files_path]),
|
11
|
+
AWSAccessKeyId: S3DirectRails.s3_access_key_id,
|
12
|
+
bucket: s3_bucket_url,
|
13
|
+
_policy: s3_policy(path: options[:uploaded_files_path]),
|
14
|
+
_signature: s3_signature(path: options[:uploaded_files_path]) }.to_query
|
15
|
+
|
16
|
+
content_tag :iframe, '',
|
17
|
+
src: "#{s3_bucket_url}/#{options[:uploader_path]}?#{upload_params}",
|
18
|
+
frameborder: 0,
|
19
|
+
height: options[:iframe_height] || 40,
|
20
|
+
width: options[:iframe_width] || 350,
|
21
|
+
data: { create_resource_url: options[:create_resource_url] }
|
22
|
+
end
|
23
|
+
|
24
|
+
def s3_bucket_url
|
25
|
+
"http://#{S3DirectRails.s3_uploader_bucket}.s3.amazonaws.com"
|
26
|
+
end
|
27
|
+
|
28
|
+
def s3_key(path)
|
29
|
+
"#{path}/${filename}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def s3_policy(options = {})
|
33
|
+
options[:content_type] ||= ''
|
34
|
+
options[:acl] ||= 'public-read'
|
35
|
+
options[:max_file_size] ||= 5.gigabytes
|
36
|
+
options[:path] ||= ''
|
37
|
+
|
38
|
+
Base64.encode64(
|
39
|
+
"{'expiration': '#{10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')}',
|
40
|
+
'conditions': [
|
41
|
+
{'bucket': '#{S3DirectRails.s3_uploader_bucket}'},
|
42
|
+
['starts-with', '$key', ''],
|
43
|
+
{'acl': '#{options[:acl]}'},
|
44
|
+
{'success_action_status': '201'},
|
45
|
+
['content-length-range', 0, #{options[:max_file_size]}],
|
46
|
+
['starts-with','$Content-Type','']
|
47
|
+
]
|
48
|
+
}").gsub(/\n|\r/, '')
|
49
|
+
end
|
50
|
+
|
51
|
+
def s3_signature options = {}
|
52
|
+
Base64.encode64(
|
53
|
+
OpenSSL::HMAC.digest(
|
54
|
+
OpenSSL::Digest::Digest.new('sha1'),
|
55
|
+
S3DirectRails.s3_secret_access_key, s3_policy(options))).gsub("\n","")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 's3_direct_rails/uploader_helper.rb'
|
2
|
+
require 's3_direct_rails/railtie' if defined?(Rails)
|
3
|
+
|
4
|
+
module S3DirectRails
|
5
|
+
mattr_accessor :s3_uploader_bucket
|
6
|
+
@@s3_uploader_bucket = nil
|
7
|
+
|
8
|
+
mattr_accessor :s3_uploader_folder
|
9
|
+
@@s3_uploader_folder = 'uploader'
|
10
|
+
|
11
|
+
mattr_accessor :s3_access_key_id
|
12
|
+
@@s3_access_key_id = nil
|
13
|
+
|
14
|
+
mattr_accessor :s3_secret_access_key
|
15
|
+
@@s3_secret_access_key = nil
|
16
|
+
|
17
|
+
def self.setup
|
18
|
+
yield self
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'aws/s3'
|
2
|
+
|
3
|
+
namespace :s3 do
|
4
|
+
|
5
|
+
desc "Store uploader on s3"
|
6
|
+
task :store_uploader => :environment do
|
7
|
+
av = ActionView::Base.new(Rails.root.join('app', 'views'))
|
8
|
+
uploader_html = av.render 'uploader/uploader'
|
9
|
+
ensure_s3_connection!
|
10
|
+
AWS::S3::S3Object.store(
|
11
|
+
File.join(S3DirectRails.s3_uploader_folder,'uploader.html'),
|
12
|
+
uploader_html,
|
13
|
+
S3DirectRails.s3_uploader_bucket,
|
14
|
+
:access => :public_read )
|
15
|
+
|
16
|
+
['jquery.fileupload.js', 'jquery.iframe-transport.js', 'uploader.js'].each do |js_file|
|
17
|
+
AWS::S3::S3Object.store(
|
18
|
+
File.join(S3DirectRails.s3_uploader_folder,js_file),
|
19
|
+
open(File.join(Rails.root,'app','assets','javascripts','uploader',js_file)),
|
20
|
+
S3DirectRails.s3_uploader_bucket,
|
21
|
+
:access => :public_read )
|
22
|
+
end
|
23
|
+
puts 'Uploader deployed.'
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def ensure_s3_connection!
|
29
|
+
return if @connected
|
30
|
+
AWS::S3::Base.establish_connection!(
|
31
|
+
:access_key_id => S3DirectRails.s3_access_key_id,
|
32
|
+
:secret_access_key => S3DirectRails.s3_secret_access_key
|
33
|
+
)
|
34
|
+
@connected = true
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/s3_direct_rails/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 's3_direct_rails'
|
6
|
+
s.version = S3DirectRails::VERSION
|
7
|
+
s.date = '2012-05-09'
|
8
|
+
s.summary = 'S3 Direct Rails'
|
9
|
+
s.description = 'Upload files directly to S3 using Rails, Carrierwave and jQuery Fileupload'
|
10
|
+
s.authors = ['Cory Kaufman-Schofied']
|
11
|
+
s.email = 'cory@corykaufman.com'
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.homepage = 'https://github.com/allspiritseve/s3_direct_rails'
|
14
|
+
s.add_dependency 'aws-s3'
|
15
|
+
end
|