<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jetpack Flight Log &#187; swfobject</title>
	<atom:link href="http://jetpackweb.com/blog/tags/swfobject/feed/" rel="self" type="application/rss+xml" />
	<link>http://jetpackweb.com/blog</link>
	<description>Rock{et}ing the interweb</description>
	<lastBuildDate>Sun, 12 Jun 2011 17:51:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Rails 2.3.4 and SWFUpload &#8211; Rack Middleware for Flash Uploads that Degrade Gracefully</title>
		<link>http://jetpackweb.com/blog/2009/10/21/rails-2-3-4-and-swfupload-rack-middleware-for-flash-uploads-that-degrade-gracefully/</link>
		<comments>http://jetpackweb.com/blog/2009/10/21/rails-2-3-4-and-swfupload-rack-middleware-for-flash-uploads-that-degrade-gracefully/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 05:24:15 +0000</pubDate>
		<dc:creator>Brian Racer</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[swfobject]]></category>
		<category><![CDATA[swfupload]]></category>

		<guid isPermaLink="false">http://jetpackweb.com/blog/?p=523</guid>
		<description><![CDATA[Browser upload controls have been pretty much the same for years. They are very difficult to style, and do not look consistent across browsers. Perhaps the biggest issue with them is they provide no feedback to the user about how long the submission will take. One alternative is to use Flash for the uploads. There [...]]]></description>
			<content:encoded><![CDATA[<p>Browser upload controls have been pretty much the same for years. They are very difficult to style, and do not look consistent across browsers. Perhaps the biggest issue with them is they provide no feedback to the user about how long the submission will take. One alternative is to use Flash for the uploads. There are numerous libraries available, I like <a href="http://swfupload.org/" target="_blank">SWFUpload</a>. Since the reason you are here is probably because you can&#8217;t get it working in Rails, I&#8217;m going to try and help you deal with the quirks associated with using Flash and Rails together.</p>
<p>It used to be you would monkeypatch the CGI class to get Flash uploaders to work due to issues with Flash. With the introduction of <a href="http://rack.rubyforge.org/" target="_blank">Rack</a> in Rails 2.3 things now work quite differently. What we will do is create some rack middleware to intercept traffic from Flash to deal with it&#8217;s quirks. I have created a small example application of an mp3 player and uploader. You will probably want to download it, as it contains a few files not displayed in this article. You can clone it from the <a href="http://github.com/anveo/swfupload_demo" target="_blank">github project page</a>.</p>
<p>First lets create a simple Song model:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">.<span class="sy0">/</span>script generate model Song title:string artist:string length_in_seceonds:integer track_file_name:string track_content_type:string track_file_size:integer</pre></div></div>

<p><i>title</i>, <i>artist</i>, and <i>length_in_seconds</i> are meta-data we will pull from the ID3 tags of the uploaded mp3 file, and the rest will be used by <a href="http://www.thoughtbot.com/projects/paperclip" target="_blank">Paperclip</a> to handle the attachment. Lets add the paperclip attachment and a few simple validations to our new Song model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="kw1">class</span> Song <span class="sy0">&lt;</span> <span class="re2">ActiveRecord::Base</span>
&nbsp;
  has_attached_file <span class="re3">:track</span>,
                    <span class="re3">:path</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;:rails_root/public/assets/:attachment/:id_partition/:id/:style/:basename.:extension&quot;</span>,
                    <span class="re3">:url</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;/assets/:attachment/:id_partition/:id/:style/:basename.:extension&quot;</span>
&nbsp;
  validates_presence_of <span class="re3">:title</span>, <span class="re3">:artist</span>, <span class="re3">:length_in_seconds</span>
  validates_attachment_presence <span class="re3">:track</span>
  validates_attachment_content_type <span class="re3">:track</span>, <span class="re3">:content_type</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span> <span class="st0">'application/mp3'</span>, <span class="st0">'application/x-mp3'</span>, <span class="st0">'audio/mpeg'</span>, <span class="st0">'audio/mp3'</span> <span class="br0">&#93;</span>
  validates_attachment_size <span class="re3">:track</span>, <span class="re3">:less_than</span> <span class="sy0">=&gt;</span> <span class="nu0">20</span>.<span class="me1">megabytes</span>
&nbsp;
  attr_accessible <span class="re3">:title</span>, <span class="re3">:artist</span>, <span class="re3">:length_in_seconds</span>
&nbsp;
  <span class="kw1">def</span> convert_seconds_to_time
    total_minutes = length_in_seconds <span class="sy0">/</span> <span class="nu0">1</span>.<span class="me1">minutes</span>
    seconds_in_last_minute = length_in_seconds <span class="sy0">-</span> total_minutes.<span class="me1">minutes</span>.<span class="me1">seconds</span>
    <span class="st0">&quot;#{total_minutes}m #{seconds_in_last_minute}s&quot;</span>
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>

<p>Next comes an upload form and some containers to hold the SWFUploader:</p>

<div class="wp_syntax"><div class="code"><pre class="haml">- form_tag songs_path, :multipart =&gt; true do
  #swfupload_degraded_container
    %noscript= &quot;You should have Javascript enabled for a nicer upload experience&quot;
    = file_field_tag :Filedata
    = submit_tag &quot;Add Song&quot;
  #swfupload_container{ :style =&gt; &quot;display: none&quot; }
    %span#spanButtonPlaceholder
  #divFileProgressContainer</pre></div></div>

<p>The container that holds the SWFUploader will be hidden until we know the user can support it. Initially a standard file upload form will display. A number of things can go wrong, so we need to think about a few levels of degradation here. The user might not have flash installed, the user might have an outdated version of flash, he might not have javascript installed or enabled(which is needed to load the flash), and there may be a problem downloading the flash swf file. Yikes. Luckily using the <a href="http://code.google.com/p/swfobject/" target="_blank">swfobject</a> library we can easily handle all these potential issues.</p>
<p>If the user is missing javascript, he will see the message in the <strong>noscript</strong> tag and be presented a standard upload control.</p>
<p>If the user is missing flash or it is outdated, he will be presented a dialog with an upgrade link. Otherwise he can use the standard upload control.</p>
<p>If  everything goes okey-dokey, then some function handlers we write will hide the the degradation container, and display the flash container.</p>
<p><i>Oh, and just so you know the current version of Flash Player for linux do not fire the event that monitors upload progress, so you will not get the status bar until the upload finishes. No work around for that right now.</i></p>
<p>So lets initialize the SWFUpload via some javascript. Many tutorials out there seem to put the authentication token and session information in the URL, but there are some options with current version of SWFUpload to POST and avoid that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span class="sy0">:</span>javascript
  SWFUpload.<span class="kw3">onload</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> swf_settings <span class="sy0">=</span> <span class="br0">&#123;</span>
&nbsp;
      <span class="co1">// SWFObject settings</span>
      minimum_flash_version<span class="sy0">:</span> <span class="st0">&quot;9.0.28&quot;</span><span class="sy0">,</span>
      swfupload_pre_load_handler<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        $<span class="br0">&#40;</span><span class="st0">'#swfupload_degraded_container'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        $<span class="br0">&#40;</span><span class="st0">'#swfupload_container'</span><span class="br0">&#41;</span>.<span class="me1">show</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span><span class="sy0">,</span>
      swfupload_load_failed_handler<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
      post_params<span class="sy0">:</span> <span class="br0">&#123;</span>
        <span class="st0">&quot;#{session_key_name}&quot;</span><span class="sy0">:</span> <span class="st0">&quot;#{cookies[session_key_name]}&quot;</span><span class="sy0">,</span>
        <span class="st0">&quot;authenticity_token&quot;</span><span class="sy0">:</span> <span class="st0">&quot;#{form_authenticity_token}&quot;</span><span class="sy0">,</span>
      <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
      upload_url<span class="sy0">:</span> <span class="st0">&quot;#{songs_path}&quot;</span><span class="sy0">,</span>
      flash_url<span class="sy0">:</span> <span class="st0">'/flash/swfupload/swfupload.swf'</span><span class="sy0">,</span>
&nbsp;
      file_types<span class="sy0">:</span> <span class="st0">&quot;*.mp3&quot;</span><span class="sy0">,</span>
      file_types_description<span class="sy0">:</span> <span class="st0">&quot;mp3 Files&quot;</span><span class="sy0">,</span>
      file_size_limit<span class="sy0">:</span> <span class="st0">&quot;20 MB&quot;</span><span class="sy0">,</span>
&nbsp;
      button_placeholder_id<span class="sy0">:</span> <span class="st0">&quot;spanButtonPlaceholder&quot;</span><span class="sy0">,</span>
      button_width<span class="sy0">:</span> <span class="nu0">380</span><span class="sy0">,</span>
      button_height<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span>
      button_text <span class="sy0">:</span> <span class="st0">'&lt;span class=&quot;button&quot;&gt;Select Files &lt;span class=&quot;buttonSmall&quot;&gt;(20 MB Max)&lt;/span&gt;&lt;/span&gt;'</span><span class="sy0">,</span>
      button_text_style <span class="sy0">:</span> <span class="st0">'.button { font-family: Helvetica, Arial, sans-serif; font-size: 24pt; } .buttonSmall { font-size: 18pt; }'</span><span class="sy0">,</span>
      button_text_top_padding<span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span>
      button_text_left_padding<span class="sy0">:</span> <span class="nu0">18</span><span class="sy0">,</span>
      button_window_mode<span class="sy0">:</span> SWFUpload.<span class="me1">WINDOW_MODE</span>.<span class="me1">TRANSPARENT</span><span class="sy0">,</span>
      button_cursor<span class="sy0">:</span> SWFUpload.<span class="me1">CURSOR</span>.<span class="me1">HAND</span><span class="sy0">,</span>
      file_queue_error_handler <span class="sy0">:</span> fileQueueError<span class="sy0">,</span>
      file_dialog_complete_handler <span class="sy0">:</span> fileDialogComplete<span class="sy0">,</span>
      upload_progress_handler <span class="sy0">:</span> uploadProgress<span class="sy0">,</span>
      upload_error_handler <span class="sy0">:</span> uploadError<span class="sy0">,</span>
      upload_success_handler <span class="sy0">:</span> uploadSuccess<span class="sy0">,</span>
      upload_complete_handler <span class="sy0">:</span> uploadComplete<span class="sy0">,</span>
&nbsp;
      custom_settings <span class="sy0">:</span> <span class="br0">&#123;</span>
        upload_target<span class="sy0">:</span> <span class="st0">&quot;divFileProgressContainer&quot;</span>
      <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
    <span class="kw2">var</span> swf_upload <span class="sy0">=</span> <span class="kw2">new</span> SWFUpload<span class="br0">&#40;</span>swf_settings<span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>

<p>You will want to check out the <a href="http://demo.swfupload.org/Documentation/#settingsobject" target="_blank">official SWFUpload docs</a> to understand what all of these variable do. There are <a href="http://github.com/anveo/swfupload_demo/blob/master/public/javascripts/swfupload/handlers.js" target="_blank">many handlers</a> we have to define to handle various events, and if you clone the project you can review them in detail.</p>
<p>We also need to set styles for the containers that will be generated. You can see the Sass file I created for SWFUpload <a href="http://github.com/anveo/swfupload_demo/blob/master/app/stylesheets/swfupload.sass" target="_blank">here</a>, and <a href="http://github.com/anveo/swfupload_demo/blob/master/app/stylesheets/nifty.sass" target="_blank">another one</a> for Ryan Bates <a href="http://github.com/ryanb/nifty-generators" target="_blank">nifty_generators</a>.</p>
<p>Another quirk we have to be aware of when dealing with flash uploads is that everything gets a content-type of an octet stream. We will use the <a href="http://mime-types.rubyforge.org/" target="_blank">mime-types</a> library to identify it for validation. Keep in mind it only uses the extension to determine the file type. (<i>I haven&#8217;t tested it yet, but I believe <a href="http://github.com/mattetti/mimetype-fu" target="_blank">mimetype-fu</a> will actually check file-data and magic numbers</i>). By default SWFUpload calls the file parameter &#8216;Filedata&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span class="kw1">def</span> create
    <span class="kw3">require</span> <span class="st0">'mp3info'</span>
&nbsp;
    mp3_info = Mp3Info.<span class="me1">new</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span><span class="re3">:Filedata</span><span class="br0">&#93;</span>.<span class="me1">path</span><span class="br0">&#41;</span>
&nbsp;
    song = Song.<span class="me1">new</span>
    song.<span class="me1">artist</span> = mp3_info.<span class="me1">tag</span>.<span class="me1">artist</span>
    song.<span class="me1">title</span> = mp3_info.<span class="me1">tag</span>.<span class="me1">title</span>
    song.<span class="me1">length_in_seconds</span> = mp3_info.<span class="me1">length</span>.<span class="me1">to_i</span>
&nbsp;
    params<span class="br0">&#91;</span><span class="re3">:Filedata</span><span class="br0">&#93;</span>.<span class="me1">content_type</span> = <span class="re2">MIME::Types</span>.<span class="me1">type_for</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span><span class="re3">:Filedata</span><span class="br0">&#93;</span>.<span class="me1">original_filename</span><span class="br0">&#41;</span>.<span class="me1">to_s</span>
    song.<span class="me1">track</span> = params<span class="br0">&#91;</span><span class="re3">:Filedata</span><span class="br0">&#93;</span>
    song.<span class="me1">save</span>
&nbsp;
    render <span class="re3">:text</span> <span class="sy0">=&gt;</span> <span class="br0">&#91;</span>song.<span class="me1">artist</span>, song.<span class="me1">title</span>, song.<span class="me1">convert_seconds_to_time</span><span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot; - &quot;</span><span class="br0">&#41;</span>
  <span class="kw1">rescue</span> Mp3InfoError <span class="sy0">=&gt;</span> e
    render <span class="re3">:text</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;File error&quot;</span>
  <span class="kw1">rescue</span> <span class="kw4">Exception</span> <span class="sy0">=&gt;</span> e
    render <span class="re3">:text</span> <span class="sy0">=&gt;</span> e.<span class="me1">message</span>
  <span class="kw1">end</span></pre></div></div>

<p>Another annoyance with flash uploads is that it doesn&#8217;t send cookie data. That is why we are sending the session information in the POST data. We will intercept requests from Flash, check for the session key, and if so inject it into the cookie header. We can do this with some pretty simple middleware.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="kw3">require</span> <span class="st0">'rack/utils'</span>
&nbsp;
<span class="kw1">class</span> FlashSessionCookieMiddleware
  <span class="kw1">def</span> initialize<span class="br0">&#40;</span>app, session_key = <span class="st0">'_session_id'</span><span class="br0">&#41;</span>
    <span class="re1">@app</span> = app
    <span class="re1">@session_key</span> = session_key
  <span class="kw1">end</span>
&nbsp;
  <span class="kw1">def</span> call<span class="br0">&#40;</span>env<span class="br0">&#41;</span>
    <span class="kw1">if</span> env<span class="br0">&#91;</span><span class="st0">'HTTP_USER_AGENT'</span><span class="br0">&#93;</span> =~ <span class="sy0">/</span>^<span class="br0">&#40;</span>Adobe<span class="sy0">|</span>Shockwave<span class="br0">&#41;</span> Flash<span class="sy0">/</span>
      params = ::<span class="re2">Rack::Request</span>.<span class="me1">new</span><span class="br0">&#40;</span>env<span class="br0">&#41;</span>.<span class="me1">params</span>
      env<span class="br0">&#91;</span><span class="st0">'HTTP_COOKIE'</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span> <span class="re1">@session_key</span>, params<span class="br0">&#91;</span>@session_key<span class="br0">&#93;</span> <span class="br0">&#93;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">'='</span><span class="br0">&#41;</span>.<span class="me1">freeze</span> <span class="kw1">unless</span> params<span class="br0">&#91;</span>@session_key<span class="br0">&#93;</span>.<span class="kw2">nil</span>?
    <span class="kw1">end</span>
    <span class="re1">@app</span>.<span class="me1">call</span><span class="br0">&#40;</span>env<span class="br0">&#41;</span>
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>

<p>This is a modified version from code the appears in a few tutorials about flash uploads. It will allow the session information to be in the query string *or* POST data. Next we have to make sure this middleware gets put to use so in <i>config/initializers/session_store.rb</i> add:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="re2">ActionController::Dispatcher</span>.<span class="me1">middleware</span>.<span class="me1">insert_before</span><span class="br0">&#40;</span><span class="re2">ActionController::Base</span>.<span class="me1">session_store</span>, FlashSessionCookieMiddleware, <span class="re2">ActionController::Base</span>.<span class="me1">session_options</span><span class="br0">&#91;</span><span class="re3">:key</span><span class="br0">&#93;</span><span class="br0">&#41;</span></pre></div></div>

<p>And that&#8217;s, uhh, all there is too it. Again, I really suggest you <a href="http://github.com/anveo/swfupload_demo" target="_blank">checkout the example project</a>. It also uses the nifty WordPress Audio Player flash control to play the music you upload!</p>
<p><img src="http://jetpackweb.com/blog/wp-content/uploads/2009/10/Picture-21.png" /><br />
<img src="http://jetpackweb.com/blog/wp-content/uploads/2009/10/Picture-11.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://jetpackweb.com/blog/2009/10/21/rails-2-3-4-and-swfupload-rack-middleware-for-flash-uploads-that-degrade-gracefully/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>

