Skip to content

Commit

Permalink
Replace merge_paths with Path::Tiny:
Browse files Browse the repository at this point in the history
This was a bit hard to figure out, but with the help of @wchristian,
it seems as though this merges path from argument 1 onto argument 2.

I have this replaced it with a single path() call. All tests pass,
so I hope this is okay. We should run these tests under Windows to
make sure it works there as well.
  • Loading branch information
xsawyerx committed Mar 1, 2017
1 parent 9b29aca commit 27be625
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/Dancer2/Core/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ sub send_file {
|| $self->config->{public_dir}
|| Path::Tiny::path( $self->location, 'public' )->stringify;

$file_path = Dancer2::Handler::File->merge_paths( $path, $dir );
$file_path = Path::Tiny::path( $dir, $path )->stringify;
my $err_response = sub {
my $status = shift;
$self->response->status($status);
Expand Down
20 changes: 1 addition & 19 deletions lib/Dancer2/Handler/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sub code {
$path =~ s/^\Q$prefix\E//;
}

my $file_path = $self->merge_paths( $path, $self->public_dir );
my $file_path = Path::Tiny::path( $self->public_dir, $path )->stringify;
return $self->standard_response( $app, 403 ) if !defined $file_path;

if ( !-f $file_path ) {
Expand Down Expand Up @@ -130,22 +130,4 @@ sub code {
};
}

sub merge_paths {
my ( undef, $path, $public_dir ) = @_;

my ( $volume, $dirs, $file ) = File::Spec->splitpath( $path );
my @tokens = File::Spec->splitdir( "$dirs$file" );
my $updir = File::Spec->updir;
return if grep $_ eq $updir, @tokens;

my ( $pub_vol, $pub_dirs, $pub_file ) = File::Spec->splitpath( $public_dir );
my @pub_tokens = File::Spec->splitdir( "$pub_dirs$pub_file" );
return if length $volume and length $pub_vol and $volume ne $pub_vol;

my @final_vol = ( length $pub_vol ? $pub_vol : length $volume ? $volume : () );
my @file_path = ( @final_vol, @pub_tokens, @tokens );
my $file_path = path( @file_path );
return $file_path;
}

1;

0 comments on commit 27be625

Please sign in to comment.