Skip to content

Commit

Permalink
Add support for a custom public url
Browse files Browse the repository at this point in the history
  • Loading branch information
hedii committed Jan 31, 2020
1 parent ba06a3c commit ad2b4cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
],

],
Expand Down
12 changes: 8 additions & 4 deletions src/OvhSwiftStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions tests/OvhSwiftStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
Expand Down

0 comments on commit ad2b4cf

Please sign in to comment.