s3_upload_form 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/LICENSE +20 -0
- data/README.markdown +36 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/generators/s3_upload_form/s3_upload_form_generator.rb +17 -0
- data/generators/s3_upload_form/templates/initializers/s3_upload_form.rb +5 -0
- data/lib/s3_upload_form.rb +8 -0
- data/lib/s3_upload_form/configuration.rb +18 -0
- data/lib/s3_upload_form/helpers.rb +53 -0
- data/test/test_helper.rb +10 -0
- metadata +67 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Matthew Williams, Jakub Kuźma
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# s3_upload_form
|
2
|
+
|
3
|
+
### Usage
|
4
|
+
<% form_tag s3_bucket_url, :multipart => true do -%>
|
5
|
+
<%= s3_signature_tag :key => "uploads",
|
6
|
+
:redirect => image_processing_url,
|
7
|
+
:acl => "public-read",
|
8
|
+
:max_filesize => 0..5.megabytes,
|
9
|
+
:submit => submit_tag("Upload!") %>
|
10
|
+
<%= label_tag :file, "File" %>
|
11
|
+
<br/>
|
12
|
+
<%= file_field_tag :file %>
|
13
|
+
<br/>
|
14
|
+
<%= submit_tag "Upload" %>
|
15
|
+
<% end -%>
|
16
|
+
|
17
|
+
Remember to turn off the request forgery protection in the controller:
|
18
|
+
|
19
|
+
class UploadsController < ApplicationController
|
20
|
+
self.allow_forgery_protection = false
|
21
|
+
|
22
|
+
def new
|
23
|
+
# ...
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
To configure s3_upload_form use s3_upload_form generator...
|
30
|
+
./script/generate s3_upload_form
|
31
|
+
|
32
|
+
... and edit the generated initializer file s3_upload_form.rb.
|
33
|
+
|
34
|
+
### Jakub Kuźma, 2009
|
35
|
+
|
36
|
+
The gem is based on D2S3 plugin by Matthew Williams.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#coding: UTF-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "s3_upload_form"
|
10
|
+
gem.summary = %Q{S3 file upload form}
|
11
|
+
gem.email = "qoobaa@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/qoobaa/s3_upload_form"
|
13
|
+
gem.authors = ["Jakub Kuźma"]
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/*_test.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/*_test.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION.yml')
|
47
|
+
config = YAML.load(File.read('VERSION.yml'))
|
48
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "d2s3 #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
|
3
|
+
class S3UploadFormGenerator < Rails::Generator::Base
|
4
|
+
def manifest
|
5
|
+
record do |m|
|
6
|
+
m.directory "config"
|
7
|
+
m.directory "config/initializers"
|
8
|
+
m.file "initializers/s3_upload_form.rb", "config/initializers/s3_upload_form.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def banner
|
15
|
+
"Usage: #{$0} s3_upload_form"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
module S3UploadForm
|
4
|
+
class Configuration
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
ATTRIBUTES = [:access_key_id, :secret_access_key, :bucket]
|
8
|
+
|
9
|
+
attr_accessor *ATTRIBUTES
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configuration
|
13
|
+
if block_given?
|
14
|
+
yield Configuration.instance
|
15
|
+
end
|
16
|
+
Configuration.instance
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module S3UploadForm
|
2
|
+
module Helpers
|
3
|
+
def s3_bucket_url(protocol = "http")
|
4
|
+
bucket = S3UploadForm.configuration.bucket
|
5
|
+
|
6
|
+
raise "Please configure S3UploadForm before use" if bucket.blank?
|
7
|
+
|
8
|
+
"#{protocol}://#{bucket}.s3.amazonaws.com/"
|
9
|
+
end
|
10
|
+
|
11
|
+
def s3_signature_tag(options = {})
|
12
|
+
bucket = S3UploadForm.configuration.bucket
|
13
|
+
access_key_id = S3UploadForm.configuration.access_key_id
|
14
|
+
secret_access_key = S3UploadForm.configuration.secret_access_key
|
15
|
+
|
16
|
+
raise "Please configure S3UploadForm before use" if bucket.blank? or access_key_id.blank? or secret_access_key.blank?
|
17
|
+
|
18
|
+
key = options.delete(:key) || ""
|
19
|
+
redirect = options.delete(:redirect) || "/"
|
20
|
+
acl = options.delete(:acl) || "public-read"
|
21
|
+
expiration_date = (options.delete(:expiration_date) || 10.hours).from_now.strftime("%Y-%m-%dT%H:%M:%S.000Z")
|
22
|
+
filesize = options.delete(:filesize) || (1..1.megabyte)
|
23
|
+
filesize_range = filesize.kind_of?(Fixnum) ? 1..filesize : filesize
|
24
|
+
|
25
|
+
policy_document = <<-eos
|
26
|
+
{
|
27
|
+
"expiration": "#{expiration_date}",
|
28
|
+
"conditions":
|
29
|
+
[
|
30
|
+
{"bucket": "#{bucket}"},
|
31
|
+
["starts-with", "$key", "#{key}"],
|
32
|
+
{"acl": "#{acl}"},
|
33
|
+
{"success_action_redirect": "#{redirect}"},
|
34
|
+
["content-length-range", #{filesize_range.first}, #{filesize_range.last}]
|
35
|
+
]
|
36
|
+
}
|
37
|
+
eos
|
38
|
+
|
39
|
+
policy = Base64.encode64(policy_document).gsub("\n","")
|
40
|
+
signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha1"), secret_access_key, policy)).gsub("\n","")
|
41
|
+
|
42
|
+
content_tag :div, :style => "margin: 0pt; padding: 0pt; display: inline;" do
|
43
|
+
content = ""
|
44
|
+
content << hidden_field_tag("key", "#{key}/${filename}")
|
45
|
+
content << hidden_field_tag("AWSAccessKeyId", access_key_id)
|
46
|
+
content << hidden_field_tag("acl", acl)
|
47
|
+
content << hidden_field_tag("success_action_redirect", redirect)
|
48
|
+
content << hidden_field_tag("policy", policy)
|
49
|
+
content << hidden_field_tag("signature", signature)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3_upload_form
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Jakub Ku\xC5\xBAma"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-20 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: qoobaa@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.markdown
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- generators/s3_upload_form/s3_upload_form_generator.rb
|
33
|
+
- generators/s3_upload_form/templates/initializers/s3_upload_form.rb
|
34
|
+
- lib/s3_upload_form.rb
|
35
|
+
- lib/s3_upload_form/configuration.rb
|
36
|
+
- lib/s3_upload_form/helpers.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/qoobaa/s3_upload_form
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: S3 file upload form
|
66
|
+
test_files:
|
67
|
+
- test/test_helper.rb
|