Thursday, February 21, 2013

Coldfusion Web Application Saving to DropBox

Every now and again, I'm working on a project that renders something that an administrative user needs to take with them; be it a pdf, or a csv representation of a report. Either way, once it is completed, I used to use coldfusion's cfcontent tag to dump the stream to the browser....which I've always thought of as pulling a fast-one on the browser, tricking it into thinking it is a physical file on disk to be downloaded, and Bob's your uncle.

This works, but let's be honest, cfcontent seems a bit of a sketchy way of delivering a file.... but it works... unless you're on shared hosting, in which case it's disabled... or will be, or should be, or will-be-until-you're-in-production.

I started out looking through the API for Dropbox, to try and figure out how to get a web application to create a new file in dropbox, which is totally possible... but much too complicated, as it has to be keyed to a specific dropbox account.

Then it dawned on me... I can create files on the disk... cfdocument allows you to specify a file name, so does cfimage, and cffile lets you create almost anything... Dropbox (and Google Drive) , are after all just a folder on the disk.. which is controlled by dark magic :-)

And this idea actually works... you just target the Dropbox folder, get your web app to store the generated file here, and the cloud-storage mechanisms do the heavy lifting for you. You could even create a dropbox folder hierarchy to keep things tidy.

2 comments:

Dave said...

But how do you know that Dropbox has completed syncing the file? Let's say you want to retrieve a file from Dropbox. We need to know when the file has completed syncing and is now ready to be downloaded to our ColdFusion application.

Unknown said...

Hi Dave,

In this case, I'm using this for outbound publishing only: writing a new, uniquely named file to the disk, so anyone sharing the folder knows the is syncd when the file exists at their end. ( and CF sends them an email to expect a new file to appear ). Probably getting cf to deliver the file by attachment would work as well, since it's quite small in this case.

For a more advanced inbound file monitor, there is a directory watching gateway in Railo that can trigger when files change on disk.