Skip to content

Commit b05b6f7

Browse files
[FEATURE] Checksums for filesystem disks. (#44660)
* Expose checksum method for filesystem disks. * Added docblock. * Update FilesystemAdapter.php * Update FilesystemAdapter.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4a7bf43 commit b05b6f7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"fruitcake/php-cors": "^1.2",
2525
"laravel/serializable-closure": "^1.2.2",
2626
"league/commonmark": "^2.2",
27-
"league/flysystem": "^3.0.16",
27+
"league/flysystem": "^3.8.0",
2828
"monolog/monolog": "^2.0",
2929
"nesbot/carbon": "^2.62.1",
3030
"nunomaduro/termwind": "^1.13",

src/Illuminate/Filesystem/FilesystemAdapter.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use League\Flysystem\UnableToDeleteDirectory;
2525
use League\Flysystem\UnableToDeleteFile;
2626
use League\Flysystem\UnableToMoveFile;
27+
use League\Flysystem\UnableToProvideChecksum;
2728
use League\Flysystem\UnableToReadFile;
2829
use League\Flysystem\UnableToRetrieveMetadata;
2930
use League\Flysystem\UnableToSetVisibility;
@@ -554,6 +555,24 @@ public function size($path)
554555
return $this->driver->fileSize($path);
555556
}
556557

558+
/**
559+
* Get the checksum for a file.
560+
*
561+
* @return string|false
562+
*
563+
* @throws UnableToProvideChecksum
564+
*/
565+
public function checksum(string $path, array $options = []): string|false
566+
{
567+
try {
568+
return $this->driver->checksum($path, $options);
569+
} catch (UnableToProvideChecksum $e) {
570+
throw_if($this->throwsExceptions(), $e);
571+
572+
return false;
573+
}
574+
}
575+
557576
/**
558577
* Get the mime-type of a given file.
559578
*

tests/Filesystem/FilesystemAdapterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,13 @@ public function testPrefixesUrls()
571571

572572
$this->assertEquals('https://example.org/images/picture.jpeg', $filesystemAdapter->url('picture.jpeg'));
573573
}
574+
575+
public function testGetChecksum()
576+
{
577+
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
578+
$filesystemAdapter->write('path.txt', 'contents of file');
579+
580+
$this->assertEquals('730bed78bccf58c2cfe44c29b71e5e6b', $filesystemAdapter->checksum('path.txt'));
581+
$this->assertEquals('a5c3556d', $filesystemAdapter->checksum('path.txt', ['checksum_algo' => 'crc32']));
582+
}
574583
}

0 commit comments

Comments
 (0)