Set <base href> and use it to get file info url path

This commit is contained in:
Chris Kankiewicz
2020-01-26 22:31:29 -07:00
parent e1c6f1b85c
commit 480986db35
5 changed files with 69 additions and 2 deletions

View File

@@ -56,6 +56,9 @@
};
},
computed: {
baseHref() {
return document.getElementsByTagName('base')[0].href;
},
styles() {
return { 'hidden': ! this.visible };
},
@@ -68,7 +71,7 @@
this.filePath = filePath;
this.visible = true;
await axios.get('/file-info/' + filePath).then(function (response) {
await axios.get(this.baseHref + 'file-info/' + filePath).then(function (response) {
this.hashes = response.data.hashes;
this.loading = false;
}.bind(this)).catch(

View File

@@ -2,8 +2,8 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<base href="{{ base_href() }}">
<link rel="icon" href="{{ config('dark_mode') ? asset('images/favicon.dark.png') : asset('images/favicon.light.png') }}">
<link rel="stylesheet" href="{{ asset('app.css') }}">
<title>{{ path == '.' ? 'Home' : path }} &bull; Directory Lister</title>

View File

@@ -15,6 +15,7 @@ class TwigProvider
/** @const Constant description */
protected const VIEW_FUNCTIONS = [
ViewFunctions\Asset::class,
ViewFunctions\BaseHref::class,
ViewFunctions\Breadcrumbs::class,
ViewFunctions\Config::class,
ViewFunctions\Icon::class,

View File

@@ -0,0 +1,22 @@
<?php
namespace App\ViewFunctions;
class BaseHref extends ViewFunction
{
/** @var string The function name */
protected $name = 'base_href';
/**
* Return the URL for a given path.
*
* @return string
*/
public function __invoke(): string
{
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$baseHref = sprintf('%s://%s%s', $protocol, $_SERVER['HTTP_HOST'] ?? 'localhost', dirname($_SERVER['SCRIPT_NAME']));
return rtrim($baseHref, '/') . '/';
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Tests\ViewFunctions;
use App\ViewFunctions\BaseHref;
use Tests\TestCase;
class BaseHrefTest extends TestCase
{
public function teardown(): void
{
unset($_SERVER['HTTPS'], $_SERVER['SCRIPT_NAME']);
parent::tearDown();
}
public function test_it_can_return_the_base_href(): void
{
$baseHref = new BaseHref($this->container, $this->config);
$this->assertEquals('http://localhost/', $baseHref());
}
public function test_it_return_the_base_href_when_using_https(): void
{
$_SERVER['HTTPS'] = 'on';
$baseHref = new BaseHref($this->container, $this->config);
$this->assertEquals('https://localhost/', $baseHref());
}
public function test_it_can_return_the_base_href_when_in_a_subdirectory(): void
{
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
$baseHref = new BaseHref($this->container, $this->config);
$this->assertEquals('http://localhost/some/dir/', $baseHref());
}
}