Skip to content

Commit

Permalink
[FEATURE] Checksums for filesystem disks. (laravel#44660)
Browse files Browse the repository at this point in the history
* Expose checksum method for filesystem disks.

* Added docblock.

* Update FilesystemAdapter.php

* Update FilesystemAdapter.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
frankdejonge and taylorotwell authored Oct 19, 2022
1 parent 4a7bf43 commit b05b6f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"fruitcake/php-cors": "^1.2",
"laravel/serializable-closure": "^1.2.2",
"league/commonmark": "^2.2",
"league/flysystem": "^3.0.16",
"league/flysystem": "^3.8.0",
"monolog/monolog": "^2.0",
"nesbot/carbon": "^2.62.1",
"nunomaduro/termwind": "^1.13",
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 Down Expand Up @@ -554,6 +555,24 @@ public function size($path)
return $this->driver->fileSize($path);
}

/**
* Get the checksum for a file.
*
* @return string|false
*
* @throws UnableToProvideChecksum
*/
public function checksum(string $path, array $options = []): string|false
{
try {
return $this->driver->checksum($path, $options);
} catch (UnableToProvideChecksum $e) {
throw_if($this->throwsExceptions(), $e);

return false;
}
}

/**
* Get the mime-type of a given file.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,13 @@ public function testPrefixesUrls()

$this->assertEquals('https://example.org/images/picture.jpeg', $filesystemAdapter->url('picture.jpeg'));
}

public function testGetChecksum()
{
$filesystemAdapter = new FilesystemAdapter($this->filesystem, $this->adapter);
$filesystemAdapter->write('path.txt', 'contents of file');

$this->assertEquals('730bed78bccf58c2cfe44c29b71e5e6b', $filesystemAdapter->checksum('path.txt'));
$this->assertEquals('a5c3556d', $filesystemAdapter->checksum('path.txt', ['checksum_algo' => 'crc32']));
}
}

0 comments on commit b05b6f7

Please sign in to comment.