From ad2b4cfff0c54df5a6f5c99d4283736043c72696 Mon Sep 17 00:00:00 2001 From: hedii Date: Fri, 31 Jan 2020 09:28:37 +0100 Subject: [PATCH] Add support for a custom public url --- README.md | 3 +- src/OvhSwiftStorageServiceProvider.php | 12 +++++--- tests/OvhSwiftStorageTest.php | 39 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a00e1c3..e583f94 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,11 @@ return [ 'region' => env('OVH_SWIFT_OPENSTACK_REGION'), 'projectId' => env('OVH_SWIFT_OPENSTACK_PROJECT_ID'), 'containerName' => env('OVH_SWIFT_CONTAINER_NAME'), - 'prefix' => env('OVH_SWIFT_PREFIX', null), + 'prefix' => env('OVH_SWIFT_PREFIX'), 'username' => env('OVH_SWIFT_OPENSTACK_USERNAME'), 'password' => env('OVH_SWIFT_OPENSTACK_PASSWORD'), 'visibility' => env('OVH_SWIFT_VISIBILITY', 'public'), + 'public_url' => env('OVH_SWIFT_PUBLIC_URL'), ], ], diff --git a/src/OvhSwiftStorageServiceProvider.php b/src/OvhSwiftStorageServiceProvider.php index 6859d26..9e89633 100644 --- a/src/OvhSwiftStorageServiceProvider.php +++ b/src/OvhSwiftStorageServiceProvider.php @@ -74,11 +74,15 @@ public function boot(FilesystemManager $filesystemManager, CacheRepository $cach private function getContainerPublicUrl(array $config): ?string { if (isset($config['visibility']) && $config['visibility'] === 'public') { - $region = strtolower($config['region']); + if (isset($config['publicUrl']) && $config['publicUrl']) { + $base = $config['publicUrl']; + } else { + $region = strtolower($config['region']); + + $base = "https://storage.{$region}.cloud.ovh.net/v1/AUTH_{$config['projectId']}/{$config['containerName']}"; + } - return (isset($config['prefix']) && $config['prefix']) - ? "https://storage.{$region}.cloud.ovh.net/v1/AUTH_{$config['projectId']}/{$config['containerName']}/{$config['prefix']}" - : "https://storage.{$region}.cloud.ovh.net/v1/AUTH_{$config['projectId']}/{$config['containerName']}"; + return (isset($config['prefix']) && $config['prefix']) ? "{$base}/{$config['prefix']}" : $base; } return null; diff --git a/tests/OvhSwiftStorageTest.php b/tests/OvhSwiftStorageTest.php index 647e9cf..c76c877 100644 --- a/tests/OvhSwiftStorageTest.php +++ b/tests/OvhSwiftStorageTest.php @@ -90,6 +90,44 @@ public function it_should_not_create_a_public_file_if_visibility_is_not_public() Storage::url($fileName); } + /** @test */ + public function it_should_use_a_custom_public_url_if_needed(): void + { + $this->app['config']->set('filesystems.disks.ovh-swift', $this->getOvhSwiftConfiguration([ + 'publicUrl' => 'http://foo.example.com', + 'prefix' => null + ])); + + $fileName = $this->randomFileName(); + $content = $this->randomContent(); + + Storage::put($fileName, $content); + + $this->assertSame( + "http://foo.example.com/{$fileName}", + Storage::url($fileName) + ); + } + + /** @test */ + public function it_should_preserve_prefix_with_a_custom_public_url(): void + { + $this->app['config']->set('filesystems.disks.ovh-swift', $this->getOvhSwiftConfiguration([ + 'publicUrl' => 'http://foo.example.com', + 'prefix' => 'a-prefix' + ])); + + $fileName = $this->randomFileName(); + $content = $this->randomContent(); + + Storage::put($fileName, $content); + + $this->assertSame( + "http://foo.example.com/a-prefix/{$fileName}", + Storage::url($fileName) + ); + } + protected function getOvhSwiftConfiguration(array $config = []): array { $baseConfig = [ @@ -102,6 +140,7 @@ protected function getOvhSwiftConfiguration(array $config = []): array 'username' => env('OVH_SWIFT_OPENSTACK_USERNAME'), 'password' => env('OVH_SWIFT_OPENSTACK_PASSWORD'), 'visibility' => env('OVH_SWIFT_VISIBILITY'), + 'publicUrl' => env('OVH_SWIFT_PUBLIC_URL'), ]; return array_merge($baseConfig, $config);