Created
December 8, 2011 02:50
-
-
Save peczenyj/1445901 to your computer and use it in GitHub Desktop.
middleware para verificar progresso
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package FileHandleProxy; | |
use base qw/FileHandle/; | |
# constructor | |
sub new { | |
my ($class, %args) = @_; | |
my $self = \%args; | |
bless $self, $class; | |
return $self; | |
} | |
sub read { | |
my $self = shift; | |
my $percentage = 100 * $self->{_read_position} / $self->{content_length}; | |
$self->{out}->print("progresso: $percentage %\n"); | |
my $rc = $self->{fh}->read(@_); | |
$self->{_read_position} += $rc; | |
$rc; | |
} | |
1; | |
package Plack::Middleware::Progress; | |
use strict; | |
use warnings; | |
use parent qw/Plack::Middleware/; | |
sub call { | |
my $self = shift; | |
my $env = shift; | |
$self->_progress($env) if $env->{'CONTENT_TYPE'} =~ /multipart\/form-data/; | |
return $self->app->($env); | |
} | |
sub _progress { | |
my $self = shift; | |
my $env = shift; | |
use Data::Dumper; | |
$env->{'psgi.errors'}->autoflush(); | |
$env->{'psgi.errors'}->print(Dumper($env)); | |
$env->{'psgi.input'} = FileHandleProxy->new( | |
out=> $env->{'psgi.errors'}, | |
fh => $env->{'psgi.input'}, | |
content_length => $env->{'CONTENT_LENGTH'} || 0, | |
_read_position => 0, | |
); | |
1; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment