| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace League\Flysystem; |
| 5 |
|
| 6 |
use function hash_final; |
| 7 |
use function hash_init; |
| 8 |
use function hash_update_stream; |
| 9 |
|
| 10 |
trait CalculateChecksumFromStream |
| 11 |
{ |
| 12 |
private function calculateChecksumFromStream(string $path, Config $config): string |
| 13 |
{ |
| 14 |
try { |
| 15 |
$stream = $this->readStream($path); |
| 16 |
$algo = (string) $config->get('checksum_algo', 'md5'); |
| 17 |
$context = hash_init($algo); |
| 18 |
hash_update_stream($context, $stream); |
| 19 |
|
| 20 |
return hash_final($context); |
| 21 |
} catch (FilesystemException $exception) { |
| 22 |
throw new UnableToProvideChecksum($exception->getMessage(), $path, $exception); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @return resource |
| 28 |
*/ |
| 29 |
abstract public function readStream(string $path); |
| 30 |
} |
| 31 |
|