diff options
author | Eric Wong <normalperson@yhbt.net> | 2009-11-12 18:49:21 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2009-11-12 18:51:17 -0800 |
commit | 701405d00364c63c0efbf11a39368dd2cbee0053 (patch) | |
tree | 86a9c8ca562665e87dbae91ecfc64f837c47248b /examples/rails_app-2.3.4/app | |
parent | ca0493aa6b53a38b6254c29b44982c369ad9757f (diff) | |
download | upr-701405d00364c63c0efbf11a39368dd2cbee0053.tar.gz |
Add jQuery-compatible Rails example
This uses the new Upr::JSON class to generate output. Apparently this is the same format used by Apache, nginx, and lighttpd. Hopefully this will help increase upr adoption.
Diffstat (limited to 'examples/rails_app-2.3.4/app')
4 files changed, 98 insertions, 12 deletions
diff --git a/examples/rails_app-2.3.4/app/controllers/application_controller.rb b/examples/rails_app-2.3.4/app/controllers/application_controller.rb index 11df5d1..eccce4e 100644 --- a/examples/rails_app-2.3.4/app/controllers/application_controller.rb +++ b/examples/rails_app-2.3.4/app/controllers/application_controller.rb @@ -1,3 +1,10 @@ class ApplicationController < ActionController::Base - helper :all # include all helpers, all the time + # helper :all # include all helpers, all the time + defined?($upr) or before_filter do + # grab the backend in case we forget to set it (or if we're using DRb) + defined?($upr) or ObjectSpace.each_object(Upr::InputWrapper) do |x| + $upr ||= x.backend + end + end + end diff --git a/examples/rails_app-2.3.4/app/controllers/files_controller.rb b/examples/rails_app-2.3.4/app/controllers/files_controller.rb index 6509a71..004c13e 100644 --- a/examples/rails_app-2.3.4/app/controllers/files_controller.rb +++ b/examples/rails_app-2.3.4/app/controllers/files_controller.rb @@ -1,21 +1,26 @@ require 'digest/sha1' class FilesController < ApplicationController - defined?($upr) or before_filter do - # grab the backend in case we forget to set it (or if we're using DRb) - defined?($upr) or ObjectSpace.each_object(Upr::InputWrapper) do |x| - $upr ||= x.backend + # used by streaming upload progress + def new + if request.post? + size, hexdigest = _read_sha1_size + msg = "Successfully upload file (size: #{size}, sha1: #{hexdigest})" + flash[:notice] = msg + redirect_to "/files/new" end end def index end + # used by mup-compatible upload progress def status tmp = $upr.read(params[:upload_id]).inspect render :text => "#{Rack::Utils.escape_html(tmp)}\n" end + # used by mup-compatible upload progress def progress render :update do |page| status = $upr.read(params[:upload_id]) and @@ -23,19 +28,27 @@ class FilesController < ApplicationController end end + # used by mup-compatible upload progress def upload - file = params[:data] + size, hexdigest = _read_sha1_size + render :text => "sha1: #{hexdigest}<br />" \ + "size: #{size}<br />" \ + '<script type="text/javascript">' \ + 'window.parent.UploadProgress.finish();</script>' + end + +private + + def _read_sha1_size + file = params[:data] or return [ -1, :unknown ] + File.unlink(file.path) if file.respond_to?(:path) digest = Digest::SHA1.new if buf = file.read(16384) begin digest.update(buf) end while file.read(16384, buf) end - size = file.stat.size - File.unlink(file.path) - render :text => "sha1: #{digest.hexdigest}<br />" \ - "size: #{size}<br />" \ - '<script type="text/javascript">' \ - 'window.parent.UploadProgress.finish();</script>' + [ file.size, digest.hexdigest ] end + end diff --git a/examples/rails_app-2.3.4/app/controllers/progress_controller.rb b/examples/rails_app-2.3.4/app/controllers/progress_controller.rb new file mode 100644 index 0000000..b3d91f3 --- /dev/null +++ b/examples/rails_app-2.3.4/app/controllers/progress_controller.rb @@ -0,0 +1,10 @@ +class ProgressController < ApplicationController + def index + opt = { + :backend => $upr, + :frequency => 0.5, + :env => request.env, + } + render(Upr::JSON.new(opt).rails_render_options) + end +end diff --git a/examples/rails_app-2.3.4/app/views/files/new.html.erb b/examples/rails_app-2.3.4/app/views/files/new.html.erb new file mode 100644 index 0000000..42f5f27 --- /dev/null +++ b/examples/rails_app-2.3.4/app/views/files/new.html.erb @@ -0,0 +1,56 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" +"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<!-- example taken from git://github.com/drogus/jquery-upload-progress --> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> + <head> + <title>ajaxFileUpload</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <script src="/javascripts/jquery.js"></script> + <script src="/javascripts/jquery.uploadProgress.js"></script> + <script type="text/javascript"> + $(function() { + $('form').uploadProgress({ + /* scripts locations for safari */ + jqueryPath: "/javascripts/jquery.js", + uploadProgressPath: "/javascripts/jquery.uploadProgress.js", + start:function(){}, + uploading: function(upload) {$('#percents').html(upload.percents+'%');}, + interval: 500 + }); + }); + </script> + <style type="text/css"> + .bar { + width: 300px; + } + + #progress { + background: #eee; + border: 1px solid #222; + margin-top: 20px; + } + #progressbar { + width: 0px; + height: 24px; + background: #333; + } + </style> + </head> + + <body> + <% if flash[:notice] %> + <h1><%= flash[:notice] %></h1> + <% end %> + <form id="upload" enctype="multipart/form-data" action="/files/new" method="post"> + <input name="data" type="file"/> + <input type="submit" value="Upload"/> + </form> + + <div id="uploading"> + <div id="progress" class="bar"> + <div id="progressbar"> </div> + </div> + </div> + <div id="percents"></div> + </body> +</html> |