Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Created October 11, 2011 11:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bsodmike/abbb48a690dd386bc4b7 to your computer and use it in GitHub Desktop.
Direct upload to S3... at least, an attempt to do so!
<% @page_title = "eLearning Video Upload"
@body_classes = "single-col"
%>
<% content_for :extras_for_head do %>
<script src="/javascripts/plupload/plupload.full.js" type="text/javascript" charset="utf-8"></script>
<script src="/javascripts/plupload/plupload.html5.js" type="text/javascript" charset="utf-8"></script>
<% end %>
<% session_key_name = ActionController::Base.session_options[:key] %>
<div id="location">
eLA Video Upload
</div>
<div id="content-pane" class="edit-v2">
<% form_for([:admin, @video], :html => { :multipart => true }) do |f| %>
<p>
<div style="margin: 2em 0;" id="upload_container">
<div id="filelist"></div>
<a id="pickfiles" href="#">[Select files]</a>
<a id="uploadfiles" href="#">[Upload files]</a>
</div>
</p>
<p>
<%#= f.submit %> or <%#= link_to 'Cancel', admin_ela_videos_path %>
</p>
<% end %>
</div>
<script>//<[CDATA[
$(function() {
if( $("#filelist").length ){
var uploader = new plupload.Uploader({
runtimes : 'html5',
browse_button : 'pickfiles',
max_file_size : '10mb',
url : '/admin/s3_uploads',
multiple_queues : true,
multipart: true,
multipart_params: {
'_http_accept': 'application/javascript',
'authenticity_token' : '<%= form_authenticity_token %>',
'<%= session_key_name %>' : encodeURIComponent('<%= u cookies[session_key_name] %>')
},
});
// When the user selects files for upload, show them on the page
//
uploader.bind('FilesAdded', function(up, files) {
$.each(files, function(i, file) {
$('#filelist').append(
'<div id="' + file.id + '">' +
file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
'</div>'
);
});
});
// When the file is uploaded, parse the response JSON and show that URL.
//
uploader.bind('FileUploaded', function(up, file, response){
var url = JSON.parse( response.response ).url;
$("#"+file.id).addClass("uploaded").html( url );
});
// Show upload progress over time- with HTML5 doesn't
// really show values besides 0 and 100.
//
uploader.bind('UploadProgress', function(up, file) {
$('#' + file.id + " b").html(file.percent + "%");
});
// When the upload button is clicked, upload!
//
$('#uploadfiles').click(function(e) {
uploader.start();
e.preventDefault();
});
uploader.init();
}
});
//]]></script>
class Admin::S3UploadsController < AdminBaseController
# TODO: admin filters & auth..
def create
s3 = AWS::S3::S3Object.store(params[:name], params[:file], 'ela_videos', :access => :public_read)
render :json => {
:url => public_s3_url(params[:name])
}
end
private
def public_s3_url filename
# if S3_CREDENTIALS['prefix'].present?
# "#{S3_CREDENTIALS['prefix']}/#{filename}"
# else
request.protocol +
AWS::S3::Base.connections['AWS::S3::Base'].options[:server] +
"/ela_videos/#{filename}"
# end
end
end
@bsodmike
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment