Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save djbender/e5733e7ffe1fea411912 to your computer and use it in GitHub Desktop.

Select an option

Save djbender/e5733e7ffe1fea411912 to your computer and use it in GitHub Desktop.

Revisions

  1. @schneems schneems revised this gist May 15, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions screen.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    .progress {
    max-width: 600px;
    margin: 0.2em 0 0.2em 0;
    }

    .progress .bar {
    height: 1.2em;
    padding: 0.2em;
    color: white;
    display: none;
    }
  2. @schneems schneems revised this gist May 15, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion user_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    # GET /users/new
    def new
    @s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: 201, acl: :public_read)
    @s3_direct_post = S3_BUCKET.presigned_post(
    key: "uploads/#{SecureRandom.uuid}/${filename}",
    acl: :public_read,
    success_action_status: 201)
    @user = User.new
    end
  3. @schneems schneems revised this gist May 15, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions users-show.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <!-- ... -->

    <%= form_for(@user, html: { class: "directUpload" }) do |f| %>
    <% if @user.errors.any? %>
    <div id="error_explanation">
  4. @schneems schneems renamed this gist May 15, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @schneems schneems revised this gist May 15, 2014. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion foo.js.erb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,26 @@
    <%= form_for(@user, html: { class: "directUpload" }) do |f| %>
    <% if @user.errors.any? %>
    <div id="error_explanation">
    <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

    <ul>
    <% @user.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
    <% end %>
    </ul>
    </div>
    <% end %>

    <div class="field">
    <%= f.label :avatar_url %><br>
    <%= f.file_field :avatar_url %>
    </div>
    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>

    <script>
    $(function() {
    $('.directUpload').find("input:file").each(function(i, elem) {
    var fileInput = $(elem);
    @@ -50,4 +73,5 @@
    }
    });
    });
    });
    });
    </script>
  6. @schneems schneems revised this gist May 15, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions config-initializers-aws.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    AWS.config(access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] )

    S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]
  7. @schneems schneems revised this gist May 15, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions user_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # GET /users/new
    def new
    @s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: 201, acl: :public_read)
    @user = User.new
    end
  8. @schneems schneems renamed this gist May 15, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @schneems schneems created this gist May 15, 2014.
    53 changes: 53 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    $(function() {
    $('.directUpload').find("input:file").each(function(i, elem) {
    var fileInput = $(elem);
    var form = $(fileInput.parents('form:first'));
    var submitButton = form.find('input[type="submit"]');
    var progressBar = $("<div class='bar'></div>");
    var barContainer = $("<div class='progress'></div>").append(progressBar);
    fileInput.after(barContainer);
    fileInput.fileupload({
    fileInput: fileInput,
    url: '<%= @s3_direct_post.url %>',
    type: 'POST',
    autoUpload: true,
    formData: <%= @s3_direct_post.fields.to_json.html_safe %>,
    paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]"
    dataType: 'XML', // S3 returns XML if success_action_status is set to 201
    replaceFileInput: false,
    progressall: function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    progressBar.css('width', progress + '%')
    },
    start: function (e) {
    console.log('Started');
    submitButton.prop('disabled', true);

    progressBar.
    css('background', 'green').
    css('display', 'block').
    css('width', '0%').
    text("Loading...");
    },
    done: function(e, data) {
    console.log('Done');
    submitButton.prop('disabled', false);
    progressBar.text("Uploading done");

    // extract key and generate URL from response
    var key = $(data.jqXHR.responseXML).find("Key").text();
    var url = '//<%= @s3_direct_post.url.host %>/' + key;

    // create hidden field
    var input = $("<input />", { type:'hidden', name: fileInput.attr('name'), value: url })
    form.append(input);
    },
    fail: function(e, data) {
    console.log("Failed");
    progressBar.
    css("background", "red").
    text("Failed");
    }
    });
    });
    });