Class: UploadsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/uploads_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #api_authenticate!, #current_project, #expire_revision!, #followed_projects, #no_cache, #read_revision, #require_login, #return_or_cache_revision!, #revision, #save_current_project, #set_current_project, #unfurling?

Methods included from UrlHelper

#edit_release_path, #edit_release_url, #feature_path, #github_commit_range_url, #github_commit_url, #github_project_url, #github_url?, #goldmine_case_number_url, #link_to_project_feature, #new_release_url, #release_path, #release_url, #releases_path

Instance Method Details

#policiesObject

Using dropzone to upload straight to S3 github.com/enyo/dropzone/issues/33

Browser Uploads to S3 using HTML POST Forms aws.amazon.com/articles/1434

Direct Browser Uploading – Amazon S3, CORS, FileAPI, XHR2 and Signed PUTs www.ioncannon.net/programming/1539/direct-browser-uploading-amazon-s3-cors-fileapi-xhr2-and-signed-puts/

Configuring CORS on an S3 bucket docs.aws.amazon.com/AmazonS3/latest/dev/cors.html

Granting Access to a Single S3 Bucket Using Amazon IAM mikeferrier.com/2011/10/27/granting-access-to-a-single-s3-bucket-using-amazon-iam/



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/uploads_controller.rb', line 22

def policies
  ext = File.extname(params[:name])
  unique_file_name = params.values_at(:name, :size, :type).join(".")
  filename = Digest::SHA1.hexdigest(unique_file_name) + ext
  object_name = "uploads/#{current_user.id}/#{filename}"

  policy_document = MultiJson.dump({
    "expiration" => 1.day.from_now.utc.iso8601,
    "conditions" => [
      {"bucket" => Houston.config.s3[:bucket]},
      ["starts-with", "$key", "uploads/"],
      {"acl" => "public-read"},
      {"success_action_status" => "201"},
      ["content-length-range", 0, 3 * 1048576] # 3 MB
    ]})

  policy = Base64.encode64(policy_document).gsub("\n","")

  signature = Base64.encode64(
      OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new("sha1"),
          Houston.config.s3[:secret], policy)
      ).gsub("\n","")

  render json: {
    "AWSAccessKeyId" => Houston.config.s3[:access_key],
    key: object_name,
    policy: policy,
    signature: signature,
    success_action_status: 201,
    acl: "public-read" }
end