Search code examples
phplaraveloctobercms

Adding file extension to allowed list for file uploads in OctoberCMS?


The problem is in OctoberCMS backend within the File Manager of Assets, I receive the following error message when I attempt to upload a SVG file:

Error uploading file 'logo.svg': Only the following file types are allowed: jpg, jpeg, bmp, png, webp, gif, ico, css, js, woff, woff2, ttf, eot, json, md, less, sass, scss

The error message is generated from AssetList.php and references the function getDefinitions($type) in October\Rain\Filesystem.

    /**
     * Returns a definition set from config or from the default sets.
     * @param $type string
     * @return array
     */
    public function getDefinitions($type)
    {
        if (!method_exists($this, $type)) {
            throw new Exception(sprintf('No such definition set exists for "%s"', $type));
        }

        return (array) Config::get('cms.fileDefinitions.'.$type, $this->$type());
    }

I cannot find any references in /config/cms.php to 'fileDefinitions'.

How do I add SVG to the allowed file definitions array without losing the existing list of filetypes.


Solution

  • To upload a SVG that I have authored and know doesn't contain malicious code like embedded CSS and Javascript, I added the following to the bottom of the config: /config/cms.php.

        /*
        |--------------------------------------------------------------------------
        | File extensions list for allowed file uploads
        |--------------------------------------------------------------------------
        */
    
        'fileDefinitions' => [
    
            'assetExtensions' => [
                'svg', 'jpg', 'jpeg', 'bmp', 'png', 'webp', 'gif', 'ico', 'css', 'js', 'woff', 'woff2', 'ttf', 'eot', 'json', 'md', 'less', 'sass', 'scss'
            ],
        ],
    

    Save, then the command to clear the cache and it solves the problem.

    $ php artisan config:clear