From 480986db35ce1e641d61d162e6cb2237bae494f0 Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Sun, 26 Jan 2020 22:31:29 -0700 Subject: [PATCH] Set and use it to get file info url path --- .../js/components/file-info-modal.vue | 5 ++- app/resources/views/layouts/app.twig | 2 +- app/src/Providers/TwigProvider.php | 1 + app/src/ViewFunctions/BaseHref.php | 22 ++++++++++ tests/ViewFunctions/BaseHrefTest.php | 41 +++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 app/src/ViewFunctions/BaseHref.php create mode 100644 tests/ViewFunctions/BaseHrefTest.php diff --git a/app/resources/js/components/file-info-modal.vue b/app/resources/js/components/file-info-modal.vue index c7efa31..44928ac 100644 --- a/app/resources/js/components/file-info-modal.vue +++ b/app/resources/js/components/file-info-modal.vue @@ -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( diff --git a/app/resources/views/layouts/app.twig b/app/resources/views/layouts/app.twig index 6bbd14b..4b8fc80 100644 --- a/app/resources/views/layouts/app.twig +++ b/app/resources/views/layouts/app.twig @@ -2,8 +2,8 @@ + - {{ path == '.' ? 'Home' : path }} • Directory Lister diff --git a/app/src/Providers/TwigProvider.php b/app/src/Providers/TwigProvider.php index 1137d68..e1662cf 100644 --- a/app/src/Providers/TwigProvider.php +++ b/app/src/Providers/TwigProvider.php @@ -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, diff --git a/app/src/ViewFunctions/BaseHref.php b/app/src/ViewFunctions/BaseHref.php new file mode 100644 index 0000000..53c6791 --- /dev/null +++ b/app/src/ViewFunctions/BaseHref.php @@ -0,0 +1,22 @@ +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()); + } +}