One of the issues with running the OTIH system over a distributed network of static and mobile devices is the lower bandwidth available at the mobile end. It is therefore important to figure out the best ways to make use of local file storage on an ‘Out There’ server, whilst maintaining an up to date link back to the ‘In Here’ site.
A potential bandwidth hog would be the need to access images from the In Here server for display in the pages of our Drupal-based CMS on Out There devices. Given that we will sync files between the OT and IH servers, a much better solution is to serve the HTML from the IH server for users at both locations, but to grab the images locally at each end.
Drupal doesn’t support this kind of thing as standard, but after wading through a fair amount of the core code, we found that it actually requires only a few additional lines to get it to work.
In our site, we are using two modules to display images. One is ImageField, which is responsible for the CCK image type and produces the full sized images, the other is ImageCache, used to produce thumbnails or other modified sizes.
In nearly all cases, path variables in Drupal are relative, this makes sense as you can then easily move the site to various servers, but it makes it hard to modify the full path on the fly. To do this and get it rendered correctly we need to modify the theme function of both our modules: Function theme_imagefield_image in ImageField.module, and function theme_imagecache in the ImageCache.module. What we are aiming to do is change the value of the url that is returned from these functions, as this will then be rendered by the site.
Firsly, we find the line in the function that generates the standard url, in the ImageField module this is: $url = file_create_url($path) . $query_string; , and in ImageCache, it is: $imagecache_url = imagecache_create_url($presetname, $path);
After this, we insert our code. We are going to decide which server to use based on the current users’ membership of a group. If they are members of ‘Out There’, we will modify the url and thus the images will be served locally. We also want to ignore the admin user, they will get images from the standard server, even though they are technically a member of the ‘Out There’ group.
The following lines get or set the variables we need. $user identifies the current user, $base_url gives us the current path of the server (without the relative part) and we also identify the gid of the group we are checking for in our conditional statement.