Created
May 18, 2011 13:46
-
-
Save davejlong/978590 to your computer and use it in GitHub Desktop.
SCP Functionality within ColdFusion using the SFTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent displayname="CF SCP" hint="Handles SCP style functionality within ColdFusion" output="false"> | |
<cfproperty name="scpserver" hint="Remote server to send files to" type="string" /> | |
<cfproperty name="username" hint="Username for access to the remote server" type="struct" /> | |
<cfproperty name="password" hint="Password of the remote server (required if not using private key authentication)" type="string" /> | |
<cfproperty name="key" hint="Path to private key for private key authentication" type="string" /> | |
<cfproperty name="connection" type="any" /> | |
<!--- #### Object Constructor #### ---> | |
<cffunction name="init" hint="Object constructor" access="public" output="false" returntype="any"> | |
<cfargument name="server" type="string" required="false" default="" /> | |
<cfargument name="username" type="string" required="false" default="" /> | |
<cfargument name="password" type="string" required="false" default="" /> | |
<cfargument name="key" type="string" default="" required="false" /> | |
<cfargument name="localFile" type="string" default="" required="false" /> | |
<cflog text="Creating new object." file="cfscp" /> | |
<cfscript> | |
if(len(Arguments.server))setServer(Arguments.server); | |
if(len(Arguments.username))setUsername(Arguments.username); | |
if(len(Arguments.password))setPassword(Arguments.password); | |
if(len(Arguments.key))setKey(Arguments.key); | |
variables.connectionCreated = false; | |
if(len(Arguments.localFile)){ | |
openConnection(); | |
putFile(Arguments.localFile); | |
closeConnection(); | |
} | |
</cfscript> | |
<cfreturn this /> | |
</cffunction> | |
<!--- #### EventGateway Method #### ---> | |
<cffunction name="onIncomingMessage" output="no"> | |
<cfargument name="CFEvent" type="struct" required="yes"> | |
<cfscript> | |
var props = { | |
server = CFEvent.Data.server, | |
username = CFEvent.Data.username, | |
localFile = CFEvent.Data.localFile | |
}; | |
var loc = {}; | |
if(len(CFEvent.Data.password))props.password = CFEvent.Data.password; | |
else if(len(CFEvent.Data.key))props.key = CFEvent.Data.key; | |
</cfscript> | |
<cflog text="Initializing EventGateway" file="cfscp" /> | |
<cflog text="Opening Connection to #CFEvent.Data.server#" file="cfscp" /> | |
<cfftp | |
action="open" | |
secure="true" | |
connection="loc.scp" | |
attributecollection="#props#" /> | |
<cflog text="Sneding file (#CFEvent.data.localFile#) to server" file="cfscp" /> | |
<cfscript> | |
if(NOT len(CFEvent.Data.remoteFile))CFEvent.Data.remoteFile = listLast(CFEvent.Data.localFile,'/'); | |
</cfscript> | |
<cfftp | |
action="putfile" | |
connection="loc.scp" | |
localfile="#CFEvent.Data.localFile#" | |
remotefile="#CFEvent.Data.remoteFile#" /> | |
<cflog text="Closing connection to #CFEvent.Data.server#" file="cfscp" /> | |
<cfftp action="close" connection="loc.scp" /> | |
<cflog text="Closing EventGateway" file="cfscp" /> | |
</cffunction> | |
<!--- #### Object Methods #### ---> | |
<cffunction name="openConnection" access="public" output="false" returntype="any"> | |
<cfscript> | |
var loc = structNew(); | |
loc.auth = { | |
server = scpserver, | |
username = username | |
}; | |
if(len(password))loc.auth.password = password; | |
else if(len(key))loc.auth.key = key; | |
else throw(message='Attribute validation error', detail='Either a password or key is required.'); | |
</cfscript> | |
<cflog text="Opening a new connection to #scpserver#" file="cfscp" /> | |
<cftry> | |
<cfftp | |
action="open" | |
secure="true" | |
connection="loc.scp" | |
attributecollection="#loc.auth#" /> | |
<cfset connection = loc.scp /> | |
<cfcatch type="any"> | |
<cfthrow message="#cfcatch.message#" detail="#cfcatch.detail#" /> | |
</cfcatch> | |
</cftry> | |
<cfset Variables.connectionCreated = true /> | |
<cfreturn connection /> | |
</cffunction> | |
<cffunction name="closeConnection" access="public" output="false" returntype="boolean"> | |
<cflog text="Closing connection to #scpserver#" file="cfscp" /> | |
<cfftp action="close" connection="connection" /> | |
<cfreturn true /> | |
</cffunction> | |
<cffunction name="putFile" access="public" output="false" returntype="boolean"> | |
<cfargument name="localFile" type="string" required="true" /> | |
<cfargument name="remoteFile" type="string" required="false" default="" /> | |
<cflog text="Sending #Arguments.localFile# to #scpserver#" file="cfscp" /> | |
<cfscript> | |
if(NOT Variables.connectionCreated)openConnection(); | |
if(NOT len(Arguments.remoteFile))Arguments.remoteFile = listLast(Arguments.localFile,'/'); | |
</cfscript> | |
<cfftp | |
action="putfile" | |
connection="connection" | |
localfile="#Arguments.localFile#" | |
remotefile="#Arguments.remoteFile#" /> | |
<cfreturn true /> | |
</cffunction> | |
<!--- #### PROPERTY GETTERS AND SETTERS #### ---> | |
<cfscript> | |
function getServer(){return scpserver;} | |
function setServer(argServer){ | |
scpserver = Arguments.argServer; | |
} | |
function getUsername(){return username;} | |
function setUsername(argUsername){ | |
username = Arguments.argUsername; | |
} | |
function setPassword(argPassword){ | |
password = Arguments.argPassword; | |
} | |
function getKey(){return key;} | |
function setKey(argKey){ | |
key = Arguments.argKey; | |
} | |
function getConnection(){return connection;} | |
</cfscript> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment