Override file::url component

Trying to override the file component to serve files from the content folder directly to prevent copying to the media folder. I need it to work if the word “tiles” is anywhere in the URL. Tried this so far but doesn’t seem to work…

<?php

Kirby::plugin('hashandsalt/kirby-skip', [
    'components' => [
        'file::url' => function (Kirby $kirby, $file) {
          $page = $file->parent();
          if (preg_match('/tiles\/|tiles$/', $page->uri()) > 0) {
            return $kirby->url() . '/content/' . $page->diruri() . '/' . $file->filename();
          } else {
            return $file->mediaurl();
          }
        }
    ]
]);

The file paths look a bit like this:

/content/subpage/anothersubpage/somerandomname.tiles/l1/0001/ajpegfile.jpg
/content/subpage/anothersubpage/tiles/l1/0001/00002.jpg
/content/subpage/anothersubpage/somerandomname.tiles/preview.jpg
/content/subpage/anothersubpage/tiles/preview.jpg

The only constant is the word “tiles”. I want to prevent anything under a “tiles” folder being copied to the media folder, regardless of depth.

Can these folders be anywhere in the tree? What is the max depth of the tree? Do they all share a main last parent (or is the common last parent the site object?)

The first bit is constant…

/content/macros/

these pages all have a top level parent of “macros”. So along with that and the word “tiles” somewhere in the url, those are the concrete things to pick up on. Maximum depth is 5…

/content/macros/somepage/tiles/l1/01/l1_01_01.jpg
/content/macros/somepage/somrandomname-tiles/l1/01/l1_01_01.jpg

The depth is either 3 or 5…

/content/macros/anothersubpage/somerandomname-tiles/preview.jpg