diff --git a/src/Filesystem.php b/src/Filesystem.php index b98208ac4..4e9a984a3 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -19,7 +19,7 @@ class Filesystem implements FilesystemOperator public function __construct( FilesystemAdapter $adapter, array $config = [], - PathNormalizer $pathNormalizer = null + PathNormalizer $pathNormalizer = null, ) { $this->adapter = $adapter; $this->config = new Config($config); @@ -163,6 +163,19 @@ public function publicUrl(string $path, array $config = []): string return $this->publicUrlGenerator->publicUrl($path, $config); } + private function resolvePublicUrlGenerator(): ?PublicUrlGenerator + { + if ($publicUrl = $this->config->get('public_url')) { + return new PrefixPublicUrlGenerator($publicUrl); + } + + if ($this->adapter instanceof PublicUrlGenerator) { + return $this->adapter; + } + + return null; + } + /** * @param mixed $contents */ @@ -188,17 +201,4 @@ private function rewindStream($resource): void rewind($resource); } } - - private function resolvePublicUrlGenerator(): ?PublicUrlGenerator - { - if ($publicUrl = $this->config->get('public_url')) { - return new PrefixPublicUrlGenerator($publicUrl); - } - - if ($this->adapter instanceof PublicUrlGenerator) { - return $this->adapter; - } - - return null; - } } diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php index 4b28abf2c..d177d3ffd 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php @@ -23,6 +23,7 @@ use League\Flysystem\UnableToRetrieveMetadata; use League\Flysystem\UnableToSetVisibility; use League\Flysystem\UnableToWriteFile; +use League\Flysystem\UrlGeneration\PublicUrlGenerator; use League\Flysystem\Visibility; use League\MimeTypeDetection\FinfoMimeTypeDetector; use League\MimeTypeDetection\MimeTypeDetector; @@ -34,7 +35,7 @@ use function sprintf; use function strlen; -class GoogleCloudStorageAdapter implements FilesystemAdapter +class GoogleCloudStorageAdapter implements FilesystemAdapter, PublicUrlGenerator { /** * @var Bucket @@ -75,6 +76,13 @@ public function __construct( $this->mimeTypeDetector = $mimeTypeDetector ?: new FinfoMimeTypeDetector(); } + public function publicUrl(string $path, Config $config): string + { + $location = $this->prefixer->prefixPath($path); + + return 'https://storage.googleapis.com/' . $this->bucket->name() . '/' . ltrim($location, '/'); + } + public function fileExists(string $path): bool { $prefixedPath = $this->prefixer->prefixPath($path); diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php index de0893b18..96420abdc 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php @@ -13,6 +13,8 @@ use League\Flysystem\UnableToRetrieveMetadata; use League\Flysystem\UnableToWriteFile; +use function file_get_contents; + /** * @group gcs */ @@ -161,4 +163,19 @@ public function failing_to_retrieve_visibility(): void $adapter->visibility('filename.txt'); } + + /** + * @test + */ + public function generating_a_public_url(): void + { + /** @var GoogleCloudStorageAdapter $adapter */ + $adapter = $this->adapter(); + $adapter->write('some/path.txt', 'public contents', new Config(['visibility' => 'public'])); + + $url = $adapter->publicUrl('some/path.txt', new Config()); + $contents = file_get_contents($url); + + self::assertEquals('public contents', $contents); + } }