Skip to content

Commit 1027897

Browse files
committed
fixed deprecations
1 parent 022b9c0 commit 1027897

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php": ">=7.4",
2323
"cakephp/cakephp": "^4.5",
2424
"cakephp/plugin-installer": "^1.3",
25-
"knplabs/gaufrette": "^0.11",
25+
"knplabs/gaufrette": "^0.7.0|^0.8.0",
2626
"league/mime-type-detection": "^1.15"
2727
},
2828
"require-dev": {

src/Model/Behavior/FileStorageBehavior.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
namespace Burzum\FileStorage\Model\Behavior;
1212

13-
use ArrayAccess;
1413
use ArrayObject;
1514
use Burzum\FileStorage\Storage\StorageTrait;
1615
use Burzum\FileStorage\Storage\StorageUtils;
@@ -19,7 +18,6 @@
1918
use Cake\Event\EventDispatcherTrait;
2019
use Cake\Event\EventInterface;
2120
use Cake\ORM\Behavior;
22-
use Cake\Utility\Text;
2321

2422
/**
2523
* Storage Behavior
@@ -157,7 +155,9 @@ protected function _checkEntityBeforeSave(EntityInterface &$entity): void
157155
if ($fileHashMethod === true) {
158156
$fileHashMethod = 'sha1';
159157
}
160-
$entity->set('hash', StorageUtils::getFileHash(Text::uuid(), $fileHashMethod));
158+
/** @var \Psr\Http\Message\UploadedFileInterface $file */
159+
$file = $entity->get('file');
160+
$entity->set('hash', StorageUtils::getFileContentHash((string)$file->getStream(), $fileHashMethod));
161161
}
162162
}
163163
}

src/Storage/Listener/BaseListener.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Burzum\FileStorage\Storage\StorageUtils;
1212
use Cake\Datasource\EntityInterface;
1313
use Cake\Event\EventInterface;
14-
use Cake\Utility\Text;
1514
use InvalidArgumentException;
1615

1716
/**
@@ -104,7 +103,11 @@ public function afterDelete(EventInterface $event, EntityInterface $entity): voi
104103
public function afterSave(EventInterface $event, EntityInterface $entity): void
105104
{
106105
if ($this->_checkEvent($event) && $entity->isNew()) {
107-
$hash = StorageUtils::getFileHash(Text::uuid());
106+
$fileField = $this->getConfig('fileField');
107+
/** @var \Psr\Http\Message\UploadedFileInterface $file */
108+
$file = $entity->get($fileField);
109+
110+
$hash = StorageUtils::getFileContentHash((string)$file->getStream());
108111
$path = $this->pathBuilder()->fullPath($entity);
109112

110113
$entity->set('hash', $hash);

src/Storage/StorageUtils.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,27 +221,22 @@ public static function createTmpFile(?string $folder = null, bool $checkAndCreat
221221
}
222222

223223
/**
224-
* Gets the hash of a file.
224+
* Gets the hash of a file contents.
225225
*
226226
* You can use this to compare if you got two times the same file uploaded.
227-
*
228-
* @param string $file Path to the file on your local machine.
229-
* @param string $method 'md5' or 'sha1'
230-
* @throws \InvalidArgumentException
231-
* @link http://php.net/manual/en/function.md5-file.php
232-
* @link http://php.net/manual/en/function.sha1-file.php
233-
* @link http://php.net/manual/en/function.sha1-file.php#104748
234-
* @return string
235227
*/
236-
public static function getFileHash(string $file, string $method = 'sha1'): string
228+
public static function getFileContentHash(string $fileContent, string $method = 'sha1'): string
237229
{
238230
if ($method === 'md5') {
239-
return md5_file($file);
231+
return md5($fileContent);
240232
}
241233
if ($method === 'sha1') {
242-
return sha1_file($file);
234+
return sha1($fileContent);
243235
}
244236

245-
throw new InvalidArgumentException(sprintf('Invalid hash method "%s" provided!', $method));
237+
throw new InvalidArgumentException(sprintf(
238+
'Invalid hash method "%s" provided!',
239+
$method
240+
));
246241
}
247242
}

tests/TestCase/Storage/StorageUtilsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ public function testFileExtension()
245245
*
246246
* @return void
247247
*/
248-
public function testGetFileHash()
248+
public function testGetFileContentHash()
249249
{
250-
$result = StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg');
250+
$result = StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg'));
251251
$this->assertEquals($result, 'd68da24d79835d70d5d8a544f62616d0e51af191');
252252

253-
$result = StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg', 'md5');
253+
$result = StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg'), 'md5');
254254
$this->assertEquals($result, '29574141b2c44cc029828f6c5c6d3cd2');
255255
}
256256

@@ -259,9 +259,9 @@ public function testGetFileHash()
259259
*
260260
* @return void
261261
*/
262-
public function testGetFileHashInvalidArgumentException()
262+
public function testGetFileContentHashInvalidArgumentException()
263263
{
264264
$this->expectException(\InvalidArgumentException::class);
265-
StorageUtils::getFileHash($this->fileFixtures . 'titus.jpg', 'invalid-hash-method!');
265+
StorageUtils::getFileContentHash(file_get_contents($this->fileFixtures . 'titus.jpg'), 'invalid-hash-method!');
266266
}
267267
}

0 commit comments

Comments
 (0)