[Catalyst-commits] r6463 - in trunk/Catalyst-Plugin-UploadProgress: . example/Upload example/Upload/lib example/Upload/lib/Upload/Controller example/Upload/lib/Upload/View example/Upload/root example/Upload/root/static/css example/Upload/root/static/js lib/Catalyst/Plugin lib/Catalyst/Plugin/UploadProgress t

andyg at dev.catalyst.perl.org andyg at dev.catalyst.perl.org
Tue Jun 12 19:22:13 GMT 2007


Author: andyg
Date: 2007-06-12 19:22:12 +0100 (Tue, 12 Jun 2007)
New Revision: 6463

Removed:
   trunk/Catalyst-Plugin-UploadProgress/Build.PL
Modified:
   trunk/Catalyst-Plugin-UploadProgress/Changes
   trunk/Catalyst-Plugin-UploadProgress/Makefile.PL
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/README
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload.pm
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/Controller/Upload.pm
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/View/TT.pm
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/form.xhtml
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/css/progress.css
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl.js
   trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.js
   trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm
   trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress/Static.pm
   trunk/Catalyst-Plugin-UploadProgress/t/04uploadprogress.t
Log:
UploadProgress 0.04:
* Switched to Module::Install.
* Allow plugin to work at non-root locations.
* Detect aborted uploads and stop polling javascript.


Deleted: trunk/Catalyst-Plugin-UploadProgress/Build.PL
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/Build.PL	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/Build.PL	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,17 +0,0 @@
-use strict;
-use Module::Build;
-
-my $build = Module::Build->new(
-    create_makefile_pl => 'passthrough',
-    license            => 'perl',
-    module_name        => 'Catalyst::Plugin::UploadProgress',
-    requires           => { 
-        'Catalyst' => '5.50',
-    },
-    create_makefile_pl => 'passthrough',
-    create_readme      => 1,
-    test_files   => [
-        glob('t/*.t') 
-    ]
-);
-$build->create_build_script;

Modified: trunk/Catalyst-Plugin-UploadProgress/Changes
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/Changes	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/Changes	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,5 +1,12 @@
 Revision history for Perl extension Catalyst::Plugin::UploadProgress
 
+0.04    2007-06-12 14:00:00
+        - Fixed issue where you could only run UploadProgress in apps at
+          the webserver root.
+        - Aborted uploads are now detected and the javascript will stop polling
+          for updates when this happens.
+        - Switched to Module::Install.
+
 0.03    2006-10-18 22:15:00
         - Handle race condition where progress handler is
           called before any upload data is received.

Modified: trunk/Catalyst-Plugin-UploadProgress/Makefile.PL
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/Makefile.PL	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/Makefile.PL	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,31 +1,11 @@
-# Note: this file was auto-generated by Module::Build::Compat version 0.03
-    
-    unless (eval "use Module::Build::Compat 0.02; 1" ) {
-      print "This module requires Module::Build to install itself.\n";
-      
-      require ExtUtils::MakeMaker;
-      my $yn = ExtUtils::MakeMaker::prompt
-	('  Install Module::Build now from CPAN?', 'y');
-      
-      unless ($yn =~ /^y/i) {
-	die " *** Cannot install without Module::Build.  Exiting ...\n";
-      }
-      
-      require Cwd;
-      require File::Spec;
-      require CPAN;
-      
-      # Save this 'cause CPAN will chdir all over the place.
-      my $cwd = Cwd::cwd();
-      my $makefile = File::Spec->rel2abs($0);
-      
-      CPAN::Shell->install('Module::Build::Compat')
-	or die " *** Cannot install without Module::Build.  Exiting ...\n";
-      
-      chdir $cwd or die "Cannot chdir() back to $cwd: $!";
-    }
-    eval "use Module::Build::Compat 0.02; 1" or die $@;
-    use lib '_build/lib';
-    Module::Build::Compat->run_build_pl(args => \@ARGV);
-    require Module::Build;
-    Module::Build::Compat->write_makefile(build_class => 'Module::Build');
+use inc::Module::Install 0.65;
+
+name 'Catalyst-Plugin-UploadProgress';
+all_from 'lib/Catalyst/Plugin/UploadProgress.pm';
+
+requires 'Catalyst::Runtime' => '5.50';
+
+tests 't/*.t';
+
+auto_install;
+WriteAll;

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/README
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/README	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/README	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1 +1 @@
-Run script/upload_server.pl to test the application.
+Run script/upload_server.pl -f to test the application.

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/Controller/Upload.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/Controller/Upload.pm	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/Controller/Upload.pm	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,7 +1,8 @@
-package Upload::Controller::Upload;
+package # hide from PAUSE
+    Upload::Controller::Upload;
 
 use strict;
