Skip to content

Commit

Permalink
Fix local disk and Glide base url request scheme resolution
Browse files Browse the repository at this point in the history
876c93a introduced an issue by using request() in config. When using php artisan config:cache that request is not coming externally with the appropriate headers.
  • Loading branch information
ifox committed Apr 1, 2020
1 parent fc5b16a commit 15a2dbe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 0 additions & 3 deletions config/disks.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<?php

$localRootPrefix = storage_path('app/public/');
$localUrlPrefix = request()->getScheme() . '://' . str_replace(['http://', 'https://'], '', env('APP_URL')) . '/storage/';

$mediaLocalConfig = [
'driver' => 'local',
'visibility' => 'public',
'root' => $localRootPrefix . trim(config('twill.media_library.local_path'), '/ '),
'url' => $localUrlPrefix . trim(config('twill.media_library.local_path'), '/ '),
];

$fileLocalConfig = [
'driver' => 'local',
'visibility' => 'public',
'root' => $localRootPrefix . trim(config('twill.file_library.local_path'), '/ '),
'url' => $localUrlPrefix . trim(config('twill.file_library.local_path'), '/ '),
];

$s3Config = [
Expand Down
2 changes: 1 addition & 1 deletion config/glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'source' => env('GLIDE_SOURCE', storage_path('app/public/' . config('twill.media_library.local_path'))),
'cache' => env('GLIDE_CACHE', storage_path('app')),
'cache_path_prefix' => env('GLIDE_CACHE_PATH_PREFIX', 'glide_cache'),
'base_url' => env('GLIDE_BASE_URL', request()->getScheme() . '://' . str_replace(['http://', 'https://'], '', config('app.url'))),
'base_url' => env('GLIDE_BASE_URL', null),
'base_path' => env('GLIDE_BASE_PATH', 'img'),
'use_signed_urls' => env('GLIDE_USE_SIGNED_URLS', false),
'sign_key' => env('GLIDE_SIGN_KEY'),
Expand Down
11 changes: 10 additions & 1 deletion src/Services/MediaLibrary/Glide.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ public function __construct(Config $config, Application $app, Request $request)
$this->app = $app;
$this->request = $request;

$baseUrlHost = $this->config->get(
'twill.glide.base_url',
$this->request->getScheme() . '://' . str_replace(
['http://', 'https://'],
'',
$this->config->get('app.url')
)
);

$baseUrl = join('/', [
rtrim($this->config->get('twill.glide.base_url'), '/'),
rtrim($baseUrlHost, '/'),
ltrim($this->config->get('twill.glide.base_path'), '/'),
]);

Expand Down
22 changes: 22 additions & 0 deletions src/TwillServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,31 @@ private function mergeConfigs()
$this->mergeConfigFrom(__DIR__ . '/../config/dashboard.php', 'twill.dashboard');
$this->mergeConfigFrom(__DIR__ . '/../config/oauth.php', 'twill.oauth');
$this->mergeConfigFrom(__DIR__ . '/../config/disks.php', 'filesystems.disks');

if (config('twill.media_library.endpoint_type') === 'local'
&& config('twill.media_library.disk') === 'twill_media_library') {
$this->setLocalDiskUrl('media');
}

if (config('twill.file_library.endpoint_type') === 'local'
&& config('twill.file_library.disk') === 'twill_file_library') {
$this->setLocalDiskUrl('file');
}

$this->mergeConfigFrom(__DIR__ . '/../config/services.php', 'services');
}

private function setLocalDiskUrl($type)
{
config([
'filesystems.disks.twill_' . $type . '_library.url' => request()->getScheme()
. '://'
. str_replace(['http://', 'https://'], '', config('app.url'))
. '/storage/'
. trim(config('twill.' . $type . '_library.local_path'), '/ '),
]);
}

private function publishMigrations()
{
if (config('twill.load_default_migrations', true)) {
Expand Down

0 comments on commit 15a2dbe

Please sign in to comment.