Skip to content

Commit fb44910

Browse files
authored
Merge pull request #66 from tinect/feat/reuseChecksum
feat: reuse checksum of bunny.net default to sha256
2 parents 05d39c4 + 1aade40 commit fb44910

File tree

6 files changed

+105
-9
lines changed

6 files changed

+105
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require": {
12-
"league/flysystem": "^3.7",
12+
"league/flysystem": "^3.16",
1313
"ext-json": "*",
1414
"guzzlehttp/guzzle": "^7.4",
1515
"league/mime-type-detection": "^1.11"

src/BunnyCDNAdapter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use League\Flysystem\UnableToDeleteDirectory;
2020
use League\Flysystem\UnableToDeleteFile;
2121
use League\Flysystem\UnableToMoveFile;
22+
use League\Flysystem\UnableToProvideChecksum;
2223
use League\Flysystem\UnableToReadFile;
2324
use League\Flysystem\UnableToRetrieveMetadata;
2425
use League\Flysystem\UnableToSetVisibility;
@@ -493,7 +494,26 @@ public function fileExists(string $path): bool
493494
*/
494495
public function checksum(string $path, Config $config): string
495496
{
496-
return $this->calculateChecksumFromStream($path, $config);
497+
// for compatibility reasons, the default checksum algorithm is md5
498+
$algo = $config->get('checksum_algo', 'md5');
499+
500+
if ($algo !== 'sha256') {
501+
return $this->calculateChecksumFromStream($path, $config);
502+
}
503+
504+
try {
505+
$file = $this->getObject($path);
506+
} catch (UnableToReadFile $exception) {
507+
throw new UnableToProvideChecksum($exception->reason(), $path, $exception);
508+
}
509+
510+
$metaData = $file->extraMetadata();
511+
512+
if (empty($metaData['checksum']) || ! is_string($metaData['checksum'])) {
513+
throw new UnableToProvideChecksum('Checksum not available.', $path);
514+
}
515+
516+
return \strtolower($metaData['checksum']);
497517
}
498518

