Permalink
Browse files

Now compatible with rails 2.3.

Found and fixed some bugs.
  • Loading branch information...
over committed Aug 8, 2009
1 parent 83601b8 commit c9dbd04ecd664981af9b2ca906dc52e1bcdc4e49
Showing with 47 additions and 22 deletions.
  1. +1 −0 README.rdoc
  2. +41 −19 generators/easy_swf_upload/templates/cgi_session_hack.rb
  3. +1 −1 init.rb
  4. +4 −2 lib/swf_upload_helper.rb
View
@@ -23,6 +23,7 @@ You want to allow uploading only single file at once, you can pass :single_file
In your controller:
class PagesController < ApplicationController
+ # If your rails version < 2.3.0, go this way
session :cookie_only => false, :only => :upload_image_block # this is very important, don't forget to set it to false
def upload_image_block
@@ -1,24 +1,46 @@
-class CGI::Session
- # allows getting session_id from query string if cookie_only is set to false
- alias original_initialize initialize
-
- def initialize(cgiwrapper, option = {})
- unless option['cookie_only']
- session_key = option['session_key'] || '_session_id'
-
- env_table = cgiwrapper.send(:env_table) #todo find correct way to do this
- session_id = [
- env_table['QUERY_STRING'],
- env_table['REQUEST_URI'][/\?(.+)/, 1]
- ].collect do |s|
- s[/#{session_key}=([^&]{32,})/, 1] if s
- end.compact.first
-
- if session_id
- cgiwrapper.instance_variable_set(:@cookies, {session_key => [CGI.unescape(session_id)]})
+#todo: Check GEM version in generator, split to different files
+
+if RAILS_GEM_VERSION < '2.3.0'
+ class CGI::Session
+ alias original_initialize initialize
+
+ def initialize(cgiwrapper, option = {})
+ unless option['cookie_only']
+ session_key = option['session_key'] || '_session_id'
+
+ env_table = cgiwrapper.send(:env_table) #todo find correct way to do this
+ session_id = [
+ env_table['QUERY_STRING'],
+ env_table['REQUEST_URI'][/\?(.+)/, 1]
+ ].collect do |s|
+ s[/#{session_key}=([^&]{32,})/, 1] if s
+ end.compact.first
+
+ if session_id
+ cgiwrapper.instance_variable_set(:@cookies, {session_key => [CGI.unescape(session_id)]})
+ end
end
+
+ original_initialize(cgiwrapper, option)
end
+ end
+else
+ require 'rack/utils'
- original_initialize(cgiwrapper, option)
+ 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/
+ params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
+ env['HTTP_COOKIE'] = [ @session_key, params[@session_key] ].join('=').freeze unless params[@session_key].nil?
+ end
+ @app.call(env)
+ end
end
+
+ ActionController::Dispatcher.middleware.insert_before(ActionController::Base.session_store, FlashSessionCookieMiddleware, ActionController::Base.session_options[:session_key])
end
View
@@ -1,4 +1,4 @@
ActionView::Base.send(:include, SwfUploadHelper)
ActionController::Base.send(:include, SwfUpload)
-ActionView::Helpers::AssetTagHelper.register_javascript_expansion :swf_upload => %w(swfupload swfupload_plugin)
+ActionView::Helpers::AssetTagHelper.register_javascript_expansion :swf_upload => %w(swfupload swfupload_plugin)
View
@@ -1,11 +1,13 @@
module SwfUploadHelper
def swf_upload_area(title, options)
+ session_key = RAILS_GEM_VERSION < "2.3.0" ? ActionController::Base.session[0][:session_key] : ActionController::Base.session_options[:session_key]
+
%Q{<div class="swfUploadArea">
<div class="buttonText" style="display:none">#{title}</div>
<div class="url" style="display:none">#{options[:url]}</div>
<div class="token" style="display:none">#{form_authenticity_token}</div>
- <div class="session_key" style="display:none">#{CGI::escape(ActionController::Base.session[0][:session_key])}</div>
- <div class="session_id" style="display:none">#{session.session_id}</div>
+ <div class="session_key" style="display:none">#{CGI::escape(session_key)}</div>
+ <div class="session_id" style="display:none">#{cookies[session_key]}</div>
<div class="filetypes" style="display:none">#{options[:filetypes]}</div>
<div class="buttonStyle" style="display:none">#{options[:button_style]}</div>
<div class="singleFile" style="display:none">#{options[:single_file] ? 'true' : 'false'}</div>

0 comments on commit c9dbd04

Please sign in to comment.