Superbalist / flysystem-google-cloud-storage

Flysystem Adapter for Google Cloud Storage
MIT License
265 stars 105 forks source link

GoogleStorageAdapter::deleteDir fails to delete a directory #96

Closed jhaboush closed 5 years ago

jhaboush commented 5 years ago

I'm unable to delete a directory on Cloud Storage bucket since the update to 7.2.0 My application worked fine with v7.1.0 I believe this was introduced by PR#94. To illustrate the issue, please run the following snippet after doing composer require superbalist/flysystem-google-cloud-storage.

require_once __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;

$storageClient = new StorageClient([
    'projectId' => '<valid-project-id>',
    'keyFilePath' => '</path/to/service/account/file.json>',
]);
$bucket = $storageClient->bucket('<bucket-name>');
$adapter = new GoogleStorageAdapter($storageClient, $bucket);
$filesystem = new Filesystem($adapter);
$filesystem->createDir('test');
$filesystem->put('test/file.txt', 'contents');

// The above works fine. but here this call deletes the file, but fails to delete
// directory `test` from the bucket.
$filesystem->deleteDir('test');

I inspected the changes to ::deleteDir method from said PR and I think the issue lies at line264. Because at that point $object['path'] for the case where $object['type'] === 'dir' isn't normalised, while $dirname is, therefore the directory we request to delete is never added to $filtered_objects. To verify, I edited that loop to look like:

$filtered_objects = [];
foreach ($objects as $object) {
    if ($object['type'] === 'dir') { // normalise path for directories
        $object['path'] = $this->normaliseDirName($object['path']);
    }
    if (strpos($object['path'], $dirname) !== false) {
        $filtered_objects[] = $object;
    }
}

This seems to work for me. I have now locked this dependency at v7.1.0 to continue using it. Thank you.

nicja commented 5 years ago

This will be addressed in https://github.com/Superbalist/flysystem-google-cloud-storage/pull/97

nicja commented 5 years ago

Fixed in https://github.com/Superbalist/flysystem-google-cloud-storage/releases/tag/7.2.1