Fun with mappings and resources: Part 1 ZIP and FTP

Posted by Mark Drew on code on April 2, 2009

Tagged under railo

One of the many features that I find fantastic in Railo, and has been the topic of many a "after-work-down-the-pub" conversations, has been that of mappings.

How I have normally used mappings in the past has just been been to be able to access frameworks such as ModelGlue, Reactor and ColdSpring without having to put them in the webroot.

You can still do this with Railo of course, and even set that resource as trusted, but what I wanted to show was some of the really neat features of other types of mappings.

Railo supports the ability to create a mapping to other types of resources, such as FTP, HTTP, SFTP, Amazon's S3, ram and ZIP (and TAR and TGZ) filesystems.

A quick example of where this might be used is to move old logfiles to a zip file. this happens to everyone's server once in a while, you have a bunch of log files that need moving to zip, all you have to do is create a mapping to the zip file:
Virtual: /archive
Resource: zip:///Users/markdrew/wwwroot/mapping_examples/archive.zip

And then you have one line of code to move that log file to the archive:

    <cffile action="move" source="biglog.log" destination="/archive/biglog.log">

If you need to read it from the archive it is just as easy:

    <cffile action="read" file="/archive/biglog.log" variable="content">
    <cfdump eval=content>

There you go, same line without having to do cfzip or anything like that. Just move the item to that resource.

But wait! Lets say I want to move the logs off the server, and put them onto an FTP site. What do we do then? Well, we could change our code to use cfftp, or just change the mapping:

Virual: /archive
Resource: ftp://username:[email protected]/uploads/

Save it, the code does the same and voila, we are now saving it to an FTP site!  Nice!




comments powered by Disqus