Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

Commit

Permalink
Made the behavior work with the UploadedFile object
Browse files Browse the repository at this point in the history
  • Loading branch information
burzum committed Nov 2, 2020
1 parent 0437ad8 commit 794f153
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/FileStorage/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function entityToFileObject(EntityInterface $entity): FileInterface
}

if ($entity->has('file')) {
$file = $file->withFile($entity->get('file')['tmp_name']);
/**
* @var $uploadedFile \Psr\Http\Message\UploadedFileInterface
*/
$uploadedFile = $entity->get('file');
$file = $file->withFile($uploadedFile->getStream()->getMetadata('uri'));
}

return $file;
Expand Down
26 changes: 13 additions & 13 deletions src/Model/Behavior/FileStorageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function isFileUploadPresent($entity)
{
$field = $this->getConfig('fileField');
if ($this->getConfig('ignoreEmptyFile') === true) {
if (!isset($entity[$field]['error']) || $entity[$field]['error'] === UPLOAD_ERR_NO_FILE) {
if (!isset($entity[$field]) || $entity[$field]->getError() === UPLOAD_ERR_NO_FILE) {
return false;
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public function beforeSave(Event $event, EntityInterface $entity)
$this->dispatchEvent('FileStorage.beforeSave', [
'entity' => $entity,
'storageAdapter' => $this->getStorageAdapter($entity->get('adapter'))
], $this->_table);
], $this->getTable());
}

/**
Expand Down Expand Up @@ -182,6 +182,7 @@ public function afterSave(Event $event, EntityInterface $entity, $options)
);
} catch (Throwable $exception) {
$this->getTable()->delete($entity);
throw $exception;
}
}

Expand Down Expand Up @@ -223,7 +224,7 @@ public function afterDelete(Event $event, EntityInterface $entity, $options)
{
$this->dispatchEvent('FileStorage.afterDelete', [
'entity' => $entity,
], $this->_table);
], $this->getTable());

$file = $this->entityToFileObject($entity);
$this->fileStorage->remove($file);
Expand All @@ -242,16 +243,15 @@ public function afterDelete(Event $event, EntityInterface $entity, $options)
*/
protected function getFileInfoFromUpload(&$upload, $field = 'file')
{
if (!empty($upload[$field]['tmp_name'])) {
$File = new \Cake\Filesystem\File($upload[$field]['tmp_name']);
$upload['filesize'] = filesize($upload[$field]['tmp_name']);
$upload['mime_type'] = $File->mime();
}

if (!empty($upload[$field]['name'])) {
$upload['extension'] = pathinfo($upload[$field]['name'], PATHINFO_EXTENSION);
$upload['filename'] = $upload[$field]['name'];
}
/**
* @var $uploadedFile \Psr\Http\Message\UploadedFileInterface
*/
$uploadedFile = $upload[$field];

$upload['filesize'] = $uploadedFile->getSize();
$upload['mime_type'] = $uploadedFile->getClientMediaType();
$upload['extension'] = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
$upload['filename'] = $uploadedFile->getClientFilename();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);

namespace Burzum\FileStorage;

use Cake\Core\BasePlugin;
Expand Down

0 comments on commit 794f153

Please sign in to comment.