Manage files over complete website

Here’s a request that comes up from time to time by the users of the Kirby websites I created: "How can I manage files?"

What they mean is having a central place in the panel that lists all files and allows them, for example, to remove old files.

I figured, I probably could create a virtual page that collect all images via the site.index and then apply that to a files section as parent and show them in the panel. Or I guess so at least.

Or does anyone have an approach to that they successfully used?

I would also have to query and show the pages that reference a file to make sure people do not change or delete files that are referenced.

How would you know that a file is used?

Files can be used in blocks, in files fields, in textarea fields or simply directly in the template. While you could go through all content files and their field to look for references to a file, you would also have to keep track about those used directly in a template/snippet (unless you never do that).

As for the list of files itself, it would probably make sense to show them in a custom Panel area with an action to remove them.

I didn’t try any approach to it but for existing files, I would probably have tried to convert the whole index collection to JSON, then to one big string, then search for the ID/or URL in that structure, and then, as I would know that there are only a few patterns of data structures used, travel up the tree and find the page ID. That’s most likely resource hungry, but not as resource hungry as looping directly over the whole collection and its children. It would work, but probably ends in hardly maintainable code with many magic strings.

And then, to not have to do that all the time as the file library grows, try to save reference information down via hooks when a file is uploaded or a page is saved. That would give me one big file with all references that should be up-to-date as long as the panel is used for content management.

As I never use files uploaded to Kirby directly in snippets, that one wouldn’t be a problem.

Ah, that’s a great idea. I have to look into that.

Most likely calls for writing a plugin for it.

Keep in mind that you can use images at the front-end level without having the images showing up at all within the content tree.

Let’s imagine you have a gallery in a page and you handle that with a files section within your panel.
And then in a template or a snippet you fetch those images like this

$page->images()->filterBy('template' , 'galleryimage')

Those images are technically used by the site but you’ll never be able to see that if you traverse all the content and look for all the type of data structure.

@renestalder already mentioned that he’s not doing that:

I interpreted that in a different way, my bad.
But better safe than sorry :slight_smile:

Oh, I maybe interpret that differently too. I thought of static references to files uploaded to the CMS.

Looking at your example, I sure have these cases. However, thinking about it, I hardly have anything that is not transparent enough in the CMS to understand these. Taking your example, in my cases, I would have a content block for choosing the page to show all images from. Thus, I would be able to find those references in my content files.

But it’s a completely valid point now that I understand what @texnixe probably meant in the first place and what I got wrong here. And yes, there is most likely hardly anything “maintainable” I could do to catch cases where this information is not coming via content files.

It’s also worth mentioning that this whole idea comes from customers that are used to WordPress, where you have the media section to manage all the files. But I guess there is a case for it. Probably a weekend project at some point.

I think you all helped me already enough to understand that it might not be something I can solve as easily as everything else, but there are ways to do it. So, I consider my questions answered.

Thank you all.