wheels 0.0.29 → 0.0.30

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.29
1
+ 0.0.30
@@ -12,5 +12,6 @@ class AttachmentsController < InheritedResources::Base
12
12
  def create
13
13
  create! {collection_url}
14
14
  end
15
+
15
16
  end
16
17
 
@@ -48,6 +48,7 @@ class PagesController < InheritedResources::Base
48
48
  edit! do |format|
49
49
  set_children
50
50
  @attachment = Attachment.new(:page=>@page)
51
+ @s3provider = S3Provider.new(:key=>"attachments/#{current_user.id}")
51
52
  end
52
53
  end
53
54
 
@@ -9,5 +9,186 @@ module PagesHelper
9
9
  def to_html_options(pages)
10
10
  pages.map{|p| [p.title, p.id]}
11
11
  end
12
+
13
+
14
+
15
+ ######################################################
16
+ ##
17
+ ##
18
+ ## Begin of uploadify_s3
19
+ ##
20
+ ##
21
+ ######################################################
22
+
23
+ def uploadify_s3(options = {})
24
+ # stylesheet_link_tag('uploadify/uploadify') <<
25
+ # javascript_include_tag('uploadify/jquery.uploadify.v2.1.0.min') <<
26
+ # javascript_include_tag('uploadify/swfobject') <<
27
+ javascript_uploadify_s3_tag(options)
28
+ end
29
+
30
+ protected
31
+
32
+ def javascript_uploadify_s3_tag(options = {})
33
+ options = default_options.merge(options)
34
+ javascript_tag( %(
35
+ $(document).ready(function() {
36
+ $("#{options[:file_input_selector]}").uploadify({
37
+ 'fileDataName' : 'file',
38
+ 'uploader' : '/uploadify.swf',
39
+ 'script' : '#{bucket_url}',
40
+ 'cancelImg' : '/images/uploadify/cancel.png',
41
+ 'folder' : 'attachments',
42
+ 'auto' : true,
43
+ 'multi' : true,
44
+ 'buttonText' : 'Add File',
45
+ 'sizeLimit' : '#{max_filesize}',
46
+ 'fileDesc' : '#{options[:file_desc]}',
47
+ 'fileExt' : '#{options[:file_ext]}',
48
+ 'onSelect' : function(event, queueID, fileObj) {
49
+ if (fileObj.size >= "#{max_filesize}") {
50
+ $("#{options[:file_input_selector]}").uploadifyCancel(queueID);
51
+ alert('Sorry the max file size is #{((max_filesize/1024)/1024)} MB');
52
+ return false;
53
+ }
54
+
55
+ $('div.button_group').hide();
56
+ return true;
57
+ },
58
+ 'onComplete' : function(event, queueID, fileObj, response) {
59
+ $('div.button_group').show();
60
+ fileInfo = {
61
+ 'name' : fileObj.name,
62
+ 'size' : fileObj.size,
63
+ 'type' : fileObj.type,
64
+ 'url' : '#{bucket_url}#{upload_path}/' + fileObj.name + '#{rangen}'
65
+ };
66
+ var onsucc = (#{options[:on_success]});
67
+ onsucc(fileInfo);
68
+ $('#{options[:file_input_selector]}').hide();
69
+ return true;
70
+ },
71
+ 'onError' : function (a, b, c, d) {
72
+ if (d.info == 201) {
73
+ fileInfo = {
74
+ 'name' : c.name,
75
+ 'size' : c.size,
76
+ 'type' : c.type,
77
+ 'url' : '#{bucket_url}#{upload_path}/' + c.name + '#{rangen}'
78
+ };
79
+ var onsucc = (#{options[:on_success]});
80
+ onsucc(fileInfo);
81
+ $('#{options[:file_input_selector]}').hide();
82
+ } else {
83
+ var onerror = (#{options[:on_error]});
84
+ if (onerror) {
85
+ onerror(d.type, d.text);
86
+ $('#file_uploaderQueue').hide();
87
+ return false;
88
+ }
89
+ }
90
+
91
+ return true;
92
+ },
93
+ 'scriptData' : {
94
+ 'AWSAccessKeyId': '#{aws_access_key}',
95
+ 'key': '#{key}',
96
+ 'acl': '#{acl}',
97
+ 'policy': '#{s3_policy}',
98
+ 'success_action_status': '201',
99
+ 'signature': encodeURIComponent(encodeURIComponent('#{s3_signature}')),
100
+ 'Content-Type': ''
101
+ }
102
+ });
103
+ });
104
+ ))
105
+ end
106
+
107
+ def bucket_url
108
+ "http://#{bucket}.s3.amazonaws.com/"
109
+ end
110
+
111
+ def key
112
+ "#{upload_path}/${filename}#{rangen}"
113
+ end
114
+
115
+ def rangen
116
+ @rangen ||= '' # rand(36 ** 8).to_s(36)
117
+ end
118
+
119
+ def policy_doc
120
+ @policy ||= "{'expiration': '#{expiration_date}',
121
+ 'conditions': [{'bucket': '#{bucket}'},
122
+ ['starts-with', '$key', '#{upload_path}'],
123
+ {'acl': '#{acl.to_s}'},
124
+ ['content-length-range', 0, #{max_filesize}],
125
+ {'success_action_status': '201'},
126
+ ['starts-with','$folder',''],
127
+ ['starts-with','$Filename',''],
128
+ ['starts-with','$fileext',''],
129
+ ]
130
+ }"
131
+ end
132
+
133
+ def s3_policy
134
+ Base64.encode64(policy_doc).gsub(/\n|\r/, '')
135
+ end
136
+
137
+ def s3_signature
138
+ raise "Secret key should not be nil." if self.aws_secret_key.nil?
139
+ b64_hmac_sha1(aws_secret_key, s3_policy)
140
+ end
141
+
142
+ def load_s3config
143
+ a = YAML.load_file("#{Rails.root}/config/amazon_s3.yml")
144
+ puts 'loading config from yaml file.'
145
+ @s3config = a['development']
146
+ puts @s3config.inspect
147
+ end
148
+
149
+ def s3config(name)
150
+ load_s3config unless @s3config
151
+ @s3config[name]
152
+ end
153
+
154
+ def aws_access_key
155
+ s3config 'access_key_id'
156
+ end
157
+
158
+ def aws_secret_key
159
+ s3config('secret_access_key')
160
+ end
161
+
162
+ def bucket
163
+ s3config('bucket')
164
+ end
165
+
166
+ def acl
167
+ s3config('default_acl') || "public-read"
168
+ end
169
+
170
+ def upload_path
171
+ s3config('upload_path') || 'attachments'
172
+ end
173
+
174
+ def max_filesize
175
+ s3config('max_file_size') || 1000.megabyte
176
+ end
177
+
178
+ def expiration_date
179
+ 10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
180
+ end
181
+
182
+ def default_options
183
+ {
184
+ :button_text => 'Add File',
185
+ :button_img => '/images/uploadify/upload.png',
186
+ :height => '20',
187
+ :width => '30',
188
+ :file_ext => '*.*',
189
+ :file_input_selector => '#file_upload',
190
+ :file_desc => 'Please choose your file'
191
+ }
192
+ end
12
193
  end
13
194
 
@@ -0,0 +1,84 @@
1
+ class S3Provider
2
+ attr_accessor :bucket, :access_key_id, :secret_access_key, :key, :content_type,
3
+ :acl, :expiration_date, :max_filesize, :policy, :signature, :form_fields
4
+
5
+ def initialize(options = {})
6
+ filename = "#{Rails.root}/config/amazon_s3.yml"
7
+ config = YAML.load_file(filename)
8
+
9
+ self.bucket = config["development"]['bucket']
10
+ self.access_key_id = config["development"]['access_key_id']
11
+ self.secret_access_key = config["development"]['secret_access_key']
12
+ self.key = options[:key] || ''
13
+ self.content_type = options[:content_type] || ''
14
+ self.acl = options[:acl] || 'public-read'
15
+ self.expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
16
+ self.max_filesize = options[:max_filesize] || 500.megabyte
17
+
18
+ self.policy = Base64.encode64(
19
+ "{'expiration': '#{self.expiration_date}',
20
+ 'conditions': [
21
+ {'bucket': '#{self.bucket}'},
22
+ ['starts-with', '$key', '#{self.key}'],
23
+ {'acl': '#{self.acl}'},
24
+ {'success_action_status': '201'},
25
+ ['content-length-range', 0, #{self.max_filesize}],
26
+ ['starts-with', '$Content-Type', '']
27
+ ]
28
+ }").gsub(/\n|\r/, '')
29
+
30
+ self.signature = Base64.encode64(
31
+ OpenSSL::HMAC.digest(
32
+ OpenSSL::Digest::Digest.new('sha1'),
33
+ self.secret_access_key, self.policy)).gsub("\n","")
34
+
35
+ self.form_fields = %(
36
+ <form action="#{form_action}" method="post" enctype="multipart/form-data" id="upload-form">
37
+ <input type="hidden" name="key" value="#{self.key}/${filename}">
38
+ <input type="hidden" name="AWSAccessKeyId" value="#{self.access_key_id}">
39
+ <input type="hidden" name="acl" value="#{self.policy}">
40
+ <input type="hidden" name="success_action_redirect" value="http://localhost/">
41
+ <input type="hidden" name="policy" value="#{self.policy}">
42
+ <input type="hidden" name="signature" value="#{self.signature}">
43
+ <input type="hidden" name="Content-Type" value="image/jpeg">
44
+ <input name="file" type="file">
45
+ <input type="submit" value="Upload File to S3">
46
+ </form>
47
+ )
48
+ end
49
+ def form_action
50
+ "https://#{self.bucket}.s3.amazonaws.com/"
51
+ end
52
+ def script_data
53
+ %(
54
+ "AWSAccessKeyId": #{s self.access_key_id},
55
+ "key": #{s self.key},
56
+ "acl": #{s self.acl},
57
+ "policy": #{url_encoded(%("#{self.policy}"), 2)},
58
+ "signature": #{s self.signature}
59
+ )
60
+ end
61
+
62
+ def foolbar
63
+ %(<input type="hidden" name="key" value="#{self.key}/${filename}">
64
+ <input type="hidden" name="AWSAccessKeyId" value="#{self.access_key_id}">
65
+ <input type="hidden" name="acl" value="#{self.acl}">
66
+ <input type="hidden" name="policy" value="#{self.policy}">
67
+ <input type="hidden" name="signature" value="#{self.signature}">
68
+ <input type="hidden" name="success_action_status" value="201">
69
+ <input type="hidden" name="Content-Type" value="#{self.content_type}">
70
+ <input name="file" type="file" id="file_input" />
71
+ <input name="submit" value="Upload" type="submit" />)
72
+ end
73
+ private
74
+ def url_encoded(str, num=1)
75
+ str = url_encoded(str) if num==2
76
+ "encodeURIComponent(#{str})"
77
+ end
78
+ def s(str)
79
+ "\"#{str}\""
80
+ end
81
+
82
+
83
+ end
84
+
@@ -5,31 +5,24 @@
5
5
  - resource.errors.full_messages.each do |msg|
6
6
  %li= msg
7
7
 
8
- = form_for [@page, @attachment], :html=>{:multipart=>true} do |f|
8
+ != S3Provider.new(:key=>"attachments/#{current_user.id}").form_fields
9
+
10
+ = form_for [@page, Attachment.new(:page=>@page)], :remote=>true, :html=>{:multipart=>true, :id=>"upload_form"} do |f|
9
11
  = f.file_field :file, :id=>"file_input"
12
+ = f.hidden_field :file_file_name, :id=>"file_file_name_input"
13
+ = f.hidden_field :file_file_size, :id=>"file_file_size_input"
14
+ = f.hidden_field :file_content_type, :id=>"file_content_type_input"
15
+ = f.hidden_field :file_updated_at, :id=>"file_updated_at_input"
10
16
  = f.submit "Upload File", :id=>"upload_button"
11
17
 
12
18
  = content_for :head do
13
19
  = javascript_include_tag *%w(swfobject jquery.uploadify)
14
- :javascript
15
- $(function() {
20
+ :javascript
21
+ $(function(){
16
22
  $('#upload_button').hide();
17
- $('#file_input').uploadify ({
18
- script : '#{page_attachments_path(@page, :container=>'attachments_list')}',
19
- uploader : '/uploadify.swf',
20
- fileDataName : 'attachment[file]',
21
- multi : true,
22
- auto : true,
23
- buttonText : 'New File',
24
- cancelImg : '/images/uploadify/cancel.png',
25
- onComplete : function(a, b, c, response){ eval(response); },
26
- scriptData : {
27
- '_http_accept': 'application/javascript',
28
- '_method': 'post',
29
- '#{session_key_name}' : encodeURIComponent('#{u cookies[session_key_name]}'),
30
- 'authenticity_token': encodeURIComponent('#{raw form_authenticity_token}'),
31
- 'session_encoded': '#{make_session_string}',
32
- }
33
23
  });
34
- });
24
+
25
+ = uploadify_s3(:file_input_selector => '#file_input', :button_text => 'Add File', |
26
+ :on_success => %(function(f) {submitFileForm(f.name, f.size, f.type)}), |
27
+ :on_error => %(function(type, text) {alert("Problem during file upload type: " + type + " text: " + text);}))
35
28
 
@@ -0,0 +1,12 @@
1
+ $(function(){
2
+ $('#upload_button').hide();
3
+ });
4
+
5
+ function submitFileForm(fileName, fileSize, updatedAt, contentType) {
6
+ $('#file_file_name_input').val(fileName);
7
+ $('#file_file_size_input').val(fileSize);
8
+ $('#file_update_at').val(updatedAt);
9
+ $('#file_content_type').val(contentType);
10
+ $('#upload_form').submit();
11
+ }
12
+
@@ -0,0 +1,52 @@
1
+ class S3loginProvider
2
+ attr_accessor :bucket, :access_key_id, :secret_access_key, :key, :content_type,
3
+ :acl, :expiration_date, :max_filesize, :policy, :signature
4
+
5
+ def initialize(options = {})
6
+ filename = "#{Rails.root}/config/amazon_s3.yml"
7
+ config = YAML.load_file(filename)
8
+
9
+ self.bucket = config[ENV["RAILS_ENV"]]['bucket_name']
10
+ self.access_key_id = config[ENV["RAILS_ENV"]]['access_key_id']
11
+ self.secret_access_key = config[ENV["RAILS_ENV"]]['secret_access_key']
12
+
13
+ self.key = options[:key] || ''
14
+ self.content_type = options[:content_type] || ''
15
+ self.acl = options[:acl] || 'public-read'
16
+ self.expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
17
+ self.max_filesize = options[:max_filesize] || 500.megabyte
18
+
19
+ self.policy = Base64.encode64(
20
+ "{'expiration': '#{expiration_date}',
21
+ 'conditions': [
22
+ {'bucket': '#{bucket}'},
23
+ ['starts-with', '$key', '#{key}'],
24
+ {'acl': '#{acl}'},
25
+ {'success_action_status': '201'},
26
+ ['content-length-range', 0, #{max_filesize}],
27
+ ['starts-with', '$Filename', ''],
28
+ ['starts-with', '#{content_type}', '']
29
+ ]
30
+ }").gsub(/\n|\r/, '')
31
+
32
+ self.signature = Base64.encode64(
33
+ OpenSSL::HMAC.digest(
34
+ OpenSSL::Digest::Digest.new('sha1'),
35
+ self.secret_access_key, self.policy)).gsub("\n","")
36
+
37
+ self.form_fields = %(
38
+ <form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data" id="upload-form">
39
+ <input type="hidden" name="key" value="#{key}/${filename}">
40
+ <input type="hidden" name="AWSAccessKeyId" value="#{access_key_id}">
41
+ <input type="hidden" name="acl" value="#{acl}">
42
+ <input type="hidden" name="policy" value="#{policy}">
43
+ <input type="hidden" name="signature" value="#{signature}">
44
+ <input type="hidden" name="success_action_status" value="201">
45
+ <input type="hidden" name="Content-Type" value="#{content_type}">
46
+ <input name="file" type="file" id="" />
47
+ <input name="submit" value="Upload" type="submit" id="upload_button" />
48
+ </form>
49
+ )
50
+ end
51
+ end
52
+
data/lib/wheels.rb CHANGED
@@ -3,4 +3,5 @@ require 'wheels/routes.rb'
3
3
  require 'wheels/action_controller_extensions.rb'
4
4
  require 'wheels/action_view_helper_extensions.rb'
5
5
  require 'wheels/flash_session_cookie_middleware.rb'
6
+ # require 'wheels/s3login_provider.rb'
6
7
 
data/public/test.html ADDED
@@ -0,0 +1,18 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
4
+ </head>
5
+ <body>
6
+ <form action="http://s3.amazonaws.com/cagym" method="post" enctype="multipart/form-data">
7
+ <input type="text" name="key" value="testfile.txt" />
8
+ <input type="text" name="acl" value="public-read" />
9
+ <input type="text" name="content-type" value="text/plain" />
10
+ <input type="hidden" name="AWSAccessKeyId" value="AKIAIDEFW5P6AQLRXWGQ" />
11
+ <input type="hidden" name="policy" value="ewogICJleHBpcmF0aW9uIjogIjIwMTAtMTAtMTBUMTI6MDA6MDAuMDAwWiIsCiAgImNvbmRpdGlvbnMiOiBbCiAgICB7ImJ1Y2tldCI6ICJjYWd5bSIgfSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIiB9LAogICAgeyJzdWNjZXNzX2FjdGlvbl9zdGF0dXMiOiIyMDEifSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0LyJdLAogICAgWyJzdGFydHMtd2l0aCIsIiRrZXkiLCI0MFwvMjAwOTEyMjAtMTQ0MDM4XC8iXSwKICAgIFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwXQogIF0KfQo=" />
12
+ a<input type="hidden" name="signature" value="0imPd+fdigSOMClB6+0Dxo01y2I=" />
13
+ <input name="file" type="file" />
14
+ <input name="submit" value="Upload" type="submit" />
15
+ </form>
16
+ </body>
17
+ </html>
18
+
data/wheels.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wheels}
8
- s.version = "0.0.29"
8
+ s.version = "0.0.30"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyler Gannon"]
12
- s.date = %q{2010-08-19}
12
+ s.date = %q{2010-08-20}
13
13
  s.description = %q{Call rails generate wheels.}
14
14
  s.email = %q{tgannon@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -50,6 +50,7 @@ Gem::Specification.new do |s|
50
50
  "app/models/page_revision.rb",
51
51
  "app/models/profile.rb",
52
52
  "app/models/role.rb",
53
+ "app/models/s3_provider.rb",
53
54
  "app/models/tagging.rb",
54
55
  "app/models/user.rb",
55
56
  "app/views/access_control_entries/_form.html.haml",
@@ -69,6 +70,7 @@ Gem::Specification.new do |s|
69
70
  "app/views/attachments/_form.html.haml",
70
71
  "app/views/attachments/_index.html.haml",
71
72
  "app/views/attachments/_show.html.haml",
73
+ "app/views/attachments/_uploadify.html.erb",
72
74
  "app/views/attachments/create.js.haml",
73
75
  "app/views/attachments/destroy.js.haml",
74
76
  "app/views/attachments/edit.js.haml",
@@ -206,6 +208,7 @@ Gem::Specification.new do |s|
206
208
  "lib/wheels/active_record_user_extensions.rb",
207
209
  "lib/wheels/flash_session_cookie_middleware.rb",
208
210
  "lib/wheels/routes.rb",
211
+ "lib/wheels/s3login_provider.rb",
209
212
  "lib/wheels/user.rb",
210
213
  "lib/wheels/wheels_engine.rb",
211
214
  "public/404.html",
@@ -261,6 +264,7 @@ Gem::Specification.new do |s|
261
264
  "public/stylesheets/ui-lightness/images/ui-icons_ffd27a_256x240.png",
262
265
  "public/stylesheets/ui-lightness/images/ui-icons_ffffff_256x240.png",
263
266
  "public/stylesheets/ui-lightness/jquery-ui-1.8.2.custom.css",
267
+ "public/test.html",
264
268
  "wheels.gemspec"
265
269
  ]
266
270
  s.homepage = %q{http://github.com/tylergannon/wheels}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wheels
3
3
  version: !ruby/object:Gem::Version
4
- hash: 37
4
+ hash: 35
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 29
10
- version: 0.0.29
9
+ - 30
10
+ version: 0.0.30
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler Gannon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-19 00:00:00 -07:00
18
+ date: 2010-08-20 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -75,6 +75,7 @@ files:
75
75
  - app/models/page_revision.rb
76
76
  - app/models/profile.rb
77
77
  - app/models/role.rb
78
+ - app/models/s3_provider.rb
78
79
  - app/models/tagging.rb
79
80
  - app/models/user.rb
80
81
  - app/views/access_control_entries/_form.html.haml
@@ -94,6 +95,7 @@ files:
94
95
  - app/views/attachments/_form.html.haml
95
96
  - app/views/attachments/_index.html.haml
96
97
  - app/views/attachments/_show.html.haml
98
+ - app/views/attachments/_uploadify.html.erb
97
99
  - app/views/attachments/create.js.haml
98
100
  - app/views/attachments/destroy.js.haml
99
101
  - app/views/attachments/edit.js.haml
@@ -231,6 +233,7 @@ files:
231
233
  - lib/wheels/active_record_user_extensions.rb
232
234
  - lib/wheels/flash_session_cookie_middleware.rb
233
235
  - lib/wheels/routes.rb
236
+ - lib/wheels/s3login_provider.rb
234
237
  - lib/wheels/user.rb
235
238
  - lib/wheels/wheels_engine.rb
236
239
  - public/404.html
@@ -286,6 +289,7 @@ files:
286
289
  - public/stylesheets/ui-lightness/images/ui-icons_ffd27a_256x240.png
287
290
  - public/stylesheets/ui-lightness/images/ui-icons_ffffff_256x240.png
288
291
  - public/stylesheets/ui-lightness/jquery-ui-1.8.2.custom.css
292
+ - public/test.html
289
293
  - wheels.gemspec
290
294
  has_rdoc: true
291
295
  homepage: http://github.com/tylergannon/wheels