nystudio107 / craft-imageoptimize

Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.
https://nystudio107.com/plugins/imageoptimize
Other
235 stars 36 forks source link

Renaming assets on an S3 volume fails #255

Closed joshuabaker closed 3 years ago

joshuabaker commented 3 years ago

Description

When renaming an asset on an S3 volume the underlying file replacement fails causing broken asset links.

Stack trace

ErrorException: Warning: unlink(/srv/app/*******/htdocs/api/storage/runtime/assets/sources/11938.jpg): No such file or directory
#27 /api/vendor/yiisoft/yii2/helpers/BaseFileHelper.php(416): yii\helpers\BaseFileHelper::unlink
#26 /api/vendor/craftcms/cms/src/helpers/FileHelper.php(439): craft\helpers\FileHelper::unlink
#25 /api/vendor/craftcms/cms/src/services/AssetTransforms.php(1064): craft\services\AssetTransforms::getLocalImageSource
#24 /api/vendor/craftcms/cms/src/elements/Asset.php(1372): craft\elements\Asset::getTransformSource
#23 /api/vendor/craftcms/cms/src/services/AssetTransforms.php(1550): craft\services\AssetTransforms::_createTransformForAsset
#22 /api/vendor/craftcms/cms/src/services/AssetTransforms.php(815): craft\services\AssetTransforms::_generateTransform
#21 /api/vendor/craftcms/cms/src/services/AssetTransforms.php(706): craft\services\AssetTransforms::ensureTransformUrlByIndexModel
#20 /api/vendor/craftcms/cms/src/services/Assets.php(620): craft\services\Assets::getAssetUrl
#19 /api/vendor/nystudio107/craft-imageoptimize/src/imagetransforms/CraftImageTransform.php(65): nystudio107\imageoptimize\imagetransforms\CraftImageTransform::getTransformUrl
#18 /api/vendor/nystudio107/craft-imageoptimize/src/services/OptimizedImages.php(521): nystudio107\imageoptimize\services\OptimizedImages::addVariantImageToModel
#17 /api/vendor/nystudio107/craft-imageoptimize/src/services/OptimizedImages.php(130): nystudio107\imageoptimize\services\OptimizedImages::populateOptimizedImageModel
#16 /api/vendor/nystudio107/craft-imageoptimize/src/services/OptimizedImages.php(248): nystudio107\imageoptimize\services\OptimizedImages::updateOptimizedImageFieldData
#15 /api/vendor/nystudio107/craft-imageoptimize/src/fields/OptimizedImages.php(223): nystudio107\imageoptimize\fields\OptimizedImages::afterElementSave
#14 /api/vendor/craftcms/cms/src/base/Element.php(3196): craft\base\Element::afterSave
#13 /api/vendor/craftcms/cms/src/elements/Asset.php(1838): craft\elements\Asset::afterSave
#12 /api/vendor/craftcms/cms/src/services/Elements.php(2454): craft\services\Elements::_saveElementInternal
#11 /api/vendor/craftcms/cms/src/services/Elements.php(755): craft\services\Elements::saveElement
#10 /api/vendor/craftcms/cms/src/controllers/AssetsController.php(251): craft\controllers\AssetsController::actionSaveAsset
#9 [internal](0): call_user_func_array
#8 /api/vendor/yiisoft/yii2/base/InlineAction.php(57): yii\base\InlineAction::runWithParams
#7 /api/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\Controller::runAction
#6 /api/vendor/craftcms/cms/src/web/Controller.php(190): craft\web\Controller::runAction
#5 /api/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Module::runAction
#4 /api/vendor/craftcms/cms/src/web/Application.php(274): craft\web\Application::runAction
#3 /api/vendor/craftcms/cms/src/web/Application.php(577): craft\web\Application::_processActionRequest
#2 /api/vendor/craftcms/cms/src/web/Application.php(253): craft\web\Application::handleRequest
#1 /api/vendor/yiisoft/yii2/base/Application.php(386): yii\base\Application::run
#0 /app.php(24): null

Versions

This is possibly a variant or duplicate of #253.

joshuabaker commented 3 years ago

Might the following condition be the issue?

https://github.com/nystudio107/craft-imageoptimize/blob/d163cced55f64976974a3ab73a51bf54a15beb4c/src/fields/OptimizedImages.php#L222-L239

The SCENARIO_REPLACE scenario isn’t considered here, but it’s used in Craft’s replaceAssetFile method.

khalwat commented 3 years ago

That's an interesting notion @joshuabaker -- are you able to quickly change it to:

                 if ($isNew || $scenario === Asset::SCENARIO_FILEOPS || $scenario === Asset::SCENARIO_REPLACE) { 

To see if that fixes this in your case?

khalwat commented 3 years ago

Related? https://github.com/nystudio107/craft-imageoptimize/issues/253

khalwat commented 3 years ago

@joshuabaker ? Have you tried applying the above?

joshuabaker commented 3 years ago

@khalwat I haven’t, no. We’ve moved to next/image for this particular site.

khalwat commented 3 years ago

Okay. I've been unable to reproduce, so I will close it. Feel free to re-open if you revisit it.