Skip to content

Commit

Permalink
removed handle support from Mojo::Asset::File again and fixed small bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 14, 2011
1 parent f288b6d commit 3507c12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,11 +1,11 @@
This file documents the revision history for Perl extension Mojolicious.

2.38 2011-12-14 00:00:00
- Improved Mojo::Asset::File to support file handles.
- Improved documentation. (marcus, sri)
- Improved tests.
- Fixed memory leaks caused by named capture groups bug in Perl.
(plu, sri)
- Fixed small cleanup bug in Mojo::Asset::File.

2.37 2011-12-10 00:00:00
- Welcome to the Mojolicious core team Marcus Ramberg, Glen Hinkle
Expand Down
20 changes: 11 additions & 9 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojo::Asset';
use Carp 'croak';
use Errno;
use Fcntl;
use File::Copy ();
use File::Copy 'move';
use File::Spec;
use IO::File;
use Mojo::Util 'md5_sum';
Expand Down Expand Up @@ -127,26 +127,26 @@ sub move_to {
close $self->handle;
delete $self->{handle};

# Move and prevent clean up of moved file
my $src = $self->path;
File::Copy::move($src, $path)
or croak qq/Can't move file "$src" to "$path": $!/;
# Move file and prevent clean up
my $from = $self->path;
move($from, $path) or croak qq/Can't move file "$from" to "$path": $!/;
$self->path($path)->cleanup(0);

return $self;
}

sub size {
my $self = shift;
return 0 unless defined(my $file = $self->path || $self->handle);
return 0 unless defined(my $file = $self->path);
return -s $file;
}

sub slurp {
my $self = shift;
$self->handle->sysseek(0, SEEK_SET);
my $self = shift;
my $handle = $self->handle;
$handle->sysseek(0, SEEK_SET);
my $content = '';
while ($self->handle->sysread(my $buffer, 131072)) { $content .= $buffer }
while ($handle->sysread(my $buffer, 131072)) { $content .= $buffer }
return $content;
}

Expand All @@ -161,10 +161,12 @@ Mojo::Asset::File - File storage for HTTP 1.1 content
use Mojo::Asset::File;
# Temporary file
my $file = Mojo::Asset::File->new;
$file->add_chunk('foo bar baz');
say $file->slurp;
# Existing file
my $file = Mojo::Asset::File->new(path => '/foo/bar/baz.txt');
say $file->slurp;
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/asset.t
Expand Up @@ -134,14 +134,14 @@ ok !$asset->is_file, 'stored in memory';
$asset = $asset->add_chunk('lala');
ok !$asset->is_file, 'stored in memory';

# Handle
# Temporary file without cleanup
$file = Mojo::Asset::File->new(cleanup => 0)->add_chunk('test');
$path = $file->path;
$file = Mojo::Asset::File->new(handle => $file->handle, cleanup => 1);
ok $file->is_file, 'stored in file';
is $file->slurp, 'test', 'right content';
is $file->size, 4, 'right size';
is $file->contains('es'), 1, '"es" at position 1';
$path = $file->path;
undef $file;
ok -e $path, 'file exists';
unlink $path;
ok !-e $path, 'file has been cleaned up';

0 comments on commit 3507c12

Please sign in to comment.