Skip to content

Commit

Permalink
Public URLs for GCS
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Oct 5, 2022
1 parent d2e2497 commit e5968d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
}
}
10 changes: 9 additions & 1 deletion src/GoogleCloudStorage/GoogleCloudStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +35,7 @@
use function sprintf;
use function strlen;

class GoogleCloudStorageAdapter implements FilesystemAdapter
class GoogleCloudStorageAdapter implements FilesystemAdapter, PublicUrlGenerator
{
/**
* @var Bucket
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToWriteFile;

use function file_get_contents;

/**
* @group gcs
*/
Expand Down Expand Up @@ -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);
}
}

0 comments on commit e5968d9

Please sign in to comment.