From ca4a7c59cca512739f7f24ba6bd046aa9b41ff69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20M=C3=A9tayer?= Date: Thu, 22 Apr 2021 17:43:44 +0200 Subject: [PATCH 1/2] Setting metadata when uploading an object into a GCS bucket --- src/GoogleCloudStorage/GoogleCloudStorageAdapter.php | 5 ++++- .../GoogleCloudStorageAdapterTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php index 7bc6a3e60..f26c360dd 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php @@ -77,7 +77,10 @@ public function writeStream(string $path, $contents, Config $config): void private function upload(string $path, $contents, Config $config): void { $prefixedPath = $this->prefixer->prefixPath($path); - $options = ['name' => $prefixedPath]; + $options = [ + 'name' => $prefixedPath, + 'metadata' => $config->get('metadata', []), + ]; $visibility = $config->get(Config::OPTION_VISIBILITY, $this->defaultVisibility); $predefinedAcl = $this->visibilityHandler->visibilityToPredefinedAcl($visibility); diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php index f86ff6777..c163f470d 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php @@ -54,6 +54,17 @@ protected static function createFilesystemAdapter(): FilesystemAdapter return new GoogleCloudStorageAdapter($bucket, static::$adapterPrefix); } + /** + * @test + */ + public function writing_with_specific_metadata(): void + { + $adapter = $this->adapter(); + $adapter->write('some/path.txt', 'contents', new Config(['metadata' => ['contentType' => 'text/plain+special']])); + $mimeType = $adapter->mimeType('some/path.txt')->mimeType(); + $this->assertEquals('text/plain+special', $mimeType); + } + /** * @test */ From a63505252b747e27f1323361bab023819152a58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20M=C3=A9tayer?= Date: Tue, 31 Aug 2021 17:59:19 +0200 Subject: [PATCH 2/2] Guess the file mime type (if not provided) when uploading --- .../GoogleCloudStorageAdapter.php | 29 +++++++++++++++---- .../GoogleCloudStorageAdapterTest.php | 11 +++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php index f26c360dd..2a47f4f2c 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapter.php @@ -22,6 +22,8 @@ use League\Flysystem\UnableToSetVisibility; use League\Flysystem\UnableToWriteFile; use League\Flysystem\Visibility; +use League\MimeTypeDetection\FinfoMimeTypeDetector; +use League\MimeTypeDetection\MimeTypeDetector; use Throwable; class GoogleCloudStorageAdapter implements FilesystemAdapter @@ -46,12 +48,23 @@ class GoogleCloudStorageAdapter implements FilesystemAdapter */ private $defaultVisibility; - public function __construct(Bucket $bucket, string $prefix = '', VisibilityHandler $visibilityHandler = null, string $defaultVisibility = Visibility::PRIVATE) - { + /** + * @var MimeTypeDetector + */ + private $mimeTypeDetector; + + public function __construct( + Bucket $bucket, + string $prefix = '', + VisibilityHandler $visibilityHandler = null, + string $defaultVisibility = Visibility::PRIVATE, + MimeTypeDetector $mimeTypeDetector = null + ) { $this->bucket = $bucket; $this->prefixer = new PathPrefixer($prefix); $this->visibilityHandler = $visibilityHandler ?: new PortableVisibilityHandler(); $this->defaultVisibility = $defaultVisibility; + $this->mimeTypeDetector = $mimeTypeDetector ?: new FinfoMimeTypeDetector(); } public function fileExists(string $path): bool @@ -77,10 +90,7 @@ public function writeStream(string $path, $contents, Config $config): void private function upload(string $path, $contents, Config $config): void { $prefixedPath = $this->prefixer->prefixPath($path); - $options = [ - 'name' => $prefixedPath, - 'metadata' => $config->get('metadata', []), - ]; + $options = ['name' => $prefixedPath]; $visibility = $config->get(Config::OPTION_VISIBILITY, $this->defaultVisibility); $predefinedAcl = $this->visibilityHandler->visibilityToPredefinedAcl($visibility); @@ -89,6 +99,13 @@ private function upload(string $path, $contents, Config $config): void $options['predefinedAcl'] = $predefinedAcl; } + $metadata = $config->get('metadata', []); + $shouldDetermineMimetype = $contents !== '' && ! array_key_exists('contentType', $metadata); + if ($shouldDetermineMimetype && $mimeType = $this->mimeTypeDetector->detectMimeType($path, $contents)) { + $metadata['contentType'] = $mimeType; + } + $options['metadata'] = $metadata; + try { $this->bucket->upload($contents, $options); } catch (Throwable $exception) { diff --git a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php index c163f470d..f6164d5b3 100644 --- a/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php +++ b/src/GoogleCloudStorage/GoogleCloudStorageAdapterTest.php @@ -65,6 +65,17 @@ public function writing_with_specific_metadata(): void $this->assertEquals('text/plain+special', $mimeType); } + /** + * @test + */ + public function guessing_the_mime_type_when_writing(): void + { + $adapter = $this->adapter(); + $adapter->write('some/config.txt', '', new Config()); + $mimeType = $adapter->mimeType('some/config.txt')->mimeType(); + $this->assertEquals('text/xml', $mimeType); + } + /** * @test */