diff options
author | Eric Wong <normalperson@yhbt.net> | 2009-11-10 16:03:17 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2009-11-10 16:22:36 -0800 |
commit | 4db2d4f19f95b0b5bb7ba9dcdc7ea72c65760223 (patch) | |
tree | 67572b94f4548f505383a204bed5dc5035eff387 /examples/rails_app-2.3.4/app | |
download | upr-4db2d4f19f95b0b5bb7ba9dcdc7ea72c65760223.tar.gz |
initial commit
Diffstat (limited to 'examples/rails_app-2.3.4/app')
5 files changed, 140 insertions, 0 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 new file mode 100644 index 0000000..11df5d1 --- /dev/null +++ b/examples/rails_app-2.3.4/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < ActionController::Base + helper :all # include all helpers, all the time +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 new file mode 100644 index 0000000..3d82107 --- /dev/null +++ b/examples/rails_app-2.3.4/app/controllers/files_controller.rb @@ -0,0 +1,39 @@ +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 + end + end + + def index + end + + def status + tmp = $upr.read(params[:upload_id]).inspect + render :text => "#{Rack::Utils.escape_html(tmp)}\n" + end + + def progress + render :update do |page| + status = $upr.read(params[:upload_id]) and + page.upload_progress.update(status.length, status.seen) + end + end + + def upload + file = params[:data] + digest = Digest::SHA1.new + if buf = file.read(16384) + begin + digest.update(buf) + end while file.read(16384, buf) + end + render :text => "params: #{Rack::Utils.escape_html(params.inspect)}.\n" \ + "sha1: #{digest.hexdigest}\n" \ + '<script type="text/javascript">' \ + 'window.parent.UploadProgress.finish();</script>' + end +end diff --git a/examples/rails_app-2.3.4/app/helpers/application_helper.rb b/examples/rails_app-2.3.4/app/helpers/application_helper.rb new file mode 100644 index 0000000..22a7940 --- /dev/null +++ b/examples/rails_app-2.3.4/app/helpers/application_helper.rb @@ -0,0 +1,3 @@ +# Methods added to this helper will be available to all templates in the application. +module ApplicationHelper +end diff --git a/examples/rails_app-2.3.4/app/models/upr_status.rb b/examples/rails_app-2.3.4/app/models/upr_status.rb new file mode 100644 index 0000000..81031c2 --- /dev/null +++ b/examples/rails_app-2.3.4/app/models/upr_status.rb @@ -0,0 +1,35 @@ +class UprStatus < ActiveRecord::Base + cattr_accessor :gc_cutoff + @@gc_cutoff = 10 + + class << self + def read(upid) + if rv = find_by_upid(upid) + rv.time = Time.now.to_i + rv.save + rv + end + end + + def start(upid, length) + # this must be a find_or_create_by since some users have twitchy + # fingers and hit the upload button prematurely + find_or_create_by_upid(upid) do |x| + x.length = length + x.time = Time.now.to_i + x.seen = 0 + end + end + + def incr(upid, nr) + update_all("seen = seen + #{nr.to_i}, time = #{Time.now.to_i}", + { :upid => upid }) + end + + def gc + cutoff = Time.now.to_i - @@gc_cutoff + delete_all "time < #{cutoff}" + end + end + +end diff --git a/examples/rails_app-2.3.4/app/views/files/index.html.erb b/examples/rails_app-2.3.4/app/views/files/index.html.erb new file mode 100644 index 0000000..b604ee0 --- /dev/null +++ b/examples/rails_app-2.3.4/app/views/files/index.html.erb @@ -0,0 +1,60 @@ +<html> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> + <title>upr test</title> + <%= javascript_include_tag :all %> +<style type="text/css"> +#progress-bar { + width:500px; + height:25px; + margin:15px; + border:solid 1px #000; + position:relative; +} + +#progress-bar #status-bar { + display:block; + height:25px; + width:0; + background-color:#00f; + border-right:solid 1px #000; + position:absolute; + top:0; left:0; +} + +#progress-bar #status-text { + display:block; + padding: 0 15px; + line-height:25px; + position:absolute; + top:0; left:0; +} +</style> + </head> + <body> +<% +upid = "#{Time.now.to_i}.#{rand}" +act = { :action => 'upload', :upload_id => upid } +opt = { + :multipart => true, + :target => 'upload', + :onsubmit => "UploadProgress.monitor('#{escape_javascript(upid)}')" +} +-%> +<p><%= link_to upid, :action => 'status', :upload_id => upid %></p> +<% form_tag(act, opt) do %> + <div id="file-fields"> + <p><%= file_field_tag :data %></p> + </div> + <p><%= link_to_function 'Add File Field', 'UploadProgress.FileField.add()' %> + </p> + <p><%= submit_tag :Upload %></p> +<% end %> + +<div id="results"></div> +<div id="progress-bar"></div> + +<iframe id="upload" name="upload" src="about:blank"></iframe> + + </body> +</html> |