Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Do not apply path prefix when preparing the URL
Browse files Browse the repository at this point in the history
Fixes: #12
  • Loading branch information
cedricziel committed Feb 2, 2021
1 parent adbeffd commit a112f21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/GoogleCloudStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,7 @@ public function getVisibility($path)
*/
public function getUrl($path)
{
$path = $this->applyPathPrefix($path);

return implode('/', [$this->baseUrl, $path]);
return sprintf('%s/%s', rtrim($this->baseUrl, '/'), ltrim($path, '/'));
}

/**
Expand Down
42 changes: 41 additions & 1 deletion tests/GoogleCloudStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function testDeletingNonExistentObjectsWillNotFail(): void

public function testPrefixesCanBeUsed(): void
{
$testId = uniqid();
$testId = uniqid('', true);
$testPrefix = "my/prefix/testPrefixesCanBeUsed-{$testId}/";

$simpleConfig = new Config([]);
Expand Down Expand Up @@ -473,4 +473,44 @@ public function bucketPrefixDataProvider()
['my-bucket/prefix/in/bucket/', '/bar/baz.txt', GoogleCloudStorageAdapter::GCS_BASE_URL.'/my-bucket/prefix/in/bucket/bar/baz.txt'],
];
}

/**
* @see https://github.com/cedricziel/flysystem-gcs/issues/12
*
* @covers \CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter::getUrl()
*/
public function testPrefixIsNotAddedTwiceForUrl(): void
{
$testId = uniqid('', true);
$adapterConfig = [
'bucket' => $this->bucket,
'projectId' => $this->project,
'prefix' => sprintf('icon-%s', $testId),
];

$adapter = new GoogleCloudStorageAdapter(null, $adapterConfig);
$url = $adapter->getUrl('test.txt');

self::assertEquals(sprintf('%s/%s/%s/test.txt', GoogleCloudStorageAdapter::GCS_BASE_URL, $adapterConfig['bucket'], $adapterConfig['prefix']), $url);
}

/**
* @see https://github.com/cedricziel/flysystem-gcs/issues/12
*
* @covers \CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter::getUrl()
*/
public function testUrlCanBeCreated(): void
{
$testId = uniqid('', true);
$adapterConfig = [
'bucket' => $this->bucket,
'projectId' => $this->project,
];

$adapter = new GoogleCloudStorageAdapter(null, $adapterConfig);
$objectName = sprintf('%s/test.txt', sprintf('icon-%s', $testId));
$url = $adapter->getUrl($objectName);

self::assertEquals(sprintf('%s/%s/%s', GoogleCloudStorageAdapter::GCS_BASE_URL, $adapterConfig['bucket'], $objectName), $url);
}
}

0 comments on commit a112f21

Please sign in to comment.