spacecatninja / craft-imager-x

Image transforms, optimizations and manipulations for your Craft CMS site.
Other
26 stars 16 forks source link

Check assets kind before remove its transforms when calling Assets::EVENT_AFTER_REPLACE_ASSET #273

Closed xhuang9 closed 2 weeks ago

xhuang9 commented 3 weeks ago
### I'm submitting a...

Steps to reproduce

  1. Ensure craft is installed with latest imagerX
  2. Add any volume
  3. Add a json file to this volume
  4. Use craft asset ui to replace this json file

Description

When triggering replace assets, imageX should check the asset type, only attempt to remove asset transforms when the asset is image. Potentially this can be fixed by changing: https://github.com/spacecatninja/craft-imager-x/blob/5c130e07a519179dd8f29cda3b53986201017f7e/src/ImagerX.php#L241 With the below code. ```php // Event listener for clearing caches when an asset is replaced Event::on(Assets::class, Assets::EVENT_AFTER_REPLACE_ASSET, static function(ReplaceAssetEvent $event) use ($config) { // Check if the asset is an image before proceeding if ($event->asset->kind == 'image') { ImagerX::$plugin->imagerx->removeTransformsForAsset($event->asset); // If Imgix purging is possible, do that too if ($config->imgixEnableAutoPurging && ImgixService::getCanPurge()) { ImagerX::$plugin->imgix->purgeAssetFromImgix($event->asset); } } } ); ``` ### Displayed errors, stack trace, relevant logs
PASTE THE ERROR MESSAGE / STACK TRACE HERE
[TypeError] TypeError: Cannot assign null to property spacecatninja\imagerx\models\LocalSourceImageModel::$url of type string in 
/vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php:223 
Stack trace: 
#0 /vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php(115): spacecatninja\imagerx\models\LocalSourceImageModel->getPathsForLocalAsset() 
#1 /vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php(68): spacecatninja\imagerx\models\LocalSourceImageModel->init() 
#2 /vendor/spacecatninja/imager-x/src/services/ImagerService.php(667): spacecatninja\imagerx\models\LocalSourceImageModel->__construct() 
#3 /vendor/spacecatninja/imager-x/src/ImagerX.php(241): spacecatninja\imagerx\services\ImagerService->removeTransformsForAsset() 
#4 [internal function]: spacecatninja\imagerx\ImagerX::spacecatninja\imagerx\{closure}() 
#5 /vendor/yiisoft/yii2/base/Event.php(312): call_user_func() 
#6 /vendor/yiisoft/yii2/base/Component.php(650): yii\base\Event::trigger() 
#7 /vendor/craftcms/cms/src/services/Assets.php(172): yii\base\Component->trigger() 
#8 /vendor/craftcms/cms/src/controllers/AssetsController.php(415): craft\services\Assets->replaceAssetFile() 
#9 [internal function]: craft\controllers\AssetsController->actionReplaceFile() 
#10 /vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array() 
#11 /vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams() 
#12 /vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction() 
#13 /vendor/craftcms/cms/src/web/Application.php(349): yii\base\Module->runAction() 
#14 /vendor/craftcms/cms/src/web/Application.php(648): craft\web\Application->runAction() 
#15 /vendor/craftcms/cms/src/web/Application.php(311): craft\web\Application->_processActionRequest() 
#16 /vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest() 
#17 /web/index.php(12): yii\base\Application->run() 
#18 {main} {"memory":7538880,"exception":"[object] (TypeError(code: 0): Cannot assign null to property spacecatninja\\imagerx\\models\\LocalSourceImageModel::$url of type string at /vendor/spacecatninja/imager-x/src/models/LocalSourceImageModel.php:223)"} 

Additional info