499519
/**

tests/ClientDI_Example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
44

5+
global $storage_zone;
6+
global $api_key;
7+
global $region;
8+
59
// $storage_zone = 'testing_storage_zone';
610
// $api_key = 'testing_api_key';
711
// $region = BunnyCDNRegion::DEFAULT;

tests/FlysystemTest.php renamed to tests/FlysystemAdapterTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22

33
namespace PlatformCommunity\Flysystem\BunnyCDN\Tests;
44

5+
use Faker\Factory;
56
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
67
use League\Flysystem\Config;
78
use League\Flysystem\Filesystem;
89
use League\Flysystem\FilesystemAdapter;
910
use League\Flysystem\FilesystemException;
1011
use League\Flysystem\UnableToCopyFile;
1112
use League\Flysystem\UnableToMoveFile;
13+
use League\Flysystem\UnableToProvideChecksum;
1214
use League\Flysystem\UnableToRetrieveMetadata;
1315
use League\Flysystem\Visibility;
1416
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
1517
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
1618
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNRegion;
19+
use PlatformCommunity\Flysystem\BunnyCDN\Util;
1720
use Throwable;
1821

19-
class FlysystemTest extends FilesystemAdapterTestCase
22+
class FlysystemAdapterTest extends FilesystemAdapterTestCase
2023
{
2124
public const DEMOURL = 'https://example.org.local';
2225

@@ -215,6 +218,71 @@ public function overwriting_a_file(): void
215218
});
216219
}
217220

221+
/**
222+
* @test
223+
*/
224+
public function get_checksum(): void
225+
{
226+
$adapter = $this->adapter();
227+
228+
$adapter->write('path.txt', 'foobar', new Config());
229+
230+
$this->assertSame(
231+
'3858f62230ac3c915f300c664312c63f',
232+
$adapter->checksum('path.txt', new Config())
233+
);
234+
235+
$this->assertSame(
236+
'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2',
237+
$adapter->checksum('path.txt', new Config(['checksum_algo' => 'sha256']))
238+
);
239+
}
240+
241+
public function test_checksum_throws_error_with_non_existing_file_on_default_algo(): void
242+
{
243+
$adapter = $this->adapter();
244+
245+
$this->expectException(UnableToProvideChecksum::class);
246+
$adapter->checksum('path.txt', new Config(['checksum_algo' => 'sha256']));
247+
}
248+
249+
//test_checksum_throws_error_with_empty_checksum_from_client
250+
public function test_checksum_throws_error_with_empty_checksum_from_client(): void
251+
{
252+
$client = $this->createMock(BunnyCDNClient::class);
253+
$client->expects(self::exactly(1))->method('list')->willReturnCallback(
254+
function () {
255+
['file' => $file, 'dir' => $dir] = Util::splitPathIntoDirectoryAndFile('file.txt');
256+
$dir = Util::normalizePath($dir);
257+
$faker = Factory::create();
258+
$storage_zone = $faker->word;
259+
260+
return [[
261+
'Guid' => $faker->uuid,
262+
'StorageZoneName' => $storage_zone,
263+
'Path' => Util::normalizePath('/'.$storage_zone.'/'.$dir.'/'),
264+
'ObjectName' => $file,
265+
'Length' => $faker->numberBetween(0, 10240),
266+
'LastChanged' => date('Y-m-d\TH:i:s.v'),
267+
'ServerId' => $faker->numberBetween(0, 10240),
268+
'ArrayNumber' => 0,
269+
'IsDirectory' => false,
270+
'UserId' => 'bf91bc4e-0e60-411a-b475-4416926d20f7',
271+
'ContentType' => '',
272+
'DateCreated' => date('Y-m-d\TH:i:s.v'),
273+
'StorageZoneId' => $faker->numberBetween(0, 102400),
274+
'Checksum' => null,
275+
'ReplicatedZones' => '',
276+
]];
277+
}
278+
);
279+
280+
$adapter = new BunnyCDNAdapter($client);
281+
$this->expectException(UnableToProvideChecksum::class);
282+
$this->expectExceptionMessage('Unable to get checksum for file.txt: Checksum not available.');
283+
$adapter->checksum('file.txt', new Config(['checksum_algo' => 'sha256']));
284+
}
285+
218286
/**
219287
* @test
220288
*/

tests/MockClient.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function list(string $path): array
3939
? self::example_folder($file->path(), $this->storage_zone_name, [])
4040
: self::example_file($file->path(), $this->storage_zone_name, [
4141
'Length' => $file->fileSize(),
42+
'Checksum' => hash('sha256', $this->filesystem->read($file->path())),
4243
]);
4344
})->toArray();
4445
} catch (FilesystemException $exception) {

tests/PrefixTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use League\Flysystem\Visibility;
1313
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter;
1414
use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient;
15-
use PlatformCommunity\Flysystem\BunnyCDN\Util;
1615

1716
class PrefixTest extends FilesystemAdapterTestCase
1817
{
@@ -99,10 +98,6 @@ public function overwriting_a_file(): void
9998
}
10099

101100
/**
102-
* This seems to be a bug in flysystem's path prefixer, same with temporary URLs
103-
* Opened https://github.com/thephpleague/flysystem/pull/1595 to fix it over there. Below is the fix for here.
104-
* TODO Remove when merged and update lockfile
105-
*
106101
* @test
107102
*/
108103
public function get_checksum(): void
@@ -115,7 +110,15 @@ public function get_checksum(): void
115110

116111
$adapter->write('path.txt', 'foobar', new Config());
117112

118-
$this->assertSame('3858f62230ac3c915f300c664312c63f', $adapter->checksum(Util::normalizePath(self::PREFIX_PATH.'/path.txt'), new Config()));
113+
$this->assertSame(
114+
'3858f62230ac3c915f300c664312c63f',
115+
$adapter->checksum('path.txt', new Config())
116+
);
117+
118+
$this->assertSame(
119+
'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2',
120+
$adapter->checksum('path.txt', new Config(['checksum_algo' => 'sha256']))
121+
);
119122
}
120123

121124
public function test_construct_throws_error(): void

0 commit comments

Comments
 (0)