Skip to content

Instantly share code, notes, and snippets.

@camelpunch
Created June 21, 2010 23:51
Show Gist options
  • Select an option

  • Save camelpunch/447716 to your computer and use it in GitHub Desktop.

Select an option

Save camelpunch/447716 to your computer and use it in GitHub Desktop.
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
<% ungulate_upload_form_for(@image_upload) do %>
<%= label_tag :file, 'Avatar' %>
<%= file_field_tag :file %>
<%= submit_tag %>
<% end %>
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