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

Commit f6cb1e4

Browse files
authored
Merge pull request #17 from cedricziel/issue-11
Adapter::has should check if a directory object exists
2 parents 8d5a31e + e7c3379 commit f6cb1e4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/GoogleCloudStorageAdapter.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,13 @@ public function has($path)
288288
$path = $this->applyPathPrefix($path);
289289
$object = $this->bucket->object($path);
290290

291-
return $object->exists();
291+
if ($object->exists()) {
292+
return true;
293+
}
294+
295+
// flysystem strips trailing slashes so we need to check
296+
// for directory objects
297+
return $this->bucket->object(sprintf('%s/', $path))->exists();
292298
}
293299

294300
/**

tests/GoogleCloudStorageAdapterTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,26 @@ public function testUrlCanBeCreated(): void
513513

514514
self::assertEquals(sprintf('%s/%s/%s', GoogleCloudStorageAdapter::GCS_BASE_URL, $adapterConfig['bucket'], $objectName), $url);
515515
}
516+
517+
/**
518+
* @see https://github.com/cedricziel/flysystem-gcs/issues/11
519+
*
520+
* @covers \CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter::has()
521+
*/
522+
public function testHasWorksCorrectlyForDirectories(): void
523+
{
524+
$testId = uniqid('', true);
525+
$adapterConfig = [
526+
'bucket' => $this->bucket,
527+
'projectId' => $this->project,
528+
];
529+
530+
$directoryName = sprintf('%s', sprintf('icon-%s', $testId));
531+
532+
$adapter = new GoogleCloudStorageAdapter(null, $adapterConfig);
533+
$fs = new Filesystem($adapter);
534+
$fs->createDir($directoryName);
535+
536+
self::assertTrue($fs->has($directoryName));
537+
}
516538
}

0 commit comments

Comments
 (0)