craftcms / craftcms/cms

Dynamic asset path locations that use the entries slug, cause internal server error if saved before published

Open
#7,918 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

Description

We often create dynamic file paths for our asset fields, such as /hero-images/{section.handle}/{slug}. This has been commonplace in our websites for years and helps to keep everything organized in assets. but it seems since the new publishing workflow was introduced in 3.6, some problems are occuring.
As a slug is not created until an entry is actually published, our clients are now experiencing internal server errors when they finally publish an article that has images in. Obviously, we could simply add {slug is defined ? slug : "undefined"} but this would result in all our images being stored in an undefined folder. a better workflow would be if craft placed all images in a temp folder of some kind until the entry is published, and then move them automatically to the url with the slug added.

Steps to reproduce
  1. Create and save an unpublished entry that has an image in, within a field that has dynamic file paths that includes the slug
  2. publish
  3. Internal server error
Expected behavior

Until an entry is published, Craft should place all assets in a temp folder. then, only when the entry is published, should craft generate the dynamic path.

see errors logs:

web_php_error  2021-05-11T16:56:35Z #11 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/services/Elements.php(755): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true)
apache_access  92.187.201.34 - - [11/May/2021:16:56:35 +0000] "GET /cpresources/1d953c2b/css/craft.css?v=1620752067 HTTP/1.1" 200 31815 "https://cms.5rightsfoundation.com/admin/entries/publications/1901?draftId=40" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
web_php_error  2021-05-11T16:56:35Z 2021-05-11 17:56:31 [-][1][83f01dada03c15adde50ee732eb0f424][error][craft\errors\VolumeObjectNotFoundException] League\Flysystem\FileNotFoundException: File not found at path: user_59/8.png in /srv/app/5rights-cms/htdocs/vendor/league/flysystem/src/Filesystem.php:389
web_php_error  2021-05-11T16:56:35Z 2021-05-11 17:56:31 [-][1][83f01dada03c15adde50ee732eb0f424][warning][application] Couldn’t move asset because the file doesn’t exist: File not found at path: user_59/8.png
web_php_error  2021-05-11T16:56:35Z #0 /srv/app/5rights-cms/htdocs/vendor/league/flysystem/src/Filesystem.php(210): League\Flysystem\Filesystem->assertPresent('user_59/8.png')
web_php_error  2021-05-11T16:56:35Z Stack trace:
web_php_error  2021-05-11T16:56:35Z #1 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/base/FlysystemVolume.php(151): League\Flysystem\Filesystem->rename('user_59/8.png', 'user_1/8.png')
web_php_error  2021-05-11T16:56:35Z #2 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/elements/Asset.php(2073): craft\base\FlysystemVolume->renameFile('user_59/8.png', 'user_1/8.png')
web_php_error  2021-05-11T16:56:35Z #3 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/elements/Asset.php(1844): craft\elements\Asset->_relocateFile()
web_php_error  2021-05-11T16:56:35Z #4 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/services/Elements.php(2510): craft\elements\Asset->afterSave(false)
web_php_error  2021-05-11T16:56:35Z #5 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/services/Elements.php(755): craft\services\Elements->_saveElementInternal(Object(craft\elements\Asset), true, false, true)
web_php_error  2021-05-11T16:56:35Z #6 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/services/Assets.php(206): craft\services\Elements->saveElement(Object(craft\elements\Asset))
web_php_error  2021-05-11T16:56:35Z #7 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/fields/Assets.php(578): craft\services\Assets->moveAsset(Object(craft\elements\Asset), Object(craft\models\VolumeFolder))
web_php_error  2021-05-11T16:56:35Z #9 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/elements/Entry.php(1572): craft\base\Element->afterSave(false)
web_php_error  2021-05-11T16:56:35Z #10 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/services/Elements.php(2510): craft\elements\Entry->afterSave(false)
web_php_error  2021-05-11T16:56:35Z #12 /srv/app/5rights-cms/htdocs/vendor/craftcms/cms/src/controllers/EntryRevisionsController.php(436): craft\services\Elements->saveElement(Object(craft\elements\Entry))
Additional info
  • Craft version:3.6.12.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the stack trace entries in craft/elements/Asset.php, craft/services/Assets.php, and craft/elements/Entry.php, then reproduce the unpublished-entry workflow using an asset path containing {slug}. Trace the failed rename from the temporary user folder to the published path; done means saving before publication and publishing completes without an internal server error and the asset reaches its slug-based path.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.