From 3f293c60d58b554e10b3b1e610dba187a41e7c0e Mon Sep 17 00:00:00 2001 From: Sawyer X Date: Sun, 8 Sep 2019 11:37:23 +0300 Subject: [PATCH] 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. --- lib/Dancer2/Core/App.pm | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/Dancer2/Core/App.pm b/lib/Dancer2/Core/App.pm index 6b64cd527..4c94160b3 100644 --- a/lib/Dancer2/Core/App.pm +++ b/lib/Dancer2/Core/App.pm @@ -1036,7 +1036,6 @@ 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); @@ -1044,17 +1043,23 @@ sub send_file { $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...