This repository was archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Fix upload for edit #213
Closed
Closed
Fix upload for edit #213
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a37ffe2
Add width/height
dereuromark 10e1fe0
Adding the FileAssociationBehavior.php
7f5da42
Refactoring information gathering for uploaded records
4f2f186
Move image to file processing stack
dereuromark 5ecb05b
Fix upload for edit mode.
dereuromark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use Migrations\AbstractMigration; | ||
|
||
class AddCollectionColumn extends AbstractMigration | ||
{ | ||
/** | ||
* Change Method. | ||
* | ||
* More information on this method is available here: | ||
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method | ||
* @return void | ||
*/ | ||
public function change() | ||
{ | ||
$this->table('file_storage') | ||
->addColumn('collection', 'string', ['length' => 128, 'null' => true, 'default' => null]) | ||
->update(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
parameters: | ||
level: 7 | ||
level: 8 | ||
checkMissingIterableValueType: false | ||
checkGenericClassInNonGenericObjectType: false | ||
bootstrapFiles: | ||
- tests/bootstrap.php | ||
earlyTerminatingMethodCalls: | ||
Cake\Console\Shell: | ||
- abort | ||
ignoreErrors: | ||
- '#Cannot cast array\<string\>\|string to string#' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Burzum\FileStorage\Model\Behavior; | ||
|
||
use ArrayObject; | ||
use Cake\Datasource\EntityInterface; | ||
use Cake\Event\EventInterface; | ||
use Cake\ORM\Association\HasOne; | ||
use Cake\ORM\Behavior; | ||
use Laminas\Diactoros\UploadedFile; | ||
|
||
/** | ||
* File Association Behavior. | ||
* | ||
* @author Florian Krämer | ||
* @copyright 2012 - 2020 Florian Krämer | ||
* @license MIT | ||
*/ | ||
class FileAssociationBehavior extends Behavior | ||
{ | ||
/** | ||
* @var array | ||
* @inheritDoc | ||
*/ | ||
protected $_defaultConfig = [ | ||
'associations' => [], | ||
]; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function initialize(array $config): void | ||
{ | ||
parent::initialize($config); | ||
|
||
$class = get_class($this->getTable()); | ||
foreach ($config['associations'] as $association => $assocConfig) { | ||
$associationObject = $this->getTable()->getAssociation($association); | ||
|
||
$defaults = [ | ||
'replace' => $associationObject instanceof HasOne, | ||
'model' => substr($class, strrpos($class, '\\') + 1, -5), | ||
'property' => $this->getTable()->getAssociation($association)->getProperty(), | ||
]; | ||
|
||
$config['associations'][$association] = $assocConfig + $defaults; | ||
} | ||
|
||
$this->setConfig('associations', $config['associations']); | ||
} | ||
|
||
/** | ||
* @param \Cake\Event\EventInterface $event | ||
* @param \Cake\Datasource\EntityInterface $entity | ||
* @param \ArrayObject $options | ||
* | ||
* @return void | ||
*/ | ||
public function afterSave( | ||
EventInterface $event, | ||
EntityInterface $entity, | ||
ArrayObject $options | ||
): void { | ||
$associations = $this->getConfig('associations'); | ||
|
||
foreach ($associations as $association => $assocConfig) { | ||
$property = $assocConfig['property']; | ||
if ($entity->{$property} === null) { | ||
continue; | ||
} | ||
|
||
if ($entity->id && $entity->{$property} && $entity->{$property}->file) { | ||
$file = $entity->{$property}->file; | ||
|
||
$ok = false; | ||
if (is_array($file) && $file['error'] === UPLOAD_ERR_OK) { | ||
$ok = true; | ||
} elseif ($file instanceof UploadedFile && $file->getError() === UPLOAD_ERR_OK) { | ||
$ok = true; | ||
} | ||
|
||
if (!$ok) { | ||
continue; | ||
} | ||
|
||
if ($assocConfig['replace'] === true) { | ||
$this->findAndRemovePreviousFile($entity, $association, $assocConfig); | ||
} | ||
|
||
$entity->{$property}->set('collection', $assocConfig['collection']); | ||
$entity->{$property}->set('model', $assocConfig['model']); | ||
$entity->{$property}->set('foreign_key', $entity->id); | ||
|
||
$this->getTable()->{$association}->saveOrFail($entity->{$property}); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param \Cake\Datasource\EntityInterface $entity | ||
* @param string $association | ||
* @param array $assocConfig | ||
* | ||
* @return void | ||
*/ | ||
protected function findAndRemovePreviousFile( | ||
EntityInterface $entity, | ||
string $association, | ||
array $assocConfig | ||
): void { | ||
$result = $this->getTable()->{$association}->find() | ||
->where([ | ||
'collection' => $assocConfig['collection'], | ||
'model' => $assocConfig['model'], | ||
'foreign_key' => $entity->get((string)$this->getTable()->getPrimaryKey()), | ||
'id !=' => $entity->get($assocConfig['property'])->get((string)$this->getTable()->{$association}->getPrimaryKey()), | ||
]) | ||
->first(); | ||
|
||
if ($result) { | ||
$this->getTable()->{$association}->delete($result); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to set the id for primary key records?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The File object doesn't care, because the whole lib doesn't care about persistance (its not their job) but if you want to persist it and your file path relies on it, then you clearly should set it. The File objects only purpose and reason that it features certain things is that it might need them for generation pathes or doing certain operations in processors.
The File object is like a DTO between persistence that the user has to take core of in his app and the library that only knows about what the file object provides.