Avoiding rerouting based on file extension

Hello!

I’ve got a website that is using routing in the config file to remove the parent slug for urls following this guide. But I’m also generating images for Open Graph tags using content representations (a bit like this).

How do I set the config routing to first just return the file as requested if it includes the extension ‘.png’ (or .jpg, .pdf or whatever) before then routing to remove the parent slug?

This is where I’ve got to, but I’m a little lost…

	'routes' => [
		[
			'pattern' => '.*\.(png|pdf|jpg)$',
			'action' => function ($uid) {
			     RETURN THE FILE AS REQUESTED?
			}
		],
		[
			'pattern' => '(:any)',
			'action'  => function ($slug) {
				
				if ($page = page('parenttoremove')->find($slug)) {
					return page($page);
				}
	
				$this->next();
			}
		],
		[
			'pattern' => 'parenttoremove/(:any)',
			'action'  => function($uid) {
				go($uid);
			}
		]
	]
];

For anyone else having this problem, this is what I ended up with:

	'routes' => [
		[
			'pattern' => '(:any)',
			'action'  => function ($slug) {
				
				if ($page = page('links')->find($slug)) {
					return page($page);
				}
	
				$this->next();
			}
		],
		[
			'pattern' => 'links/(:all)(?<!\.png)$',
			'action'  => function($uid) {
				go($uid);
			}
		]
	]

This removes the parent slug (‘links’ in this case), but doesn’t match with urls ending .png so still allows access to the Open Graph image.