From 15c246bc62ab0875f756c6378cc81883f803a2ec Mon Sep 17 00:00:00 2001 From: Sugeng Sulistiyawan Date: Fri, 13 Oct 2023 21:01:20 +0700 Subject: [PATCH] Add checksum + fixed bug vars type --- .gitignore | 9 +++++++++ WebDAVAdapter.php | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5069c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +vendor +bin +coverage +coverage.xml +composer.lock +.DS_Store +.idea/ +phpunit.xml +.phpunit.result.cache \ No newline at end of file diff --git a/WebDAVAdapter.php b/WebDAVAdapter.php index cde5a7c..0327571 100644 --- a/WebDAVAdapter.php +++ b/WebDAVAdapter.php @@ -4,6 +4,7 @@ namespace League\Flysystem\WebDAV; +use League\Flysystem\ChecksumProvider; use League\Flysystem\Config; use League\Flysystem\DirectoryAttributes; use League\Flysystem\FileAttributes; @@ -16,6 +17,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; @@ -37,7 +39,7 @@ use function parse_url; use function rawurldecode; -class WebDAVAdapter implements FilesystemAdapter, PublicUrlGenerator +class WebDAVAdapter implements FilesystemAdapter, PublicUrlGenerator, ChecksumProvider { public const ON_VISIBILITY_THROW_ERROR = 'throw'; public const ON_VISIBILITY_IGNORE = 'ignore'; @@ -447,4 +449,18 @@ public function publicUrl(string $path, Config $config): string { return $this->client->getAbsoluteUrl($this->encodePath($this->prefixer->prefixPath($path))); } + + 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; + } }