flash_cookie_session 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flash_cookie_session.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,76 @@
1
+ === Rails 3 cookie sessions can cooperate with Flash
2
+
3
+ This is useful for Flash-based file uploaders (SWFUpload, Uploadify, Plupload, etc)
4
+
5
+ The problem is that Rails uses cookie-based sessions to keep track of the current_user, etc. You may also have to worry about the +authenticity_token+ being passed around if you're using +protect_from_forgery+ and you'd like to avoid those pesky +InvalidAuthenticityToken+ errors.
6
+
7
+ There are many, many blog posts about this: http://www.google.co.uk/search?q=rails+flash+upload
8
+
9
+ I'm not sure who first figured everything out, so I won't bother trying to credit anyone. Thank you, though, kind person who figured all of this out in the first place. This gem represents a meager attempt to make this all a bit easier to deal with in Rails 3. No need for special routes, no need to manually configure FlashSessionCookieMiddleware, etc.
10
+
11
+ === Installation
12
+
13
+ 1) Add this gem to your Gemfile:
14
+
15
+ gem 'flash_cookie_session'
16
+
17
+ 2) Convince your Flash uploader to pass in the additional params necessary. This is going to be different given the Flash uploader you're using, but see the provided SWFUpload example below, and note the +post_params+ section:
18
+
19
+ var swfu = new SWFUpload({
20
+ debug : false,
21
+ flash_url : '/flash/swfupload.swf',
22
+ upload_url: '<%= upload_url %>',
23
+
24
+ post_params : {
25
+ "<%= key = Rails.application.config.session_options[:key] %>" : "<%= cookies[key] %>",
26
+ "<%= request_forgery_protection_token %>" : "<%= form_authenticity_token %>",
27
+ },
28
+
29
+ button_image_url: "/images/swfupload.png",
30
+ button_width: "100",
31
+ button_height: "100",
32
+ button_placeholder_id: "swfupload",
33
+
34
+ file_size_limit : "20 MB",
35
+ file_types : "*",
36
+ file_types_description : "Files",
37
+ file_upload_limit : 20,
38
+ file_queue_limit : 20,
39
+
40
+ file_queued_handler : fileQueued,
41
+ upload_start_handler : uploadStart,
42
+ upload_progress_handler : uploadProgress,
43
+ upload_success_handler : uploadSuccess,
44
+ upload_complete_handler : uploadComplete
45
+ });
46
+
47
+ function fileQueued(file){
48
+ // alert('queued');
49
+ this.startUpload();
50
+ }
51
+
52
+ function uploadStart(file){
53
+ // alert('start');
54
+ }
55
+
56
+ function uploadProgress(file, bytesLoaded, bytesTotal){
57
+ }
58
+
59
+ function uploadSuccess(file, serverData){
60
+ // alert('success');
61
+ }
62
+
63
+ function uploadComplete(file){
64
+ // alert('complete');
65
+ this.startUpload();
66
+ }
67
+
68
+ With SWFUpload, the post_params are sent along with the file upload, which would be in params[:Filedata] in this example.
69
+
70
+ === Additional info
71
+
72
+ This is frustrating and annoying stuff, and I'm sorry that we have to deal with it. If you have an idea about how to improve the situation or example for additional Flash uploaders, pull requests that expand on the README and/or Rails 3 gem are more than welcome.
73
+
74
+ === License
75
+
76
+ Released under the MIT License: http://en.wikipedia.org/wiki/MIT_License
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "flash_cookie_session/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "flash_cookie_session"
7
+ s.version = FlashCookieSession::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Trevor Turk"]
10
+ s.email = ["trevorturk@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/flash_cookie_session"
12
+ s.summary = %q{Rails 3 cookie sessions can cooperate with Flash}
13
+ s.description = %q{Useful for Flash-based file uploaders (SWFUpload, Uploadify, Plupload, etc)}
14
+
15
+ s.rubyforge_project = "flash_cookie_session"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency("rails", ["~> 3.0"])
23
+ end
@@ -0,0 +1,3 @@
1
+ require 'rails'
2
+ require 'flash_cookie_session/middleware.rb'
3
+ require 'flash_cookie_session/railtie.rb'
@@ -0,0 +1,19 @@
1
+ require 'rack/utils'
2
+
3
+ module FlashCookieSession
4
+ class Middleware
5
+ def initialize(app, session_key = '_session_id')
6
+ @app = app
7
+ @session_key = session_key
8
+ end
9
+
10
+ def call(env)
11
+ if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
12
+ req = Rack::Request.new(env)
13
+ env['HTTP_COOKIE'] = [ @session_key, req.params[@session_key] ].join('=').freeze if req.params[@session_key]
14
+ end
15
+
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module FlashCookieSession
2
+ class Railtie < Rails::Railtie
3
+ initializer "flash_cookie_session.initializer" do
4
+
5
+ # Manually load the session_store config file
6
+ # even though this shouldn't be necessary
7
+ # otherwise Rails.application.config.session_options[:key] comes up nil :(
8
+ session_store_config = Rails.root.join('config/initializers/session_store.rb')
9
+
10
+ if session_store_config.exist?
11
+ require session_store_config
12
+ end
13
+
14
+ Rails.application.middleware.insert_before(
15
+ Rails.application.config.session_store,
16
+ "FlashCookieSession::Middleware",
17
+ Rails.application.config.session_options[:key]
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module FlashCookieSession
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flash_cookie_session
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Trevor Turk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-12 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Useful for Flash-based file uploaders (SWFUpload, Uploadify, Plupload, etc)
37
+ email:
38
+ - trevorturk@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - Rakefile
49
+ - Readme.rdoc
50
+ - flash_cookie_session.gemspec
51
+ - lib/flash_cookie_session.rb
52
+ - lib/flash_cookie_session/middleware.rb
53
+ - lib/flash_cookie_session/railtie.rb
54
+ - lib/flash_cookie_session/version.rb
55
+ has_rdoc: true
56
+ homepage: http://rubygems.org/gems/flash_cookie_session
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: flash_cookie_session
85
+ rubygems_version: 1.3.7
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Rails 3 cookie sessions can cooperate with Flash
89
+ test_files: []
90
+