Skip to content

Commit

Permalink
Fix PathPrefixedAdapter checksum
Browse files Browse the repository at this point in the history
* Add the missing prefix path in the first case (instanceof ChecksumProvider)
* Remove unwanted double prefix in the second case (`prefixPath` method already called in `readStream`, itself called by `calculateChecksumFromStream`)
  • Loading branch information
smnandre committed Jul 27, 2023
1 parent b4d2b9d commit d80b882
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PathPrefixing/PathPrefixedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ public function publicUrl(string $path, Config $config): string
public function checksum(string $path, Config $config): string
{
if ($this->adapter instanceof ChecksumProvider) {
return $this->adapter->checksum($path, $config);
return $this->adapter->checksum($this->prefix->prefixPath($path), $config);
}

return $this->calculateChecksumFromStream($this->prefix->prefixPath($path), $config);
return $this->calculateChecksumFromStream($path, $config);
}

public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
Expand Down
32 changes: 32 additions & 0 deletions src/PathPrefixing/PathPrefixedAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\Flysystem\PathPrefixing;

use League\Flysystem\ChecksumProvider;
use League\Flysystem\Config;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use League\Flysystem\UnableToGeneratePublicUrl;
Expand Down Expand Up @@ -95,6 +96,37 @@ public function publicUrl(string $path, Config $config): string
self::assertEquals('memory://prefix/path.txt', $url);
}

/**
* @test
*/
public function calculate_checksum_using_decorated_adapter(): void
{
$adapter = new class() extends InMemoryFilesystemAdapter implements ChecksumProvider {
public function checksum(string $path, Config $config): string
{
return hash('md5', $this->read($path));
}
};

$prefixedAdapter = new PathPrefixedAdapter($adapter, 'prefix');
$prefixedAdapter->write('foo.txt', 'bla', new Config);

self::assertEquals('128ecf542a35ac5270a87dc740918404', $prefixedAdapter->checksum('foo.txt', new Config()));
}

/**
* @test
*/
public function calculate_checksum_using_current_adapter(): void
{
$adapter = new InMemoryFilesystemAdapter();
$prefixedAdapter = new PathPrefixedAdapter($adapter, 'prefix');
$prefixedAdapter->write('foo.txt', 'bla', new Config);

self::assertEquals('128ecf542a35ac5270a87dc740918404', hash('md5', 'bla'));
self::assertEquals('128ecf542a35ac5270a87dc740918404', $prefixedAdapter->checksum('foo.txt', new Config()));
}

/**
* @test
*/
Expand Down

0 comments on commit d80b882

Please sign in to comment.