flash_s3_rails 0.0.1.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +156 -0
- data/README.md +92 -0
- data/Rakefile +46 -0
- data/app/assets/images/vendor/flash_s3/start_button.png +0 -0
- data/app/assets/images/vendor/flash_s3/swfupload.swf +0 -0
- data/app/assets/images/vendor/flash_s3/upload_button.png +0 -0
- data/app/assets/javascripts/jquery.flashS3.js +128 -0
- data/app/assets/javascripts/vendor/flash_s3/swfupload.fileprogress.js +151 -0
- data/app/assets/javascripts/vendor/flash_s3/swfupload.handlers.js +199 -0
- data/app/assets/javascripts/vendor/flash_s3/swfupload.js +980 -0
- data/app/assets/javascripts/vendor/flash_s3/swfupload.queue.js +98 -0
- data/app/assets/javascripts/vendor/flash_s3/swfupload.swfobject.js +111 -0
- data/app/assets/stylesheets/jquery.flashS3.css +53 -0
- data/app/helpers/flash_s3_helper.rb +45 -0
- data/app/views/flash_s3/_flash_s3_uploader.html.erb +5 -0
- data/app/views/layouts/_flash_s3.html.erb +6 -0
- data/config/routes.rb +5 -0
- data/flash_s3.gemspec +34 -0
- data/lib/flash_s3.rb +66 -0
- data/lib/flash_s3/attachment.rb +28 -0
- data/lib/flash_s3/attachment_definition.rb +39 -0
- data/lib/flash_s3/engine.rb +13 -0
- data/lib/flash_s3/s3_post_params.rb +80 -0
- data/lib/flash_s3/version.rb +3 -0
- data/spec/models/upload_spec.rb +72 -0
- data/spec/rails_spec_helper.rb +32 -0
- data/spec/requests/new_upload_spec.rb +12 -0
- data/spec/spec_helper.rb +18 -0
- metadata +204 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
module FlashS3
|
2
|
+
class AttachmentDefinition
|
3
|
+
REQUIRED_FIELDS = %w{bucket s3_access_key_id s3_secret_access_key}
|
4
|
+
OPTIONAL_FIELDS = {
|
5
|
+
's3_bucket_acl' => 'public-read',
|
6
|
+
's3_key_guid' => lambda { SecureRandom.hex },
|
7
|
+
's3_key_path' => ''
|
8
|
+
}
|
9
|
+
|
10
|
+
attr_reader :name
|
11
|
+
|
12
|
+
def initialize(name)
|
13
|
+
@name = name
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
(REQUIRED_FIELDS + OPTIONAL_FIELDS.keys).none? {|field| send(field).nil? }
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure(&blk)
|
21
|
+
instance_eval(&blk)
|
22
|
+
end
|
23
|
+
|
24
|
+
def optional_field_default_value(field)
|
25
|
+
if field_val = OPTIONAL_FIELDS[field]
|
26
|
+
field_val.respond_to?(:call) ? field_val.call : field_val
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
(REQUIRED_FIELDS + OPTIONAL_FIELDS.keys).each do |field|
|
31
|
+
eval <<-EVAL
|
32
|
+
def #{field}(*args)
|
33
|
+
@#{field} = args.first if args.first
|
34
|
+
@#{field} || FlashS3.configuration.#{field}#{" || optional_field_default_value('#{field}')" if OPTIONAL_FIELDS.keys.include?(field)}
|
35
|
+
end
|
36
|
+
EVAL
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module FlashS3
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
config.flash_s3 = ActiveSupport::OrderedOptions.new
|
6
|
+
|
7
|
+
initializer 'flash_s3.insert_into_active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
::ActiveRecord::Base.send(:include, Rails::ActiveRecord)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module FlashS3
|
2
|
+
class S3PostParams
|
3
|
+
def initialize(record, name)
|
4
|
+
@attachment_definition = record.send(name).attachment_definition
|
5
|
+
end
|
6
|
+
|
7
|
+
def params
|
8
|
+
{
|
9
|
+
"key" => s3_key,
|
10
|
+
"AWSAccessKeyId" => "#{s3_access_key_id}",
|
11
|
+
"acl" => "#{s3_bucket_acl}",
|
12
|
+
"policy" => "#{s3_policy}",
|
13
|
+
"signature" => "#{s3_signature}",
|
14
|
+
"success_action_status" => "201"
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def s3_policy
|
19
|
+
@policy ||= Base64.encode64(
|
20
|
+
%Q%{'expiration': '#{expiration_date}',
|
21
|
+
'conditions': [
|
22
|
+
{'bucket': '#{s3_bucket}'},
|
23
|
+
['starts-with', '$key', "#{ s3_key_path.present? ? s3_key_path : ''}"],
|
24
|
+
{'acl': '#{s3_bucket_acl}'},
|
25
|
+
{'success_action_status': '201'},
|
26
|
+
['content-length-range', 0, #{max_file_size}],
|
27
|
+
['starts-with', '$Filename', '']
|
28
|
+
]
|
29
|
+
}%
|
30
|
+
).gsub(/\n|\r/, '')
|
31
|
+
end
|
32
|
+
|
33
|
+
def s3_signature
|
34
|
+
Base64.encode64(
|
35
|
+
OpenSSL::HMAC.digest(
|
36
|
+
OpenSSL::Digest::Digest.new('sha1'),
|
37
|
+
s3_secret_access_key, s3_policy)
|
38
|
+
).gsub("\n","")
|
39
|
+
end
|
40
|
+
|
41
|
+
def s3_bucket
|
42
|
+
@attachment_definition.bucket
|
43
|
+
end
|
44
|
+
|
45
|
+
def s3_access_key_id
|
46
|
+
@attachment_definition.s3_access_key_id
|
47
|
+
end
|
48
|
+
|
49
|
+
def s3_secret_access_key
|
50
|
+
@attachment_definition.s3_secret_access_key
|
51
|
+
end
|
52
|
+
|
53
|
+
def expiration_date
|
54
|
+
1.hour.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
|
55
|
+
end
|
56
|
+
|
57
|
+
def max_file_size
|
58
|
+
1000.megabytes
|
59
|
+
end
|
60
|
+
|
61
|
+
def s3_bucket_acl
|
62
|
+
@attachment_definition.s3_bucket_acl
|
63
|
+
end
|
64
|
+
|
65
|
+
def s3_key_guid
|
66
|
+
@attachment_definition.s3_key_guid
|
67
|
+
end
|
68
|
+
|
69
|
+
def s3_key_path
|
70
|
+
@attachment_definition.s3_key_path
|
71
|
+
end
|
72
|
+
|
73
|
+
def s3_key
|
74
|
+
key = "${filename}"
|
75
|
+
key = "#{s3_key_guid}_" + key if s3_key_guid.present?
|
76
|
+
key = "#{s3_key_path}/" + key if s3_key_path.present?
|
77
|
+
key
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rails_spec_helper'
|
3
|
+
|
4
|
+
describe Upload do
|
5
|
+
context 'when declaring an s3_upload without an environmental config', :without_env_config => true do
|
6
|
+
let(:upload) { Upload.new }
|
7
|
+
|
8
|
+
it 'should respond to upload=' do
|
9
|
+
upload.upload = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should raise argument error with invalid keys' do
|
13
|
+
expect { upload.upload = {:junk => 'foo'} }.to raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'the upload should be invalid' do
|
17
|
+
upload.upload.should_not be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'and the bucket is not defined inline' do
|
21
|
+
it 'should not be valid if bucket is not configured globally' do
|
22
|
+
upload.upload.should_not be_valid
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should be valid if the configuration is defined with the module' do
|
26
|
+
FlashS3.configure do |config|
|
27
|
+
config.bucket = "foo"
|
28
|
+
config.s3_access_key_id = "bas"
|
29
|
+
config.s3_secret_access_key = "fubar"
|
30
|
+
end
|
31
|
+
|
32
|
+
upload.upload = {:s3_key => 'foo/bar.png'}
|
33
|
+
|
34
|
+
upload.upload.should be_valid
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'and the bucket is defined inline' do
|
39
|
+
it 'the upload should be valid' do
|
40
|
+
upload.upload_with_block = {:s3_key => 'foo/bar.png'}
|
41
|
+
upload.upload_with_block.should be_valid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with a valid s3_key' do
|
46
|
+
before do
|
47
|
+
upload.upload = {:s3_key => 'foo/bar.png'}
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should assisn the s3_key' do
|
51
|
+
upload.upload.s3_key.should == 'foo/bar.png'
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should save the s3_key if it has changed' do
|
55
|
+
upload.save
|
56
|
+
upload.upload_s3_key.should == 'foo/bar.png'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when declaring an s3_upload with an environmental config' do
|
62
|
+
let(:upload) { Upload.new }
|
63
|
+
|
64
|
+
|
65
|
+
it 'should be valid if the configuration is defined from the rails environemnt configs' do
|
66
|
+
upload.upload = {:s3_key => 'foo/bar.png'}
|
67
|
+
|
68
|
+
upload.upload.should be_valid
|
69
|
+
upload.upload.bucket.should == 'bas'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
|
3
|
+
require 'flash_s3_test/config/environment'
|
4
|
+
require 'capybara/dsl'
|
5
|
+
require 'database_cleaner'
|
6
|
+
require 'rspec/rails'
|
7
|
+
|
8
|
+
include Capybara::DSL
|
9
|
+
|
10
|
+
Capybara.app = Rack::Builder.app do
|
11
|
+
run FlashS3Test::Application
|
12
|
+
end
|
13
|
+
|
14
|
+
Capybara.server do |app, port|
|
15
|
+
require 'rack/handler/mongrel'
|
16
|
+
Rack::Handler::Mongrel.run(app, :Port => port)
|
17
|
+
end
|
18
|
+
|
19
|
+
Capybara.default_driver = :selenium
|
20
|
+
|
21
|
+
DatabaseCleaner.strategy = :truncation
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.before(:each) do
|
25
|
+
DatabaseCleaner.start
|
26
|
+
end
|
27
|
+
|
28
|
+
config.after(:each) do
|
29
|
+
DatabaseCleaner.clean
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rails_spec_helper'
|
3
|
+
|
4
|
+
describe "When uploading a file to s3" do
|
5
|
+
it "should boot the app" do
|
6
|
+
visit '/'
|
7
|
+
page.should have_content("Flash S3")
|
8
|
+
page.should have_selector("#upload_upload.flash_s3-wrapper")
|
9
|
+
ugh = find('#SWFUpload_0')
|
10
|
+
# ugh.native.send_keys(:enter)
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'flash_s3'
|
3
|
+
|
4
|
+
include FlashS3
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.before(:all) do
|
8
|
+
@original_config = ::FlashS3.configuration.dup
|
9
|
+
end
|
10
|
+
|
11
|
+
config.after(:each) do
|
12
|
+
::FlashS3.send(:configuration=, @original_config)
|
13
|
+
end
|
14
|
+
|
15
|
+
config.before(:each) do
|
16
|
+
::FlashS3.configuration.clear if example.metadata[:without_env_config] == true
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flash_s3_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.beta1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sam Woodard
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70298583715520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70298583715520
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: thin
|
27
|
+
requirement: &70298583714060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70298583714060
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: capybara
|
38
|
+
requirement: &70298583711100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70298583711100
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mongrel
|
49
|
+
requirement: &70298583709500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.2.0.pre2
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70298583709500
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: database_cleaner
|
60
|
+
requirement: &70298583707740 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70298583707740
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: &70298583706400 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70298583706400
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rails
|
82
|
+
requirement: &70298583705180 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70298583705180
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: sqlite3
|
93
|
+
requirement: &70298583703900 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70298583703900
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: jquery-rails
|
104
|
+
requirement: &70298583702980 !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: *70298583702980
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: activesupport
|
115
|
+
requirement: &70298583699820 !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
type: :runtime
|
122
|
+
prerelease: false
|
123
|
+
version_requirements: *70298583699820
|
124
|
+
- !ruby/object:Gem::Dependency
|
125
|
+
name: jquery-ui-rails
|
126
|
+
requirement: &70298583699120 !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: *70298583699120
|
135
|
+
description: Direct multi-file uploader to s3
|
136
|
+
email:
|
137
|
+
- sam.h.woodard@gmail.com
|
138
|
+
executables: []
|
139
|
+
extensions: []
|
140
|
+
extra_rdoc_files: []
|
141
|
+
files:
|
142
|
+
- .gitignore
|
143
|
+
- .rspec
|
144
|
+
- Gemfile
|
145
|
+
- Gemfile.lock
|
146
|
+
- README.md
|
147
|
+
- Rakefile
|
148
|
+
- app/assets/images/vendor/flash_s3/start_button.png
|
149
|
+
- app/assets/images/vendor/flash_s3/swfupload.swf
|
150
|
+
- app/assets/images/vendor/flash_s3/upload_button.png
|
151
|
+
- app/assets/javascripts/jquery.flashS3.js
|
152
|
+
- app/assets/javascripts/vendor/flash_s3/swfupload.fileprogress.js
|
153
|
+
- app/assets/javascripts/vendor/flash_s3/swfupload.handlers.js
|
154
|
+
- app/assets/javascripts/vendor/flash_s3/swfupload.js
|
155
|
+
- app/assets/javascripts/vendor/flash_s3/swfupload.queue.js
|
156
|
+
- app/assets/javascripts/vendor/flash_s3/swfupload.swfobject.js
|
157
|
+
- app/assets/stylesheets/jquery.flashS3.css
|
158
|
+
- app/helpers/flash_s3_helper.rb
|
159
|
+
- app/views/flash_s3/_flash_s3_uploader.html.erb
|
160
|
+
- app/views/layouts/_flash_s3.html.erb
|
161
|
+
- config/routes.rb
|
162
|
+
- flash_s3.gemspec
|
163
|
+
- lib/flash_s3.rb
|
164
|
+
- lib/flash_s3/attachment.rb
|
165
|
+
- lib/flash_s3/attachment_definition.rb
|
166
|
+
- lib/flash_s3/engine.rb
|
167
|
+
- lib/flash_s3/s3_post_params.rb
|
168
|
+
- lib/flash_s3/version.rb
|
169
|
+
- spec/models/upload_spec.rb
|
170
|
+
- spec/rails_spec_helper.rb
|
171
|
+
- spec/requests/new_upload_spec.rb
|
172
|
+
- spec/spec_helper.rb
|
173
|
+
homepage: ''
|
174
|
+
licenses: []
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
require_paths:
|
178
|
+
- lib
|
179
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
180
|
+
none: false
|
181
|
+
requirements:
|
182
|
+
- - ! '>='
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
segments:
|
186
|
+
- 0
|
187
|
+
hash: 1035574372849630356
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
+
none: false
|
190
|
+
requirements:
|
191
|
+
- - ! '>'
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: 1.3.1
|
194
|
+
requirements: []
|
195
|
+
rubyforge_project: flash_s3
|
196
|
+
rubygems_version: 1.8.15
|
197
|
+
signing_key:
|
198
|
+
specification_version: 3
|
199
|
+
summary: Direct multi-file uploader to s3
|
200
|
+
test_files:
|
201
|
+
- spec/models/upload_spec.rb
|
202
|
+
- spec/rails_spec_helper.rb
|
203
|
+
- spec/requests/new_upload_spec.rb
|
204
|
+
- spec/spec_helper.rb
|