craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

Asset upload fails if folder doesn't already exist #3467

Closed lukeyouell closed 4 years ago

lukeyouell commented 5 years ago

Description

When uploading an asset the upload fails if the folder doesn't already exist.

I'm given the following error:

Upload failed. The error message was: “Argument 1 passed to craft\services\AssetIndexer::updateIndexEntry() must be of the type integer, null given, called in /srv/users/serverpilot/apps/foobar/vendor/craftcms/cms/src/services/AssetIndexer.php on line 387”

It might be worth noting that my asset volume is AWS S3 based.

Additional info

lukeyouell commented 5 years ago

Update...

It turns out manually creating the folder isn't the solution, so the folder may not be anything to do with the error.

I've noticed that if I simply attempt to upload the image again after seeing the error it works.

andris-sevcenko commented 5 years ago

@lukeyouell Is there a full stack trace to go with that error? That line is in a method that I don't think is called when uploading anything.

MaartenVenema commented 4 years ago

@andris-sevcenko I'm also running into this error. Tried updating my image indexes and did a rebuild of the project config but those steps sadly did nothing. The only solution for now seems to be uploading the file twice. I am running Craft CMS 3.5.15.1 pro. Plugins that could possibly interfere: Image resizer and SuperTable

The error: Argument 1 passed to craft\services\AssetIndexer::updateIndexEntry() must be of the type int, null given, called in /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php on line 422

And the web.log information that i think is related to this issue:

2020-11-10 14:01:57 [-][1][-][error][craft\controllers\AssetsController::actionUpload] An error occurred when saving an asset: Argument 1 passed to craft\services\AssetIndexer::updateIndexEntry() must be of the type int, null given, called in /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php on line 422
2020-11-10 14:01:57 [-][1][-][error][TypeError] TypeError: Argument 1 passed to craft\services\AssetIndexer::updateIndexEntry() must be of the type int, null given, called in /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php on line 422 and defined in /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php:300
Stack trace:
#0 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php(422): craft\services\AssetIndexer->updateIndexEntry(NULL, Array)
#1 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/AssetIndexer.php(395): craft\services\AssetIndexer->indexFileByEntry(Object(craft\models\AssetIndexData), false, true)
#2 /home/u53922p51032/domains/brandspace.online/vendor/verbb/image-resizer/src/services/Resize.php(86): craft\services\AssetIndexer->indexFile(Object(craft\volumes\Temp), 'originals/CityC...')
#3 /home/u53922p51032/domains/brandspace.online/vendor/verbb/image-resizer/src/services/Service.php(53): verbb\imageresizer\services\Resize->resize(Object(craft\elements\Asset), 'CityCentral-Tea...', '/home/u53922p51...')
#4 [internal function]: verbb\imageresizer\services\Service->beforeHandleAssetFile(Object(craft\events\AssetEvent))
#5 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/Event.php(312): call_user_func(Array, Object(craft\events\AssetEvent))
#6 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/Component.php(636): yii\base\Event::trigger('craft\\elements\\...', 'beforeHandleFil...', Object(craft\events\AssetEvent))
#7 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/elements/Asset.php(1757): yii\base\Component->trigger('beforeHandleFil...', Object(craft\events\AssetEvent))
#8 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/Elements.php(2278): craft\elements\Asset->beforeSave(true)
#9 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/services/Elements.php(764): craft\services\Elements->_saveElementInternal(Object(craft\elements\Asset), true, true, NULL)
#10 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/controllers/AssetsController.php(345): craft\services\Elements->saveElement(Object(craft\elements\Asset))
#11 [internal function]: craft\controllers\AssetsController->actionUpload()
#12 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#13 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
#14 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller->runAction('upload', Array)
#15 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('upload', Array)
#16 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module->runAction('assets/upload', Array)
#17 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/web/Application.php(577): craft\web\Application->runAction('assets/upload', Array)
#18 /home/u53922p51032/domains/brandspace.online/vendor/craftcms/cms/src/web/Application.php(253): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#19 /home/u53922p51032/domains/brandspace.online/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#20 /home/u53922p51032/domains/brandspace.online/public_html/index.php(21): yii\base\Application->run()
#21 {main}
2020-11-10 14:01:57 [-][1][-][info][application] $_GET = [
    'p' => 'admin/actions/assets/upload'
]

$_POST = [
    'fieldId' => '123'
    'elementId' => 'null'
]

$_FILES = [
    'assets-upload' => [
        'name' => 'CityCentral-Team-Pic-2018-001.jpg'
        'type' => 'image/jpeg'
        'tmp_name' => '/tmp/phpHJ6dhO'
        'error' => 0
        'size' => 1186315
    ]
]
andris-sevcenko commented 4 years ago

Looking at the code it seems that it's because verbb/image-resizer tries to index the file, if the nonDestructiveResize setting is enabled.

In your specific case, you're using a dynamic subfolder path. If at save time, the path cannot be resolved, the file will be uploaded to the temporary volume, where it will remain until it's possible to resolve the final subfolder path.

However, it's impossible to index the temporary volume, because it has no ID. So it's up to the plugin to check for that before attempting to index the file. So this ends up being a plugin issue.

cc @engram-design