Skip to content

Commit

Permalink
Merge remote branch 'nickurban/uploadify' into uploadify
Browse files Browse the repository at this point in the history
  • Loading branch information
parndt committed Dec 30, 2010
2 parents 5b6a866 + 8f9eb8b commit 2d451b8
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -34,6 +34,8 @@ gem 'refinerycms', :path => '.'
gem 'friendly_id', :git => 'git://github.com/parndt/friendly_id', :branch => 'globalize3'
gem 'globalize3', :git => 'git://github.com/refinerycms/globalize3.git'

gem 'mime-types', :require => 'mime/types'

# Specify additional Refinery CMS Engines here (all optional):
gem 'refinerycms-generators', '~> 0.9.9', :git => 'git://github.com/resolve/refinerycms-generators.git'
# gem 'refinerycms-inquiries', '~> 0.9.9.9'
Expand Down
5 changes: 5 additions & 0 deletions resources/app/controllers/admin/resources_controller.rb
Expand Up @@ -12,9 +12,14 @@ def new
def create
@resources = []
unless params[:resource].present? and params[:resource][:file].is_a?(Array)
# Set the MIME type, since Flash doesn't do it properly. As suggested here:
# http://www.glrzad.com/ruby-on-rails/using-uploadify-with-rails-3-part-2-controller-example/
params[:resource][:file].content_type = MIME::Types.type_for(params[:resource][:file].original_filename).to_s
@resources << (@resource = Resource.create(params[:resource]))
else
params[:resource][:file].each do |resource|
# Ditto
resource.content_type = MIME::Types.type_for(resource.original_filename).to_s
@resources << (@resource = Resource.create(:file => resource))
end
end
Expand Down
19 changes: 19 additions & 0 deletions resources/app/middleware/flash_session_cookie_middleware.rb
@@ -0,0 +1,19 @@
require 'rack/utils'

class FlashSessionCookieMiddleware
def initialize(app, session_key = '_session_id')
@app = app
@session_key = session_key
end

def call(env)
if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
req = Rack::Request.new(env)
env['HTTP_COOKIE'] = [ @session_key,
req.params[@session_key] ].join('=').freeze unless req.params[@session_key].nil?
env['HTTP_ACCEPT'] = "#{req.params['_http_accept']}".freeze unless req.params['_http_accept'].nil?
end

@app.call(env)
end
end
68 changes: 68 additions & 0 deletions resources/app/views/admin/resources/_form.html.erb
@@ -1,3 +1,71 @@
<%# load Uploadify libraries, etc. %>
<% content_for :stylesheets do %>
<%= stylesheet_link_tag '/uploadify/uploadify.css' %>
<% end %>
<% content_for :javascripts do %>
<%= javascript_include_tag '/uploadify/swfobject.js' %>
<%= javascript_include_tag '/uploadify/jquery.uploadify.v2.1.4.min.js' %>

<script type="text/javascript">
<%- session_key = Rails.application.config.session_options[:key] -%>
$(function () {
// Create an empty object to store our custom script data
var uploadify_script_data = {};

// Fetch the CSRF meta tag data
var csrf_token = $('meta[name=csrf-token]').attr('content');
var csrf_param = $('meta[name=csrf-param]').attr('content');

// Now associate the data in the config, encoding the data safely
uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token));

// Associate the session information
uploadify_script_data["<%= u session_key %>"] = "<%= u cookies[session_key] %>";

<% if @app_dialog -%>
uploadify_script_data['app_dialog'] = '<%= @app_dialog %>';
uploadify_script_data['field'] = '<%= @field %>';
uploadify_script_data['update_resource'] = '<%= @update_resource %>';
uploadify_script_data['update_text'] = '<%= @update_text %>';
uploadify_script_data['thumbnail'] = '<%= @thumbnail %>';
uploadify_script_data['callback'] = '<%= @callback %>';
uploadify_script_data['conditions'] = '<%= @conditions %>';
uploadify_script_data['current_link'] = '<%= @current_link %>';
<% end -%>

// DEBUG
console.debug(uploadify_script_data);

// Configure Uploadify
$('#resource_file').uploadify({
'uploader' : '/uploadify/uploadify.swf',
'script' : '<%= @url_override %>',
'cancelImg' : '/uploadify/cancel.png',
'scriptData' : uploadify_script_data,
'fileDataName' : 'resource[file]',
'fileExt' : '*.jpg;*.gif;*.png;*.mp3;*.mp2;*.m4a;*.ogg',
'sizeLimit' : <%= Resource::MAX_SIZE_IN_MB * 1024 * 1024 %>, // size in bytes
//'auto': true,
//'fileDesc' : 'Please select a file to upload',
'onComplete' : function(event, ID, fileObj, response, data) {
<%# if @app_dialog or action_name == "insert" or from_dialog? %>
<% if action_name == "insert" %>
window.location = '<%= insert_admin_resources_url({:dialog => true, :app_dialog => true, :update_resource => @update_resource, :update_text => @update_text, :callback => @callback, :field => @field }) %>';
<% elsif action_name == 'new' %>
parent.window.location = '<%= admin_resources_url %>'
<% end %>
}
});
});

// hook into submit button and make it trigger upload
$('#new_resource').submit(function() {
$('#resource_file').uploadifyUpload();
return false;
});
</script>
<% end %>
<%= form_for [:admin, @resource], :url => @url_override || @url,
:html => {:multipart => true} do |f| -%>
Expand Down
14 changes: 14 additions & 0 deletions resources/lib/resources.rb
Expand Up @@ -42,6 +42,20 @@ class Engine < Rails::Engine
}
end

### For Uploadify ###
# Thanks to Damian Galarza for his helpful guides:
# http://www.glrzad.com/ruby-on-rails/using-uploadify-with-rails-3/
initializer "resources-with-uploadify" do |app|
app.config.autoload_paths += %W( #{config.root}/app/middleware )
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
app.config.middleware.insert_before(
ActionDispatch::Session::CookieStore,
FlashSessionCookieMiddleware,
app.config.session_options[:key]
)
end
### End For Uploadify ###

config.after_initialize do
Refinery::Plugin.register do |plugin|
plugin.name = "refinery_files"
Expand Down
Binary file added resources/public/uploadify/cancel.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions resources/public/uploadify/jquery.uploadify.v2.1.4.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d451b8

Please sign in to comment.