The simplest solution I can think of would be to have an addon that stores some attachments in the data bucket (ones you want public) and present them as such. Then you don't really need to change anything else or do any kludgey trickery elsewhere. It wouldn't be an impossible addon (and honestly not really that hard), however it's not really a Cloudflare thing, so not sure it would be best suited in this addon.
I took your advice and thought I'd share results for others that are using this add-on, so CF users.... Hope you don't mind me sharing here.
So yea ... Every time an image in a post was displayed (some posts having up to 20 pics, some thread pages having 30 posts ), XF runs a full PHP request for permission check + generates a signed, uncacheable redirect to R2 storage. Uncacheable by the user's browser as well. General traffic and crawlers hitting threads constantly, so each image = multiple PHP hits on every crawl. That saturated our PHP workers.
The small add-on that stores attachments from
publicly-viewable forums in our public, edge-cached bucket and serves them as direct URLs. Public images now serve straight from Cloudflare's cache on initial load ... no PHP, no permission check per image view. DMs and private forums are untouched ... they still get the full per-view permission check, so nothing leaks.
The public URL for the image also allowed the user's browser to cache the image as well
Before -> After (3-day windows on the /attachments/ path):
- Presign redirects through PHP: 1,638,000 → 139,000 (−91%)
- Origin timeouts (5xx): 575,000 → 85,000 (−85%)
- Total PHP-touching attachment events: ~2.2M → ~224K
It's been live and stable for weeks now. The drop landed over the first week as edge caches warmed and it's held steady since. The remaining traffic is mostly legitimate private content (DMs, private forums) that's supposed to go through PHP.
Members even state:
But the site is now quicker, cleaner, and images are loading faster than I've ever seen here.
Must be
thirty time!
Net result: the crawler flood that was knocking us over now serves from cache instead of hitting the origin.
To your other point of this not being a part of this add-on .... I see both sides. People use this for CF performance / management .... this certainly helps that but on the other hand ... should probably be a XF codebase fix really.