Skip to content

Commit

Permalink
Delete recursively all objects before deleteDir (#94)
Browse files Browse the repository at this point in the history
* Delete recursively all objects inside a dir before deleting a dir.

* Remove array_filter to assure compatibility with php <= 5.5
  • Loading branch information
kainxspirits authored and nicja committed Feb 7, 2019
1 parent 9b20240 commit 9e6ad81
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
Empty file modified composer.json
100644 → 100755
Empty file.
30 changes: 29 additions & 1 deletion src/GoogleStorageAdapter.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,35 @@ public function delete($path)
*/
public function deleteDir($dirname)
{
return $this->delete($this->normaliseDirName($dirname));
$dirname = $this->normaliseDirName($dirname);
$objects = $this->listContents($dirname, true);

// We first delete the file, so that we can delete
// the empty folder at the end.
uasort($objects, function ($a, $b) {
return $b['type'] === 'file' ? 1 : -1;
});

// We remove all objects that should not be deleted.
$filtered_objects = [];
foreach ($objects as $object) {
if (strpos($object['path'], $dirname) !== false) {
$filtered_objects[] = $object;
}
}

// Execute deletion for each object.
foreach ($filtered_objects as $object) {
$path = $object['path'];

if ($object['type'] === 'dir') {
$path = $this->normaliseDirName($path);
}

$this->delete($path);
}

return true;
}

/**
Expand Down
51 changes: 47 additions & 4 deletions tests/GoogleStorageAdapterTests.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,34 @@ public function testDeleteDir()

$storageObject = Mockery::mock(StorageObject::class);
$storageObject->shouldReceive('delete')
->once();
->twice();
$storageObject->shouldReceive('name')
->once()
->andReturn('prefix/dir_name/directory1/file1.txt');
$storageObject->shouldReceive('info')
->once()
->andReturn([
'updated' => '2016-09-26T14:44:42+00:00',
'contentType' => 'text/plain',
'size' => 5,
]);

$bucket->shouldReceive('object')
->with('prefix/dir_name/directory1/file1.txt')
->once()
->andReturn($storageObject);

$bucket->shouldReceive('object')
->with('prefix/dir_name/')
->with('prefix/dir_name/directory1/')
->once()
->andReturn($storageObject);

$bucket->shouldReceive('objects')
->with([
'prefix' => 'prefix/dir_name/'
])->once()
->andReturn([$storageObject]);

$adapter = new GoogleStorageAdapter($storageClient, $bucket, 'prefix');

$adapter->deleteDir('dir_name');
Expand All @@ -372,13 +393,35 @@ public function testDeleteDirWithTrailingSlash()

$storageObject = Mockery::mock(StorageObject::class);
$storageObject->shouldReceive('delete')
->once();
->twice();

$storageObject->shouldReceive('name')
->once()
->andReturn('prefix/dir_name/directory1/file1.txt');
$storageObject->shouldReceive('info')
->once()
->andReturn([
'updated' => '2016-09-26T14:44:42+00:00',
'contentType' => 'text/plain',
'size' => 5,
]);

$bucket->shouldReceive('object')
->with('prefix/dir_name/directory1/file1.txt')
->once()
->andReturn($storageObject);

$bucket->shouldReceive('object')
->with('prefix/dir_name/')
->with('prefix/dir_name/directory1/')
->once()
->andReturn($storageObject);

$bucket->shouldReceive('objects')
->with([
'prefix' => 'prefix/dir_name/'
])->once()
->andReturn([$storageObject]);

$adapter = new GoogleStorageAdapter($storageClient, $bucket, 'prefix');

$adapter->deleteDir('dir_name//');
Expand Down

0 comments on commit 9e6ad81

Please sign in to comment.