Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checksum + fixed bug vars type #23

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor
bin
composer.lock
coverage
coverage.xml
tests/files
20 changes: 19 additions & 1 deletion ZipArchiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace League\Flysystem\ZipArchive;

use Generator;
use League\Flysystem\ChecksumProvider;
use League\Flysystem\Config;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
Expand All @@ -15,6 +16,7 @@
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToMoveFile;
use League\Flysystem\UnableToProvideChecksum;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;
use League\Flysystem\UnableToSetVisibility;
Expand All @@ -31,7 +33,7 @@
use function rewind;
use function stream_copy_to_stream;

final class ZipArchiveAdapter implements FilesystemAdapter
final class ZipArchiveAdapter implements FilesystemAdapter, ChecksumProvider
{
private PathPrefixer $pathPrefixer;
private MimeTypeDetector$mimeTypeDetector;
Expand Down Expand Up @@ -211,7 +213,9 @@ public function setVisibility(string $path, string $visibility): void

public function visibility(string $path): FileAttributes
{
/** @var int|null $opsys */
$opsys = null;
/** @var int|null $attr */
$attr = null;
$archive = $this->zipArchiveProvider->createZipArchive();
$archive->getExternalAttributesName(
Expand Down Expand Up @@ -369,6 +373,20 @@ public function copy(string $source, string $destination, Config $config): void
}
}

public function checksum(string $path, Config $config): string
{
$algo = $config->get('checksum_algo', 'md5');
$contents = $this->read($path);
error_clear_last();
$checksum = @hash($algo, $contents);

if ($checksum === false) {
throw new UnableToProvideChecksum(error_get_last()['message'] ?? '', $path);
}

return $checksum;
}

private function ensureParentDirectoryExists(string $path, Config $config): void
{
$dirname = dirname($path);
Expand Down