public
Last active

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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
// This is my handler in the javascript
var uploadCompleteHandler = function(upload_options,event){
$.ajax({
url: '<%= notify_rails_of_successful_upload_path(:format => :js)%>',
global: false,
type: 'POST',
data: ({
'authenticity_token' : '<%= form_authenticity_token %>',
'upload' : {
'file_file_name' : upload_options.FileName,
'file_file_size' : upload_options.FileSize,
'file_content_type' : upload_options.ContentType
}
}),
dataType: 'script'
}
)
};
 
// The action
def swf_create
@upload = Upload.new(params[:upload])
 
respond_to do |format|
if @upload.save
format.js { } # crazy cool stuff here
else
format.js { render :text => 'alert("Failure from Rails!");' }
end
end
end
 
// Then in my upload model
after_create :move_upload_from_temp_to_final_resting_place
 
def move_upload_from_temp_to_final_resting_place
# Rename the image on s3 (more of a move)
AWS::S3::Base.establish_connection!(:access_key_id => S3Config[:access_key_id],:secret_access_key => S3Config[:secret_access_key])
new_name = self.file.path
old_name = "temp/#{self.file_file_name}"
(1..5).each do |try|
begin
# Copy the file
AWS::S3::S3Object.rename(old_name, new_name, S3Config[:bucket], :copy_acl => :true)
break
rescue Exception => e
sleep 1
end
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.