Created
June 21, 2010 23:51
-
-
Save camelpunch/447716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AvatarsController < ApplicationController | |
| before_filter :new_file_upload, :only => :new | |
| def create | |
| user = User.find params[:user_id] | |
| user.avatar_key = params[:key] | |
| user.save! | |
| flash[:notice] = "Your new avatar is thumbnailing and will be ready soon." | |
| redirect_to user_url(user) | |
| end | |
| protected | |
| def new_file_upload | |
| expiration = 10.hours.from_now | |
| key = "users/#{current_user.to_param}/#{Time.now.strftime('%Y%m%d%H%M%S')}/avatar" | |
| @image_upload = Ungulate::FileUpload.new( | |
| :bucket_url => "http://#{S3_CONFIG['bucket']}.s3.amazonaws.com/", | |
| :key => key, | |
| :policy => { | |
| 'expiration' => expiration, | |
| 'conditions' => [ | |
| {'bucket' => S3_CONFIG['bucket']}, | |
| {'key' => key}, | |
| {'acl' => 'private'}, | |
| {'success_action_redirect' => create_user_avatar_url(current_user)} | |
| ] | |
| } | |
| ) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <% ungulate_upload_form_for(@image_upload) do %> | |
| <%= label_tag :file, 'Avatar' %> | |
| <%= file_field_tag :file %> | |
| <%= submit_tag %> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| after_update Proc.new {|user| user.ungulate_enqueue if user.avatar_key_changed?} | |
| def ungulate_enqueue | |
| Ungulate::FileUpload.enqueue( | |
| :bucket => S3_CONFIG['bucket'], | |
| :key => avatar_key, | |
| :versions => { | |
| :thumbnail => [ :resize_to_fill, 64, 64 ], | |
| :tiny => [ :resize_to_fill, 24, 24 ], | |
| } | |
| ) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment