mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-14 18:14:14 +02:00
Set <base href> and use it to get file info url path
This commit is contained in:
@@ -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(
|
||||
|
@@ -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 }} • Directory Lister</title>
|
||||
|
@@ -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,
|
||||
|
22
app/src/ViewFunctions/BaseHref.php
Normal file
22
app/src/ViewFunctions/BaseHref.php
Normal 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, '/') . '/';
|
||||
}
|
||||
}
|
41
tests/ViewFunctions/BaseHrefTest.php
Normal file
41
tests/ViewFunctions/BaseHrefTest.php
Normal 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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user