Skip to content

Commit

Permalink
Delete asset files upon deletion of asset record
Browse files Browse the repository at this point in the history
has to enabled in Config AssetsPlugin.AssetsTable.deleteFile = true
  • Loading branch information
jcsiegrist committed May 16, 2024
1 parent 646d75c commit 597b87a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/app_assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'assetsDir' => 'resources' . DS . 'assets' . DS,
'displayField' => 'title',
'Behaviors' => [],
'deleteFile' => false,
],
'ImageAsset' => [
// driver can be 'imagick' or 'gd'
Expand Down
11 changes: 10 additions & 1 deletion src/Model/Table/AssetsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Assets\Model\Table;

use ArrayObject;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
Expand All @@ -11,6 +12,7 @@
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Laminas\Diactoros\UploadedFile;
use Nette\Utils\FileSystem;

/**
* Assets Model
Expand Down Expand Up @@ -124,11 +126,18 @@ public function validationDefault(Validator $validator): Validator
* @param \ArrayObject $options Options passed to the event
* @return \Cake\ORM\Query
*/
public function beforeFind(EventInterface $e, Query $query, \ArrayObject $options)
public function beforeFind(EventInterface $e, Query $query, ArrayObject $options)
{
return $query->orderDesc('modified');
}

public function afterDelete(EventInterface $e, EntityInterface $entity, ArrayObject $options)
{
if (Configure::read('AssetsPlugin.AssetsTable.deleteFile') === true) {
FileSystem::delete(ROOT . DS . $entity->directory . $entity->filename);
}
}

/**
* @return string
*/
Expand Down

0 comments on commit 597b87a

Please sign in to comment.