Jump to content

Recommended Posts

Posted

We've long wanted a way to utilise a CDN to offload images/files/videos from ProcessWire sites without losing all the native greatness of ProcessWire image and file field types.

Having read various discussions on here about ways to approach this that never seemed to reach conclusion, I've thrown myself into creating a module that allows offloading of files to Bunny.net CDN as we need a solution for a specific project.

think this would be easily adaptable to any S3 compatible CDN but I've only tested on Bunny.

⚠️ This is still very beta! Use at your own risk!
I've been conducting basic testing and so far, so good but there's bound to be holes or things that others may suggest better ways of doing. But I'm now at a stage where the insight/experience of the PW community might add value to the project - so I'm sharing now!

Full disclosure: Once past the initial project scaffolding I've been using AI/careful prompting to write some of the code so that I can arrive at a prototype as quickly as possible. This seems to have worked well, although some of the code looks a little verbose and could probably be refactored later on. Also not security/pen-tested yet.

https://github.com/warp-design/WireBunnyCdn/

Features:

  • Automatically uploads images to Bunny storage on page save, including all variants and mirrors assets folder structure for simple merging back to local at a later date if needed.
  • Automatically cleanses deleted files (or files from deleted pages) from your CDN.
  • Option to mirror files to CDN or delete local copies (this is the main aim for me, otherwise we could just use ProCache).
  • Handles (basic currently) image sizing - either using standard ProcessWire `$image->size(X,X)` methods or by implementing Bunny Optimizer for sizing using URL params.
  • Rewrites image paths via CDN so that you can use standard `$page->imageField->url` calls with the output being a Bunny path rather than local PW path. Also handles the image previews in admin view.

Roadmap:

  • Support for video uploads (with optional separate CDN endpoint for Bunny Stream buckets).
  • Support for front-end video output to templates using Bunny stream players/optimisation etc.
  • Implement chunked/background uploads for large files.
  • Support for other size() method options, like cropping etc and mapping to Bunny Optimizer equivalents.

 

Anyway - look forward to hearing any advice/feedback/bug reports... I'm sure there's many!

Screenshot2025-09-24at18_20_10.thumb.png.8ba0359e3b3328b0e6a2f86e296ce3df.png

Screenshot 2025-09-24 at 18.16.45.png

  • Like 12
  • Thanks 2
Posted

Awesome.  I use BunnyCDN as well (previously KeyCDN) configured with ProCache, mainly because they publish the list of IPs they use if their edge-servers need to pull assets (and then serve) from your website.

While this post isn't directly related to your module, it matters if you are using WireRequestBlocker for the reasons I explained here (post only viewable if you have access).

Long story short, if a CDN makes a request to a URL on your site that matches a blocking rule in WireRequestBlocker (rare, but it's bound to happen by accident) then the IP of that particular BunnyCDN edge-server will get blocked.  Then if a visitor on your site is being sent assets from that edge server, then it will error because the CDN was never able to obtain it due to the edge-server being blocked.

This is why the site might look fine in California, but broken in New York for example, as the user is being sent assets from different edge-servers.

To prevent this from happening, I have a cronjob set up (runs every 24 hours) to grab the list of BunnyCDN edge-server IPs and I insert it into WireRequestBlockers "IP addresses to whitelist" field.

This is a function that can do what I described above:

function bunnycdnWhitelistIps() {
    if(!wire('modules')->isInstalled('WireRequestBlocker')) return false;

    // Fetch BunnyCDN edge server list
    $url = 'https://bunnycdn.com/api/system/edgeserverlist';

    // Use cURL to fetch the content
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if($httpCode !== 200 || $response === false) {
      throw new WireException("Error fetching data from BunnyCDN API. HTTP Code: $httpCode\n");
    }

    // Parse the JSON response
    $data = json_decode($response, true);

    if(json_last_error() !== JSON_ERROR_NONE) {
      throw new WireException("Invalid IPs.");
    }

    // Extract IP addresses into an array
    $ipAddresses = [];

    if(isset($data) && is_array($data)) {
      foreach($data as $ip) {
        if(filter_var($ip, FILTER_VALIDATE_IP)) {
          $ipAddresses[] = $ip;
        }
      }
    }

    // Remove duplicates and sort
    $ipAddresses = array_unique($ipAddresses);
    sort($ipAddresses);

    $data = wire('modules')->getModuleConfigData('WireRequestBlocker');
    $data['goodIps'] = implode("\n", $ipAddresses);
    wire('modules')->saveModuleConfigData('WireRequestBlocker', $data); 
  }

 

  • Thanks 1
Posted

Thank you - this is really useful to know. If I’m understanding it correctly I think we’re ok in this instance as we’re only pushing content to Bunny via API rather than the CDN hitting the site itself.

It’s definitely early days though and I think there will be lots of edge cases to look out for. We’ve thrown this together for a product MVP that will have some ad traffic sent to it, so there should be plenty of data to look at.

The main motivation at the moment is to minimise the data storage burden vs local storage on the VPS where the rest of the site runs rather than performance optimisation. But caching/compression etc will be interesting next steps.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...