-use base 'Catalyst::Base';
+use base 'Catalyst::Controller';
 use Data::Dumper;
 
 sub default : Private {

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/View/TT.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/View/TT.pm	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload/View/TT.pm	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,29 +1,7 @@
-package Upload::View::TT;
+package # hide from PAUSE
+    Upload::View::TT;
 
 use strict;
 use base 'Catalyst::View::TT';
 
-=head1 NAME
-
-Upload::V::TT - TT View Component
-
-=head1 SYNOPSIS
-
-See L<Upload>
-
-=head1 DESCRIPTION
-
-TT View Component.
-
-=head1 AUTHOR
-
-root
-
-=head1 LICENSE
-
-This library is free software . You can redistribute it and/or modify
-it under the same terms as perl itself.
-
-=cut
-
 1;

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload.pm	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/lib/Upload.pm	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,4 +1,5 @@
-package Upload;
+package # hide from PAUSE
+    Upload;
 
 use strict;
 use Catalyst;
@@ -18,7 +19,7 @@
 sub default : Private {
     my ( $self, $c ) = @_;
 
-    $c->res->redirect( '/upload' );
+    $c->res->redirect( $c->uri_for('/upload') );
 }
 
 sub end : Private {

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/form.xhtml
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/form.xhtml	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/form.xhtml	2007-06-12 18:22:12 UTC (rev 6463)
@@ -9,13 +9,13 @@
     <link href="/static/css/progress.css" media="screen" rel="Stylesheet" type="text/css" />
     <script src="/static/js/progress.js" type="text/javascript"></script>
 	<script src="/static/js/progress.jmpl.js" type="text/javascript"></script>
-	-->
+    -->
 </head>
 
 <body>
 
 <div id="form">
-    <form action="/upload"
+    <form action=""
           method="post"
           enctype="multipart/form-data"
           onsubmit="return startEmbeddedProgressBar(this)">

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/css/progress.css
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/css/progress.css	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/css/progress.css	2007-06-12 18:22:12 UTC (rev 6463)
@@ -20,6 +20,10 @@
     color: #667799;
 }
 
+.aborted {
+    color: red;
+}
+
 .progressmeter .meter {
     position: relative;
     background-color: lightgrey;

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl	2007-06-12 18:22:12 UTC (rev 6463)
@@ -10,6 +10,8 @@
         <td>
 [% IF finished -%]
           <span class="finished">Transfer complete  ([% size %])</span>
+[% ELSIF aborted -%]
+          <span class="aborted">Transfer aborted</span>
 [% ELSE -%]
           [% received %] of [% size %]
 [% END -%]

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl.js
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl.js	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.jmpl.js	2007-06-12 18:22:12 UTC (rev 6463)
@@ -1,5 +1,5 @@
-/* 
-   This Javascript code was generated by Jemplate, the Javascript
+/*
+   This JavaScript code was generated by Jemplate, the JavaScript
    Template Toolkit. Any changes made to this file will be lost the next
    time the templates are compiled.
 
@@ -22,37 +22,40 @@
 //line 4 "progress.jmpl"
 output += stash.get('percent');
 output += '%</div>\n  </div>\n  <table>\n    <tbody>\n      <tr>\n        <th>Status:</th>\n        <td>\n';
-//line 15 "progress.jmpl"
+//line 17 "progress.jmpl"
 if (stash.get('finished')) {
 output += '          <span class="finished">Transfer complete  (';
 //line 12 "progress.jmpl"
 output += stash.get('size');
 output += ')</span>\n';
 }
+else if (stash.get('aborted')) {
+output += '          <span class="aborted">Transfer aborted</span>\n';
+}
 else {
 output += '          ';
-//line 14 "progress.jmpl"
+//line 16 "progress.jmpl"
 output += stash.get('received');
 output += ' of ';
-//line 14 "progress.jmpl"
+//line 16 "progress.jmpl"
 output += stash.get('size');
 output += '\n';
 }
 
 output += '        </td>\n      </tr>\n';
-//line 27 "progress.jmpl"
+//line 29 "progress.jmpl"
 if (stash.get('starttime')) {
 output += '      <tr>\n        <th>Time:</th>\n        <td>';
-//line 21 "progress.jmpl"
+//line 23 "progress.jmpl"
 output += stash.get('elapsedtime');
 output += ' (~ ';
-//line 21 "progress.jmpl"
+//line 23 "progress.jmpl"
 output += stash.get('remainingtime');
 output += ' to go)</td>\n      </tr>\n      <tr>\n        <th>Rate:</th>\n        <td>';
-//line 25 "progress.jmpl"
+//line 27 "progress.jmpl"
 output += stash.get('currentrate');
 output += ' (avg ';
-//line 25 "progress.jmpl"
+//line 27 "progress.jmpl"
 output += stash.get('rate');
 output += ')</td>\n      </tr>\n';
 }

Modified: trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.js
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.js	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/example/Upload/root/static/js/progress.js	2007-06-12 18:22:12 UTC (rev 6463)
@@ -10,7 +10,7 @@
 
     var width  = options.width  || '480';
     var height = options.height || '150';
-    window.open ('/progress?progress_id='+id,'Apache2-UploadProgress','location=0,status=0,width='+width+',height='+height); return true;
+    window.open ('progress?progress_id='+id,'Apache2-UploadProgress','location=0,status=0,width='+width+',height='+height); return true;
 
 
 }
@@ -81,7 +81,7 @@
 
 function reportUploadProgress() {
     
-    url = '/progress?progress_id=' + progress.id;
+    url = 'progress?progress_id=' + progress.id;
 
     var req = new XMLHttpRequest();
     req.open('GET', url, Boolean(handleUploadProgressResults));
@@ -118,7 +118,7 @@
         progress.size         = state.size;
         progress.received     = state.received;
 
-        if ( progress.received != progress.size ) {
+        if ( progress.received != progress.size && !state.aborted ) {
             window.setTimeout( reportUploadProgress, 1000 );
         }
 

Modified: trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress/Static.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress/Static.pm	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress/Static.pm	2007-06-12 18:22:12 UTC (rev 6463)
@@ -93,6 +93,10 @@
     color: #667799;
 }
 
+.aborted {
+    color: red;
+}
+
 .progressmeter .meter {
     position: relative;
     background-color: lightgrey;
@@ -132,7 +136,7 @@
 
     var width  = options.width  || '480';
     var height = options.height || '150';
-    window.open ('/progress?progress_id='+id,'Apache2-UploadProgress','location=0,status=0,width='+width+',height='+height); return true;
+    window.open ('progress?progress_id='+id,'Apache2-UploadProgress','location=0,status=0,width='+width+',height='+height); return true;
 
 
 }
@@ -197,13 +201,13 @@
 
     progress.size     = formatBytes(progress.size);
     progress.received = formatBytes(progress.received);
-
+    
     document.getElementById('progress').innerHTML = Jemplate.process('progress.jmpl', progress);
 }
 
 function reportUploadProgress() {
     
-    url = '/progress?progress_id=' + progress.id;
+    url = 'progress?progress_id=' + progress.id;
 
     var req = new XMLHttpRequest();
     req.open('GET', url, Boolean(handleUploadProgressResults));
@@ -240,7 +244,7 @@
         progress.size         = state.size;
         progress.received     = state.received;
 
-        if ( progress.received != progress.size ) {
+        if ( progress.received != progress.size && !state.aborted ) {
             window.setTimeout( reportUploadProgress, 1000 );
         }
 
@@ -1335,8 +1339,8 @@
 ------------------------------------------------------------------------------*/
 
 __upload_progress_jmpl_js__
-/* 
-   This Javascript code was generated by Jemplate, the Javascript
+/*
+   This JavaScript code was generated by Jemplate, the JavaScript
    Template Toolkit. Any changes made to this file will be lost the next
    time the templates are compiled.
 
@@ -1359,37 +1363,40 @@
 //line 4 "progress.jmpl"
 output += stash.get('percent');
 output += '%</div>\n  </div>\n  <table>\n    <tbody>\n      <tr>\n        <th>Status:</th>\n        <td>\n';
-//line 15 "progress.jmpl"
+//line 17 "progress.jmpl"
 if (stash.get('finished')) {
 output += '          <span class="finished">Transfer complete  (';
 //line 12 "progress.jmpl"
 output += stash.get('size');
 output += ')</span>\n';
 }
+else if (stash.get('aborted')) {
+output += '          <span class="aborted">Transfer aborted</span>\n';
+}
 else {
 output += '          ';
-//line 14 "progress.jmpl"
+//line 16 "progress.jmpl"
 output += stash.get('received');
 output += ' of ';
-//line 14 "progress.jmpl"
+//line 16 "progress.jmpl"
 output += stash.get('size');
 output += '\n';
 }
 
 output += '        </td>\n      </tr>\n';
-//line 27 "progress.jmpl"
+//line 29 "progress.jmpl"
 if (stash.get('starttime')) {
 output += '      <tr>\n        <th>Time:</th>\n        <td>';
-//line 21 "progress.jmpl"
+//line 23 "progress.jmpl"
 output += stash.get('elapsedtime');
 output += ' (~ ';
-//line 21 "progress.jmpl"
+//line 23 "progress.jmpl"
 output += stash.get('remainingtime');
 output += ' to go)</td>\n      </tr>\n      <tr>\n        <th>Rate:</th>\n        <td>';
-//line 25 "progress.jmpl"
+//line 27 "progress.jmpl"
 output += stash.get('currentrate');
 output += ' (avg ';
-//line 25 "progress.jmpl"
+//line 27 "progress.jmpl"
 output += stash.get('rate');
 output += ')</td>\n      </tr>\n';
 }
@@ -1405,3 +1412,4 @@
 }
 
 
+

Modified: trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm	2007-06-12 18:22:12 UTC (rev 6463)
@@ -4,7 +4,7 @@
 use warnings;
 use NEXT;
 
-our $VERSION = '0.03';
+our $VERSION = '0.04';
 
 sub prepare_body_chunk {
     my ( $c, $chunk ) = @_;
@@ -35,15 +35,48 @@
     }
 }
 
+sub prepare_body {
+    my $c = shift;
+    
+    # Detect if the user stopped the upload, prepare_body will die with an invalid
+    # content-length error
+    
+    my $croaked;
+    
+    {
+        no warnings 'redefine';
+        local *Carp::croak = sub {
+            $croaked = shift;
+        };
+        
+        $c->NEXT::prepare_body(@_);
+    }
+    
+    if ( $croaked ) {
+        if ( my $id = $c->req->query_parameters->{progress_id} ) {
+            $c->log->info( "UploadProgress: User aborted upload $id" );
+        
+            # Update progress to flag this so javascript will stop polling
+            my $progress = $c->cache->get( 'upload_progress_' . $id ) || {};
+        
+            $progress->{aborted} = 1;
+        
+            $c->cache->set( 'upload_progress_' . $id, $progress );
+        }
+        
+        # rethrow the error
+        Catalyst::Exception->throw( $croaked );
+    }
+}
+
 sub dispatch {
     my $c = shift;
     
-    # if the URI is /progress?progress_id=<id> intercept the request
+    # if the URI query string is ?progress_id=<id> intercept the request
     # and display the progress JSON.
-    
     my $query = $c->req->uri->path_query;
-    if ( $query =~ /^\/progress\?progress_id=[a-f0-9]{32}$/ ) {
-        return $c->upload_progress_output( $c->req->params->{progress_id} );
+    if ( $c->req->method eq 'GET' && $query =~ m{\?progress_id=([a-f0-9]{32})$} ) {
+        return $c->upload_progress_output( $1 );
     }
     
     return $c->NEXT::ACTUAL::dispatch(@_);
@@ -86,8 +119,11 @@
     }
     
     # format the progress data as JSON
-    my $json   = '{"size":%d,"received":%d}';
-    my $output = sprintf $json, $progress->{size}, $progress->{received};
+    my $json   = '{"size":%d,"received":%d,"aborted":%d}';
+    my $output = sprintf $json, 
+        $progress->{size},
+        $progress->{received},
+        $progress->{aborted} || 0;
     
     $c->response->headers->header( Pragma  => 'no-cache' );
     $c->response->headers->header( Expires 

Modified: trunk/Catalyst-Plugin-UploadProgress/t/04uploadprogress.t
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/t/04uploadprogress.t	2007-06-11 23:37:50 UTC (rev 6462)
+++ trunk/Catalyst-Plugin-UploadProgress/t/04uploadprogress.t	2007-06-12 18:22:12 UTC (rev 6463)
@@ -24,6 +24,8 @@
     plan tests => 12;
 }
 
+use Data::Dump qw(dump);
+
 # test a single file upload
 {
     my $id = '38f8a17599a8dd9c80bfb01643404f80';
@@ -43,12 +45,12 @@
     );
     
     ok( my $response = request($request), 'Request' );
-    ok( $response->is_success, 'Upload ok' );
+    is( $response->content, 'ok', 'Upload ok' );
     
     ok( $response = request("http://localhost/progress?progress_id=$id"), 'Request' );
     is( $response->content_type, 'text/x-json', 'Progress JSON ok' );
     my $content = $response->content;
-    my ( $size, $received ) = $content =~ m/^\{"size":(\d+),"received":(\d+)\}/;
+    my ( $size, $received ) = $content =~ m/"size":(\d+),"received":(\d+)/;
     cmp_ok( $size, '>', 16384, 'JSON size ok' );
     is( $size, $received, 'JSON received ok' );
 }
@@ -79,12 +81,13 @@
     );
     
     ok( my $response = request($request), 'Request' );
-    ok( $response->is_success, 'Multi-file upload OK' );
+    is( $response->content, 'ok', 'Multi-file upload OK' );
     
-    ok( $response = request("http://localhost/progress?progress_id=$id"), 'Request' );
+    # test that any URL with '?progress_id=...' in it will work
+    ok( $response = request("http://localhost/deep/path/progress?progress_id=$id"), 'Request' );
     is( $response->content_type, 'text/x-json', 'Progress JSON ok' );
     my $content = $response->content;
-    my ( $size, $received ) = $content =~ m/^\{"size":(\d+),"received":(\d+)\}/;
+    my ( $size, $received ) = $content =~ m/"size":(\d+),"received":(\d+)/;
     cmp_ok( $size, '>', 49152, 'JSON size ok' );
     is( $size, $received, 'JSON received ok' );
 }




More information about the Catalyst-commits mailing list