Skip to content

Commit

Permalink
fixed deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Aug 14, 2024
1 parent 022b9c0 commit 1027897
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Behavior/FileStorageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
namespace Burzum\FileStorage\Model\Behavior;

use ArrayAccess;
use ArrayObject;
use Burzum\FileStorage\Storage\StorageTrait;
use Burzum\FileStorage\Storage\StorageUtils;
Expand All @@ -19,7 +18,6 @@
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\Utility\Text;

/**
* Storage Behavior
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Storage/Listener/BaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Burzum\FileStorage\Storage\StorageUtils;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\Utility\Text;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 8 additions & 13 deletions src/Storage/StorageUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
}
10 changes: 5 additions & 5 deletions tests/TestCase/Storage/StorageUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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!');
}
}

0 comments on commit 1027897

Please sign in to comment.