From e7c337922d17e0ad9fb80239c3c93c8eea04fb2e Mon Sep 17 00:00:00 2001 From: Cedric Ziel Date: Tue, 2 Feb 2021 23:54:04 +0100 Subject: [PATCH] Adapter::has should check if a directory object exists This fixes an issue where Flysystem would strip trailing slashes from the object path. Fixes: #11 --- src/GoogleCloudStorageAdapter.php | 8 +++++++- tests/GoogleCloudStorageAdapterTest.php | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/GoogleCloudStorageAdapter.php b/src/GoogleCloudStorageAdapter.php index f39d429..1385c3f 100644 --- a/src/GoogleCloudStorageAdapter.php +++ b/src/GoogleCloudStorageAdapter.php @@ -288,7 +288,13 @@ public function has($path) $path = $this->applyPathPrefix($path); $object = $this->bucket->object($path); - return $object->exists(); + if ($object->exists()) { + return true; + } + + // flysystem strips trailing slashes so we need to check + // for directory objects + return $this->bucket->object(sprintf('%s/', $path))->exists(); } /** diff --git a/tests/GoogleCloudStorageAdapterTest.php b/tests/GoogleCloudStorageAdapterTest.php index a2c6dd1..946676d 100644 --- a/tests/GoogleCloudStorageAdapterTest.php +++ b/tests/GoogleCloudStorageAdapterTest.php @@ -513,4 +513,26 @@ public function testUrlCanBeCreated(): void self::assertEquals(sprintf('%s/%s/%s', GoogleCloudStorageAdapter::GCS_BASE_URL, $adapterConfig['bucket'], $objectName), $url); } + + /** + * @see https://github.com/cedricziel/flysystem-gcs/issues/11 + * + * @covers \CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter::has() + */ + public function testHasWorksCorrectlyForDirectories(): void + { + $testId = uniqid('', true); + $adapterConfig = [ + 'bucket' => $this->bucket, + 'projectId' => $this->project, + ]; + + $directoryName = sprintf('%s', sprintf('icon-%s', $testId)); + + $adapter = new GoogleCloudStorageAdapter(null, $adapterConfig); + $fs = new Filesystem($adapter); + $fs->createDir($directoryName); + + self::assertTrue($fs->has($directoryName)); + } }