Distributed Media; a WordPress Challege

Though when the team at Saturn decided to build kerala online, on wordpress, they were quite confident, the complications started to get under their skins when the first wave hit the portal and at a point were the portal was being run from a single server and reached about 35K visits per day. The server was running on apache with about 2G Memory and hosted in Asianet the ISP division. There was times when the server was running at load averages above 60. All workarounds were getting futile, and there was certain times, like evening and early morning when the server hits the upper limit and ceases to respond. The easiest solution suggested by a brainstorming session was to distribute the media. Since the same server was serving streaming videos, and content related images, which we could easily synchronize to a different server and using an appropriate plugin, we could link out the contents. The plugin which we used is

<?php
/*
Plugin Name: WP-KOL-Media
Plugin URI: https://www.saturn.in
Description: Replaces all media files from '/wp-content/uploads/' to respective mirror servers as url.
Version: 1.0
Author: Jiju Thomas Mathew
Author URI: https://www.jijutm.com
Author Email: jijutm@saturn.in
*/
 
function jtm_wpKolMedia()
{
  function 
exactRewriteUrl(&$v$key){
    
$trg 'https://images.keralaonline.com';
    if(
eregi(".flv$"$v))
       
$trg 'https://videos.keralaonline.com';
    if(!
eregi("\.php$"$v))
      
$v str_replace('https://keralaonline.com'$trg$v);
  }
 
  function 
doMediaUrlRewrite($buffer)
   {
      
preg_match_all("#" get_bloginfo('url') . "/wp-content/(uploads|themes)/(.+)#i"$buffer$m);
      
$new $m[0];
      
array_walk($new'exactRewriteUrl');
      return 
str_replace($m[0], $new$buffer);
   }
 
ob_start("doMediaUrlRewrite");
}
 
add_action('get_header''jtm_wpKolMedia'999);
?>