Skip to content

Commit 3f293c6

Browse files
committed
Improve Path::Tiny usage:
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.
1 parent 7f3c73f commit 3f293c6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

lib/Dancer2/Core/App.pm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,25 +1036,30 @@ sub send_file {
10361036
|| $self->config->{public_dir}
10371037
|| Path::Tiny::path( $self->location, 'public' )->stringify;
10381038

1039-
$file_path = Path::Tiny::path( $dir, $path )->stringify;
10401039
my $err_response = sub {
10411040
my $status = shift;
10421041
$self->response->status($status);
10431042
$self->response->header( 'Content-Type', 'text/plain' );
10441043
$self->response->content( Dancer2::Core::HTTP->status_message($status) );
10451044
$self->with_return->( $self->response );
10461045
};
1047-
$err_response->(403) if !defined $file_path;
1048-
$err_response->(404) if !-f $file_path;
1046+
1047+
$file_path = Path::Tiny::path( $dir, $path );
1048+
1049+
$err_response->(403) if !$file_path->exists;
1050+
$err_response->(404) if !$file_path->is_file;
10491051
$err_response->(403) if !-r $file_path;
10501052

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

10541056
$content_type = Dancer2::runner()->mime_type->for_file($file_path) || 'text/plain';
10551057
if ( $content_type =~ m!^text/! ) {
10561058
$charset = $self->config->{charset} || "utf-8";
10571059
}
1060+
1061+
# cleanup for other functions not assuming on Path::Tiny
1062+
$file_path = $file_path->stringify;
10581063
}
10591064

10601065
# Now we are sure we can render the file...

0 commit comments

Comments
 (0)