Skip to content

Commit

Permalink
feat: prevent delete operation from deleting all contents
Browse files Browse the repository at this point in the history
  • Loading branch information
tinect committed Dec 23, 2023
1 parent fb44910 commit a25e59f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/BunnyCDNAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ private function copyFile(string $source, string $destination, Config $config):
*/
public function delete($path): void
{
// if path is empty or ends with /, it's a directory.
if (empty($path) || str_ends_with($path, '/')) {
throw UnableToDeleteFile::atLocation($path, 'Deletion of directories prevented.');
}

try {
$this->client->delete($path);
// @codeCoverageIgnoreStart
Expand Down
4 changes: 4 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\DirectoryNotEmptyException;
use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\NotFoundException;

if (\is_file(__DIR__.'/ClientDI.php')) {
require_once __DIR__.'/ClientDI.php';
}

class ClientTest extends TestCase
{
public BunnyCDNClient $client;
Expand Down
43 changes: 43 additions & 0 deletions tests/FlysystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToCopyFile;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToRetrieveMetadata;
Expand All @@ -19,6 +20,10 @@
use PlatformCommunity\Flysystem\BunnyCDN\Util;
use Throwable;

if (\is_file(__DIR__.'/ClientDI.php')) {
require_once __DIR__.'/ClientDI.php';
}

class FlysystemAdapterTest extends FilesystemAdapterTestCase
{
public const DEMOURL = 'https://example.org.local';
Expand Down Expand Up @@ -70,6 +75,44 @@ public function generating_a_temporary_url(): void
$this->markTestSkipped('No temporary URL support is provided for BunnyCDN');
}

/**
* @test
*/
public function delete_on_directory_throws_exception(): void
{
$this->runScenario(function () {
$adapter = $this->adapter();

$adapter->write(
'test/text.txt',
'contents',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);

$this->expectException(UnableToDeleteFile::class);
$adapter->delete('test/');
});
}

/**
* @test
*/
public function delete_with_empty_path_throws_exception(): void
{
$this->runScenario(function () {
$adapter = $this->adapter();

$adapter->write(
'test/text.txt',
'contents',
new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC])
);

$this->expectException(UnableToDeleteFile::class);
$adapter->delete('');
});
}

/**
* @test
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/PrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;

if (\is_file(__DIR__.'/ClientDI.php')) {
require_once __DIR__.'/ClientDI.php';
}

class PrefixTest extends FilesystemAdapterTestCase
{
/**
Expand Down

0 comments on commit a25e59f

Please # to comment.