[Catalyst-commits] r12748 -
trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin
t0m at dev.catalyst.perl.org
t0m at dev.catalyst.perl.org
Thu Jan 28 00:01:02 GMT 2010
Author: t0m
Date: 2010-01-28 00:01:02 +0000 (Thu, 28 Jan 2010)
New Revision: 12748
Modified:
trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm
Log:
Trim trailing space
Modified: trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm
===================================================================
--- trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm 2010-01-28 00:00:34 UTC (rev 12747)
+++ trunk/Catalyst-Plugin-UploadProgress/lib/Catalyst/Plugin/UploadProgress.pm 2010-01-28 00:01:02 UTC (rev 12748)
@@ -9,28 +9,28 @@
# I'm concerned that this doesn't call super() at all..
override 'prepare_body_chunk' => sub {
my ( $c, $chunk ) = @_;
-
+
my $body = $c->request->{_body};
$body->add( $chunk );
-
+
my $id = $c->req->query_parameters->{progress_id};
-
+
if ( $id ) {
# store current progress in cache
my $progress = $c->cache->get( 'upload_progress_' . $id );
-
+
if ( !defined $progress ) {
# new upload
$progress = {
size => $body->content_length,
received => length $chunk,
};
-
+
$c->cache->set( 'upload_progress_' . $id, $progress );
}
else {
$progress->{received} += length $chunk;
-
+
$c->cache->set( 'upload_progress_' . $id, $progress );
}
}
@@ -38,33 +38,33 @@
override 'prepare_body' => sub {
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;
};
-
+
super;
}
-
+
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 );
}
@@ -72,20 +72,20 @@
override 'dispatch' => sub {
my $c = shift;
-
+
# 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 ( $c->req->method eq 'GET' && $query =~ m{\?progress_id=([a-f0-9]{32})$} ) {
return $c->upload_progress_output( $1 );
}
-
+
return super;
};
after 'setup' => sub {
my $c = shift;
-
+
unless ( $c->can('cache') ) {
Catalyst::Exception->throw(
message => 'UploadProgress requires a cache plugin.'
@@ -95,15 +95,15 @@
sub upload_progress {
my ( $c, $upload_id ) = @_;
-
+
return $c->cache->get( 'upload_progress_' . $upload_id );
}
sub upload_progress_output {
my ( $c, $upload_id ) = @_;
-
+
$upload_id ||= $c->req->params->{progress_id};
-
+
my $progress = $c->upload_progress( $upload_id );
# there could be a race condition where /progress is called before
@@ -116,20 +116,20 @@
size => -1,
};
}
-
+
# format the progress data as JSON
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
=> 'Thu, 01 Jan 1970 00:00:00 GMT' );
$c->response->headers->header( 'Cache-Control'
=> 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' );
-
+
$c->response->content_type( 'text/x-json' );
$c->response->body( $output );
}
@@ -138,23 +138,23 @@
my $c = shift;
require Catalyst::Plugin::UploadProgress::Static;
-
+
my @output;
push @output,
'<style type="text/css">' . "\n"
. Catalyst::Plugin::UploadProgress::Static->upload_progress_css
. '</style>';
-
+
push @output,
'<script type="text/javascript">' . "\n"
. Catalyst::Plugin::UploadProgress::Static->upload_progress_js
. '</script>';
-
+
push @output,
'<script type="text/javascript">' . "\n"
. Catalyst::Plugin::UploadProgress::Static->upload_progress_jmpl_js
. '</script>';
-
+
return join "\n", @output;
}
@@ -168,7 +168,7 @@
use Catalyst;
MyApp->setup( qw/Static::Simple Cache::FastMmap UploadProgress/ );
-
+
# On the HTML page with the upload form, include the progress
# JavaScript and CSS. These are available via a single method
# if you are lazy.
@@ -177,7 +177,7 @@
[% c.upload_progress_javascript %]
</head>
...
-
+
# For better performance, copy these 3 files from the UploadProgress
# distribution to your static directory and include them normally.
<html>
@@ -187,7 +187,7 @@
<script src="/static/js/progress.jmpl.js" type="text/javascript"></script>
</head>
...
-
+
# Create the upload form with an onsubmit action that creates
# the Ajax progress bar. Note the empty div following the form
# where the progress bar will be inserted.
@@ -195,17 +195,17 @@
method="post"
enctype="multipart/form-data"
onsubmit="return startEmbeddedProgressBar(this)">
-
+
<input type="file" name="file" />
<input type="submit" />
</form>
<div id='progress'></div>
-
+
# No special code is required within your application, just handle
# the upload as usual.
sub upload : Local {
my ( $self, $c ) = @_;
-
+
my $upload = $c->request->uploads->{file};
$upload->copy_to( '/some/path/' . $upload->filename );
}
@@ -249,7 +249,7 @@
size => 110636659,
received => 134983
}
-
+
=head2 upload_progress_output
Returns a JSON response containing the upload progress data.
More information about the Catalyst-commits
mailing list