Skip to content

Commit

Permalink
Improve Path::Tiny usage:
Browse files Browse the repository at this point in the history
Instead of moving from Path::Tiny back to string as early as possible,
we're keeping it for a bit longer.

The defined() and -f checks can be done with Path::Tiny too. Then we
can just call openr_raw() instead of creating another Path::Tiny object.

Once we're done with that, we can finally stringify and go back to string
form.

Eventually, we would want to make all of our internals assume on Path::Tiny.
  • Loading branch information
xsawyerx committed Sep 8, 2019
1 parent 7f3c73f commit 3f293c6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1036,25 +1036,30 @@ sub send_file {
|| $self->config->{public_dir}
|| Path::Tiny::path( $self->location, 'public' )->stringify;

$file_path = Path::Tiny::path( $dir, $path )->stringify;
my $err_response = sub {
my $status = shift;
$self->response->status($status);
$self->response->header( 'Content-Type', 'text/plain' );
$self->response->content( Dancer2::Core::HTTP->status_message($status) );
$self->with_return->( $self->response );
};
$err_response->(403) if !defined $file_path;
$err_response->(404) if !-f $file_path;

$file_path = Path::Tiny::path( $dir, $path );

$err_response->(403) if !$file_path->exists;
$err_response->(404) if !$file_path->is_file;
$err_response->(403) if !-r $file_path;

# Read file content as bytes
$fh = Path::Tiny::path($file_path)->openr_raw();
$fh = $file_path->openr_raw();

$content_type = Dancer2::runner()->mime_type->for_file($file_path) || 'text/plain';
if ( $content_type =~ m!^text/! ) {
$charset = $self->config->{charset} || "utf-8";
}

# cleanup for other functions not assuming on Path::Tiny
$file_path = $file_path->stringify;
}

# Now we are sure we can render the file...
Expand Down

0 comments on commit 3f293c6

Please sign in to comment.