Skip to content

Commit 0b0fbf8

Browse files
authored
Merge pull request #68 from tinect/feat/preventDirectoryDeletion
feat: prevent delete operation from deleting all contents
2 parents fb44910 + a25e59f commit 0b0fbf8

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/BunnyCDNAdapter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ private function copyFile(string $source, string $destination, Config $config):
450450
*/
451451
public function delete($path): void
452452
{
453+
// if path is empty or ends with /, it's a directory.
454+
if (empty($path) || str_ends_with($path, '/')) {
455+
throw UnableToDeleteFile::atLocation($path, 'Deletion of directories prevented.');
456+
}
457+
453458
try {
454459
$this->client->delete($path);
455460
// @codeCoverageIgnoreStart

tests/ClientTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\DirectoryNotEmptyException;
1111
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\NotFoundException;
1212

13+
if (\is_file(__DIR__.'/ClientDI.php')) {
14+
require_once __DIR__.'/ClientDI.php';
15+
}
16+
1317
class ClientTest extends TestCase
1418
{
1519
public BunnyCDNClient $client;

tests/FlysystemAdapterTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use League\Flysystem\FilesystemAdapter;
1010
use League\Flysystem\FilesystemException;
1111
use League\Flysystem\UnableToCopyFile;
12+
use League\Flysystem\UnableToDeleteFile;
1213
use League\Flysystem\UnableToMoveFile;
1314
use League\Flysystem\UnableToProvideChecksum;
1415
use League\Flysystem\UnableToRetrieveMetadata;
@@ -19,6 +20,10 @@
1920
use PlatformCommunity\Flysystem\BunnyCDN\Util;
2021
use Throwable;
2122

23+
if (\is_file(__DIR__.'/ClientDI.php')) {
24+
require_once __DIR__.'/ClientDI.php';
25+
}
26+
2227
class FlysystemAdapterTest extends FilesystemAdapterTestCase
2328
{
2429
public const DEMOURL = 'https://example.org.local';
@@ -70,6 +75,44 @@ public function generating_a_temporary_url(): void
7075
$this->markTestSkipped('No temporary URL support is provided for BunnyCDN');
7176
}
7277

78+
/**
79+
* @test
80+
*/
81+
public function delete_on_directory_throws_exception(): void
82+
{
83+
$this->runScenario(function () {
84+
$adapter = $this->adapter();
85+
86+
$adapter->write(
87+
'test/text.txt',
88+
'contents',
89+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
90+
);
91+
92+
$this->expectException(UnableToDeleteFile::class);
93+
$adapter->delete('test/');
94+
});
95+
}
96+
97+
/**
98+
* @test
99+
*/
100+
public function delete_with_empty_path_throws_exception(): void
101+
{
102+
$this->runScenario(function () {
103+
$adapter = $this->adapter();
104+
105+
$adapter->write(
106+
'test/text.txt',
107+
'contents',
108+
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
109+
);
110+
111+
$this->expectException(UnableToDeleteFile::class);
112+
$adapter->delete('');
113+
});
114+
}
115+
73116
/**
74117
* @test
75118
*/

tests/PrefixTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
1414
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
1515

16+
if (\is_file(__DIR__.'/ClientDI.php')) {
17+
require_once __DIR__.'/ClientDI.php';
18+
}
19+
1620
class PrefixTest extends FilesystemAdapterTestCase
1721
{
1822
/**

0 commit comments

Comments
 (0)