From 701405d00364c63c0efbf11a39368dd2cbee0053 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Nov 2009 18:49:21 -0800 Subject: 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. --- .../app/controllers/application_controller.rb | 9 +++- .../app/controllers/files_controller.rb | 35 +++++++++----- .../app/controllers/progress_controller.rb | 10 ++++ .../rails_app-2.3.4/app/views/files/new.html.erb | 56 ++++++++++++++++++++++ 4 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 examples/rails_app-2.3.4/app/controllers/progress_controller.rb create mode 100644 examples/rails_app-2.3.4/app/views/files/new.html.erb (limited to 'examples/rails_app-2.3.4/app') 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}
" \ + "size: #{size}
" \ + '' + 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}
" \ - "size: #{size}
" \ - '' + [ 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 @@ + + + + + ajaxFileUpload + + + + + + + + + <% if flash[:notice] %> +

<%= flash[:notice] %>

+ <% end %> +
+ + +
+ +
+
+
 
+
+
+
+ + -- cgit v1.2.3-24-ge0c7