craftcms / cms

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

[4.x]: Upload .ics file in Assets #12110

Closed HannahDeWachter closed 1 year ago

HannahDeWachter commented 1 year ago

What happened?

Description

I want to upload an .ics file in Assets. The first error I got was Upload failed. The error message was: “Failed to save the asset: “ics” is not an allowed file extension.”. Then I added 'extraFileKinds' => ['ics'], in general.php. The error is now Cannot access offset of type string on string.

Steps to reproduce

  1. Add 'extraFileKinds' => ['ics'], in general.php
  2. Upload ics fille in the Assets

Expected behavior

The files gets uploaded

Actual behavior

Error: Cannot access offset of type string on string

Stack trace


Stack trace:
#0 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/elements/Asset.php(2370): craft\helpers\Assets::getFileKindByExtension('Calendar.ics')
#1 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/elements/Asset.php(2347): craft\elements\Asset->_setKind()
#2 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/services/Elements.php(2544): craft\elements\Asset->beforeSave(true)
#3 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/services/Elements.php(795): craft\services\Elements->_saveElementInternal(Object(craft\elements\Asset), true, true, NULL)
#4 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/controllers/AssetsController.php(252): craft\services\Elements->saveElement(Object(craft\elements\Asset))
#5 [internal function]: craft\controllers\AssetsController->actionUpload()
#6 /Users/hannah/Sites/kulvai/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /Users/hannah/Sites/kulvai/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#8 /Users/hannah/Sites/kulvai/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('upload', Array)
#9 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/web/Application.php(301): yii\base\Module->runAction('assets/upload', Array)
#10 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/web/Application.php(625): craft\web\Application->runAction('assets/upload', Array)
#11 /Users/hannah/Sites/kulvai/vendor/craftcms/cms/src/web/Application.php(280): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#12 /Users/hannah/Sites/kulvai/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#13 /Users/hannah/Sites/kulvai/public/index.php(23): yii\base\Application->run()````

### Craft CMS version

4.2.5.1

### PHP version

8.1

### Operating system and version

_No response_

### Database type and version

_No response_

### Image driver and version

_No response_

### Installed plugins and versions

-
i-just commented 1 year ago

In order to be able to upload .ics files, you should use extraAllowedFileExtensions: https://craftcms.com/docs/4.x/config/general.html#extraallowedfileextensions.

extraFileKinds is used to add extension to an existing kind/type of files or to register new kinds/types. (Kinds/types are what shows when you create an Assets field and select "Restrict allowed file types".)

For example, if you wanted to say that .esp files should also count as an Image (though please note that as per documentation, you would still have to add it to extraAllowedFileExtensions too).

Please also note that the syntax for adding items via extraFileKinds is different: https://craftcms.com/docs/4.x/config/general.html#extrafilekinds (you need to use a multidimensional array in there).

Hope this helps!

HannahDeWachter commented 1 year ago

@i-just This worked. Thanks!