diff --git a/composer.json b/composer.json index 824e30e..61e2651 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "php": ">=7.4", "cakephp/cakephp": "^4.5", "cakephp/plugin-installer": "^1.3", - "knplabs/gaufrette": "^0.11", + "knplabs/gaufrette": "^0.7.0|^0.8.0", "league/mime-type-detection": "^1.15" }, "require-dev": { diff --git a/src/Model/Behavior/FileStorageBehavior.php b/src/Model/Behavior/FileStorageBehavior.php index 6dd9448..c69705f 100644 --- a/src/Model/Behavior/FileStorageBehavior.php +++ b/src/Model/Behavior/FileStorageBehavior.php @@ -10,7 +10,6 @@ */ namespace Burzum\FileStorage\Model\Behavior; -use ArrayAccess; use ArrayObject; use Burzum\FileStorage\Storage\StorageTrait; use Burzum\FileStorage\Storage\StorageUtils; @@ -19,7 +18,6 @@ use Cake\Event\EventDispatcherTrait; use Cake\Event\EventInterface; use Cake\ORM\Behavior; -use Cake\Utility\Text; /** * Storage Behavior @@ -157,7 +155,9 @@ protected function _checkEntityBeforeSave(EntityInterface &$entity): void if ($fileHashMethod === true) { $fileHashMethod = 'sha1'; } - $entity->set('hash', StorageUtils::getFileHash(Text::uuid(), $fileHashMethod)); + /** @var \Psr\Http\Message\UploadedFileInterface $file */ + $file = $entity->get('file'); + $entity->set('hash', StorageUtils::getFileContentHash((string)$file->getStream(), $fileHashMethod)); } } } diff --git a/src/Storage/Listener/BaseListener.php b/src/Storage/Listener/BaseListener.php index 4d2a84a..173810b 100644 --- a/src/Storage/Listener/BaseListener.php +++ b/src/Storage/Listener/BaseListener.php @@ -11,7 +11,6 @@ use Burzum\FileStorage\Storage\StorageUtils; use Cake\Datasource\EntityInterface; use Cake\Event\EventInterface; -use Cake\Utility\Text; use InvalidArgumentException; /** @@ -104,7 +103,11 @@ public function afterDelete(EventInterface $event, EntityInterface $entity): voi public function afterSave(EventInterface $event, EntityInterface $entity): void { if ($this->_checkEvent($event) && $entity->isNew()) { - $hash = StorageUtils::getFileHash(Text::uuid()); + $fileField = $this->getConfig('fileField'); + /** @var \Psr\Http\Message\UploadedFileInterface $file */ + $file = $entity->get($fileField); + + $hash = StorageUtils::getFileContentHash((string)$file->getStream()); $path = $this->pathBuilder()->fullPath($entity); $entity->set('hash', $hash); diff --git a/src/Storage/StorageUtils.php b/src/Storage/StorageUtils.php index 15aeefd..c4b3e08 100644 --- a/src/Storage/StorageUtils.php +++ b/src/Storage/StorageUtils.php @@ -221,27 +221,22 @@ public static function createTmpFile(?string $folder = null, bool $checkAndCreat } /** - * Gets the hash of a file. + * Gets the hash of a file contents. * * You can use this to compare if you got two times the same file uploaded. - * - * @param string $file Path to the file on your local machine. - * @param string $method 'md5' or 'sha1' - * @throws \InvalidArgumentException - * @link http://php.net/manual/en/function.md5-file.php - * @link http://php.net/manual/en/function.sha1-file.php - * @link http://php.net/manual/en/function.sha1-file.php#104748 - * @return string */ - public static function getFileHash(string $file, string $method = 'sha1'): string + public static function getFileContentHash(string $fileContent, string $method = 'sha1'): string { if ($method === 'md5') { - return md5_file($file); + return md5($fileContent); } if ($method === 'sha1') { - return sha1_file($file); + return sha1($fileContent); } - throw new InvalidArgumentException(sprintf('Invalid hash method "%s" provided!', $method)); + throw new InvalidArgumentException(sprintf( + 'Invalid hash method "%s" provided!', + $method + )); } } diff --git a/tests/TestCase/Storage/StorageUtilsTest.php b/tests/TestCase/Storage/StorageUtilsTest.php index 2069813..f58e117 100644 --- a/tests/TestCase/Storage/StorageUtilsTest.php +++ b/tests/TestCase/Storage/StorageUtilsTest.php @@ -245,12 +245,12 @@ public function testFileExtension() * * @return void */ - public function testGetFileHash() + public function testGetFileContentHash() { - $result = StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg'); + $result = StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg')); $this->assertEquals($result, 'd68da24d79835d70d5d8a544f62616d0e51af191'); - $result = StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg', 'md5'); + $result = StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg'), 'md5'); $this->assertEquals($result, '29574141b2c44cc029828f6c5c6d3cd2'); } @@ -259,9 +259,9 @@ public function testGetFileHash() * * @return void */ - public function testGetFileHashInvalidArgumentException() + public function testGetFileContentHashInvalidArgumentException() { $this->expectException(\InvalidArgumentException::class); - StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg', 'invalid-hash-method!'); + StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg'), 'invalid-hash-method!'); } }