craftcms / cms

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

[4.x]: Assets fail to upload if field set to "Restrict location" and "allow subfolder" with empty subpath #11212

Closed ishitatsuyuki closed 2 years ago

ishitatsuyuki commented 2 years ago

What happened?

Description

There's a type error when the following reproduction steps are followed.

Steps to reproduce

  1. Add an assets field, set its upload options to the following:

image

  1. Create an section, then create an element. Upload a file from the + button.
  2. Observe 500.

Expected behavior

Assets get uploaded.

Actual behavior

2022-05-12 14:10:11 [web.ERROR] [craft\controllers\AssetsController::actionUpload] An error occurred when saving an asset: craft\helpers\StringHelper::ensureRight(): Argument #1 ($str) must be of type string, null gi
ven, called in /var/www/html/vendor/craftcms/cms/src/fields/Assets.php on line 935 {"memory":1678240} 
2022-05-12 14:10:11 [web.ERROR] [TypeError] TypeError: craft\helpers\StringHelper::ensureRight(): Argument #1 ($str) must be of type string, null given, called in /var/www/html/vendor/craftcms/cms/src/fields/Assets.p
hp on line 935 and defined in /var/www/html/vendor/craftcms/cms/src/helpers/StringHelper.php:513
Stack trace:
#0 /var/www/html/vendor/craftcms/cms/src/fields/Assets.php(935): craft\helpers\StringHelper::ensureRight(NULL, '/')
#1 /var/www/html/vendor/craftcms/cms/src/fields/Assets.php(448): craft\fields\Assets->_determineUploadFolderId(Object(craft\elements\Entry))
#2 /var/www/html/vendor/craftcms/cms/src/controllers/AssetsController.php(207): craft\fields\Assets->resolveDynamicPathToFolderId(Object(craft\elements\Entry))
#3 [internal function]: craft\controllers\AssetsController->actionUpload()
#4 /var/www/html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#5 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#6 /var/www/html/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('upload', Array)
#7 /var/www/html/vendor/craftcms/cms/src/web/Application.php(301): yii\base\Module->runAction('assets/upload', Array)
#8 /var/www/html/vendor/craftcms/cms/src/web/Application.php(625): craft\web\Application->runAction('assets/upload', Array)
#9 /var/www/html/vendor/craftcms/cms/src/web/Application.php(280): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#10 /var/www/html/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#11 /var/www/html/web/index.php(12): yii\base\Application->run()
#12 {main} {"memory":1678616,"exception":"[object] (TypeError(code: 0): craft\\helpers\\StringHelper::ensureRight(): Argument #1 ($str) must be of type string, null given, called in /var/www/html/vendor/craftcms/cms/
src/fields/Assets.php on line 935 at /var/www/html/vendor/craftcms/cms/src/helpers/StringHelper.php:513)"}

Upload gets stuck at 100%. I've found that assets related things will not properly display a message if a server error happens. Is that a known problem.

Fix

Change https://github.com/craftcms/cms/blob/da7da4dcb4a293ef5e2a2d961d07fd273ad9cca3/src/fields/Assets.php#L932

to:

            $subpath = $this->restrictedLocationSubpath ?: '';

Craft CMS version

4.0.2

PHP version

8.1.5

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

brandonkelly commented 2 years ago

Thanks for reporting that! It’s now fixed for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to "dev-develop as 4.0.2" and run composer update.

brandonkelly commented 2 years ago

Craft 4.0.3 is out now with those fixes.