Assets in content path

When genrating some static pages with kirby, we’d like to have the assets linked to the folder they are in rather than linked to the media folder. To do that we used the example of the ‘Assets in Content’ plugin, a summary of the index.php is below:

use Kirby\Cms\App as Kirby;

Kirby::plugin(‘name/assets-in-content', [
  'components' => [
    'file::url' => function (Kirby $kirby, $file) {
        return $kirby->url() . '/content/' . $file->parent()->diruri() . '/' . $file->filename();
    },
  ]
]);

This gives a link to the asset something like this:
https://domain.net/content/1_books/20210725_book-name/book.epub

The question is, is it possible to get a url more like the actual page url, like the one shown below:
https://domain.net/books/book-name/book.epub

Any help much appreciated.

Russ

        return $kirby->url() . '/' . $file->parent()->id() . '/' . $file->filename();

should work as well

Many thanks texnixe, that is brilliant. Static assets are pathed correctly, but images still seem to link to media? I presume there is a reason for this?

When called with something like this:

 <img src="<?=$page->cover()->resize(600,null,65)->url() ?>"
        width="<?=$page->cover()->width()?>" 
        height="<?=$page->cover()->height()?>" 
        srcset="<?=$page->cover()->srcset('bookcover') ?>"
        sizes="(min-width: 640px) 320px, 50vw"
        alt="<?=$page->cover()->caption()<>'' ? $page->cover()->caption(): ($page->title()->excerpt(120)) ?> cover"/>

Yes, you are not using images here, but File::Version objects, aka thumbs. And they are created in the media folder, never in the content folder.

To serve them from a different location, you therefore need to create a custom the File::Version component.

Or maybe something like this here: Permalinks for resized images - #3 by texnixe

Thanks texnixe, I shall investigate further.

Does anyone have any idea why this would stop working in Kirby 5.3.2?

What exactly stopped working?

Sorry I should have been more clear, the plugin in the first post:

… Kirby::plugin(‘name/assets-in-content’, [
‘components’ => [
‘file::url’ => function (Kirby $kirby, $file) {
return $kirby->url() . ‘/content/’ . $file->parent()->diruri() . ‘/’ . $file->filename();
}```

The idea was to keep some files, like ebooks, in the actual page content folder as far a url was concerned. Previously this worked, I don’t know which version that was, but it doesn’t work in the latest version.

So this url worked:
domain/books/book/book.epub
but it does not now -404 Not Found

If I remove the plugin the files appear but now from the media folder.
domain/media/pages/books/book/xxxx/book.epub

Any ideas?

I don’t have an answer from the top of my head and need to try to reproduce the issue.

Thanks Sonja, I did try a quick test in a local startkit and got the same results.

Ok, so I added the plugin from above in a Starterkit.

Then I modified the home template. From this line:

<img src="<?= $cover->resize(1024, 1024)->url() ?>" alt="<?= $cover->alt()->esc() ?>">

I removed the resize() part, so that I ended up with:

<img src="<?= $cover->url() ?>" alt="<?= $cover->alt()->esc() ?>">

When using the version of the plugin from the very first post that returns the full path:

return $kirby->url() . '/content/' . $file->parent()->diruri() . '/' . $file->filename();

with urls like http://532.test/content/1_photography/1_trees/monster-trees-in-the-fog.jpg

it still works.

When using the improved version without the prepended page numbers with a url like http://531.test/photography/trees/monster-trees-in-the-fog.jpg

return $kirby->url() . '/' . $file->parent()->id() . '/' . $file->filename();

it indeed stops to work.

It feels like a route that used to be present to make sure the right path was found has been removed.

From which version of Kirby did you update?

Thanks very much Sonja for your efforts, I have been doing this upgrade for some time, so I can’t actually remember, maybe v3 of Kirby? The site is now updated and works well locally apart from this bit and I need it to work because of the URL links to epubs and some other stuff on the pages.

Oh, ok, I was afraid of something like that but had hoped to be able to narrow it down some more when this changed.

Ok, learned something new. After debugging the issue, I found a setting I wasn’t aware of that fixes the issue. In your config file, add the option

'content.fileRedirects' => true,

and the files will reappear.

It is documented with a warning: content | Kirby CMS

As ever Sonja, many thanks, I would have never found that :grinning_face:. I don’t remember it from the original, perhaps older Kirby versions were different? But then it was some time ago and I may have forgotten. Anyway it is just what was required and now everything works as it should, so thanks again for your awesome support!

Yes, right, that setting didn’t exist in older versions of Kirby, certainly not in Kirby 3.