0

ColdFusion in odd places - using the directory watcher on my desktop

ColdFusion, InstantSpot, Tips and Tricks

Since recently installling yet another distro on my laptop, I was unable to get the FTP functionality of my webcam software (Camorama) to work properly. The program will save snapshots locally, but bombs on transfer. Rather than troubleshoot it to death, I decided to whip out a quick and dirty ColdFusion directory watcher event gateway and have it watch for updated images, and then push them to my webserver via FTP.

For anyone interested in this non-earth shattering bit of code, here it is. First I created a config file:

WebcamWatcher.cfg

# The directory we want to watch. 
directory=/home/dshuck/Webcam_Pictures

# Do we want to recurse the directories?
recurse=no

# miliseconds between checks
interval=6000

# The comma separated list of extensions to match.
extensions=*

# component method for change events
changeFunction=onChange

# component method for add events
addFunction=onAdd

# no delete events for now
deleteFunction= 

Now to create the methods in our WebcamWatcher.cfc. In short, either a changed file or an added file will trigger the putImage function which first creates the FTP connection, changes directories to my webcam directory, then pushes the file to the server. Here is the code:

WebcamWatcher.cfc

<cfcomponent output="false">
	<cffunction name="onAdd" output="false">
		<cfargument name="CFEvent" type="struct" required="yes">
		<cfset var Data=CFEvent.data />
	  	<cflog file="DirectoryWatcher" application="No" 
	     	text=" ACTION: #data.type#;  FILE: #data.filename#;  calling putImage()" />
		<cfset putImage() />
	</cffunction>
	

	<cffunction name="onChange" output="false">
	  	<cfargument name="CFEvent" type="struct" required="yes">
	  	<cfset var data=CFEvent.data>
	  	<cflog file="DirectoryWatcher" application="No" 
	      text=" ACTION: #data.type#;  FILE: #data.filename#; TIME: #timeFormat(data.lastmodified)# calling putImage();" />
		<cfset putImage() />
	</cffunction>

	<cffunction name="putImage" access="private" output="false" returntype="void">
		<cfftp action = "open"
	   		username = "joeuser"
	  		connection = "MyConnection"
	   		password = "mycoolpassword"
	   		server = "www.mywebserver.com"
	   		stopOnError = "true" />
		
		<cfif cfftp.Succeeded>
			<cfftp 
				connection="MyConnection" 
				action="changedir" 
				directory="htdocs/mywebcamdirectory" />
			
			<cfif cfftp.Succeeded>
				<cfftp 
					connection = "MyConnection"
					action = "putFile" 
					name = "uploadFile" 
					transferMode = "binary" 
					localFile = "/home/dshuck/Webcam_Pictures/webcam.jpeg" 
					remoteFile = "DaveWebcam.jpg" />
			</cfif>
	
		</cfif>
		<cflog file="DirectoryWatcher" application="false" text="file push to webserver...#cfftp.Succeeded#" />
	</cffunction>
</cfcomponent>

So, now the internet can yet again be graced with my "almost live" presence. I can almost hear the selective sigh of relief.

I have to consider this to be a somewhat odd place for ColdFusion and it got me thinking... What kinds of odd places do you or have you used ColdFusion?

Adrian J. Moreno said:
 
What, like in the backseat of a Volkswagon?
 
posted 93 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Dave at work...