Permalink
Browse files

Merge pull request #5 from GunioRobot/clean

Hi! I cleaned up your code for you!
  • Loading branch information...
2 parents 1cfc97d + 5f75a19 commit 45a0fe7d0c0dcaa4c5d7d06e69dff30cfb225fdf @mwilliams mwilliams committed Oct 29, 2011
Showing with 50 additions and 30 deletions.
  1. +20 −0 .gitignore
  2. +19 −19 README.markdown
  3. +1 −1 lib/d2s3/patch/integer.rb
  4. +3 −3 lib/d2s3/s3_config.rb
  5. +6 −6 lib/d2s3/signature.rb
  6. +1 −1 lib/d2s3/view_helpers.rb
View
@@ -0,0 +1,20 @@
+# Compiled source #
+###################
+*.com
+*.class
+*.dll
+*.exe
+*.o
+*.so
+*.pyc
+
+# Logs and databases #
+######################
+*.log
+
+# OS generated files #
+######################
+.DS_Store*
+ehthumbs.db
+Icon?
+Thumbs.db
View
@@ -2,61 +2,61 @@
d2s3 (direct to s3) is a simple Ruby on Rails helper that generates an upload form that will take a given file and upload it directly to your S3 bucket, bypassing your server. This has various benefits, the biggest being that a large upload will not tie up your server from serving other requests. This plugin is based on the instructions from the following Amazon tutorial: [http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434](http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434/ "Browser Uploads to S3 using HTML POST Forms")
### Why?
-This was built as a solution to a problem we had where images being uploaded to be processed by [Paperclip](http://thoughtbot.com/projects/paperclip "Thoughtbot - Paperclip") were consuming [Thin](http://code.macournoyer.com/thin/ "Thin - Another Web Server") servers so they were unable to process other requests.
+This was built as a solution to a problem we had where images being uploaded to be processed by [Paperclip](http://thoughtbot.com/projects/paperclip "Thoughtbot - Paperclip") were consuming [Thin](http://code.macournoyer.com/thin/ "Thin - Another Web Server") servers so they were unable to process other requests.
#### An example workflow using d2s3
1. Upload file with the `s3_http_upload_tag` helper form tag
2. With the data returned from the GET HTTP request from Amazon, create a message using Amazon's SQS service with the appropriate information needed to later process the image with [Paperclip](http://thoughtbot.com/projects/paperclip "Thoughtbot - Paperclip")
3. Create an API end point for a background process to access that accepts information to process the image with Paperclip. In our case, it accepts the ID of a photo album and the path to the photo to be processed
4. Running a back end process that monitors the SQS queue for new images and processes them immediately
-
+
We don't have immediate processing of our images with this workflow, but it's very quick assuming how large the queue is and how many back end processes we have actually running through the queue.
### Example d2s3 usage
- <%= s3_http_upload_tag :key => 'uploads',
- :content_type => 'image/jpeg',
+ <%= s3_http_upload_tag :key => 'uploads',
+ :content_type => 'image/jpeg',
:redirect => image_processing_url,
:acl => 'public-read',
:max_filesize => 5.megabytes,
:submit_button => '<input type="submit" value="Upload" class="button" id="upload-button">',
:form => {:style => 'display: inline;'} %>
-The above helper will generate the following similar HTML form, generating all of the appropriate field keys, policy, and signature based on your Amazon Web Services YAML configuration file. The form parameter also accepts a class and id for further customization.
+The above helper will generate the following similar HTML form, generating all of the appropriate field keys, policy, and signature based on your Amazon Web Services YAML configuration file. The form parameter also accepts a class and id for further customization.
<form action="https://YOUR_S3_BUCKET.s3.amazonaws.com/" method="post" enctype="multipart/form-data" style="display: inline;">
<input type="hidden" name="key" value="uploads/${filename}">
- <input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY">
- <input type="hidden" name="acl" value="public-read">
+ <input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY">
+ <input type="hidden" name="acl" value="public-read">
<input type="hidden" name="success_action_redirect" value="/image_processing_url">
<input type="hidden" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
<input type="hidden" name="signature" value="YOUR_CALCULATED_SIGNATURE">
<input type="hidden" name="Content-Type" value="image/jpeg">
<input name="file" type="file"><input type="submit" value="Upload" class="button" id="upload-button">
</form>
-
+
### Return HTTP GET request from Amazon made to the redirect you declared
- Parameters: {"bucket"=>"BUCKET_NAME",
- "etag"=>"ETAG_HASH",
+ Parameters: {"bucket"=>"BUCKET_NAME",
+ "etag"=>"ETAG_HASH",
"action"=>"YOUR_REDIRECT_URL",
"controller"=>"CONTROLLER",
"key"=>"PATH/FILENAME.EXTENSION"}
-
+
### Options:
-* **:content_type**
+* **:content_type**
* Accepts a standard content type, otherwise it will default to binary/octet-stream
-* **:redirect**
+* **:redirect**
* Directs the form where the GET request from Amazon should be made once the HTTP POST is successful
-* **:acl**
+* **:acl**
* Accepts either 'public-read' or 'private'. If blank, it defaults to 'public-read'
-* **:expiration_date**
+* **:expiration_date**
* Accepts time in the form of "3.hours" or "25.minutes". If blank, it defaults to a 10 hour window before the policy on the upload expires
-* **:max_filesize**
+* **:max_filesize**
* Accepts a max file size in the format of "5.megabytes". If blank, it defaults to 1.megabyte
-* **:submit_button**
+* **:submit_button**
* Accepts any text to represent the submit button for the form. This allows for a very custom submit button. If blank, it defaults to `<input type="submit" value="Upload">`
-* **:form => {:id => '', :class => '', :style => ''}**
+* **:form => {:id => '', :class => '', :style => ''}**
* Accepts a hash of :class, :id, and :style to add customization to the form as a whole
-
+
### **TODO**
* Write tests (shame on me, I know... they're coming)
@@ -1,5 +1,5 @@
class Integer
- # 32-bit left shift
+ # 32-bit left shift
def js_shl(count)
v = (self << count) & 0xffffffff
v > 2**31 ? v - 2**32 : v
@@ -7,19 +7,19 @@ class S3Config
def self.load_config
filename = "#{RAILS_ROOT}/config/amazon_s3.yml"
config = YAML.load_file(filename)
-
+
unless config
raise "Config object from #{filename} is nil"
end
-
+
unless config[RAILS_ENV]
raise "No environment #{RAILS_ENV} found in #{filename}"
end
@@access_key_id = config[RAILS_ENV]['access_key_id']
@@secret_access_key = config[RAILS_ENV]['secret_access_key']
@@bucket = config[RAILS_ENV]['bucket_name']
-
+
unless @@access_key_id && @@secret_access_key && @@bucket
raise "Please configure your S3 settings in #{filename}."
end
View
@@ -2,7 +2,7 @@ module D2S3
module Signature
$hexcase = false # hex output format. false - lowercase; true - uppercase
$b64pad = "=" # base-64 pad character. "=" for strict RFC compliance
- $chrsz = 8 # bits per input character. 8 - ASCII; 16 - Unicode
+ $chrsz = 8 # bits per input character. 8 - ASCII; 16 - Unicode
def hex_sha1(s)
return binb2hex(core_sha1(str2binb(s), s.length * $chrsz))
@@ -59,9 +59,9 @@ def core_sha1(x, len)
#for(var j = 0; j < 80; j++)
j = 0
while(j < 80)
- if(j < 16)
+ if(j < 16)
w[j] = x[i + j] || 0
- else
+ else
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1)
end
@@ -88,7 +88,7 @@ def core_sha1(x, len)
# Perform the appropriate triplet combination function for the current
# iteration
def sha1_ft(t, b, c, d)
- return (b & c) | ((~b) & d) if(t < 20)
+ return (b & c) | ((~b) & d) if(t < 20)
return b ^ c ^ d if(t < 40)
return (b & c) | (b & d) | (c & d) if(t < 60)
return b ^ c ^ d;
@@ -103,7 +103,7 @@ def sha1_kt(t)
# Calculate the HMAC-SHA1 of a key and some data
def core_hmac_sha1(key, data)
bkey = str2binb(key)
- if(bkey.length > 16)
+ if(bkey.length > 16)
bkey = core_sha1(bkey, key.length * $chrsz)
end
@@ -158,7 +158,7 @@ def str2binb(str)
# str += String.fromCharCode((bin[i>>5] >>> (32 - $chrsz - i%32)) & mask);
# return str;
# }
- #
+ #
# Convert an array of big-endian words to a hex string.
def binb2hex(binarray)
@@ -14,7 +14,7 @@ def s3_http_upload_tag(options = {})
expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
max_filesize = options[:max_filesize] || 1.megabyte
submit_button = options[:submit_button] || '<input type="submit" value="Upload">'
-
+
options[:form] ||= {}
options[:form][:id] ||= 'upload-form'
options[:form][:class] ||= 'upload-form'

0 comments on commit 45a0fe7

Please sign in to comment.