Skip to content

Instantly share code, notes, and snippets.

Created September 10, 2012 16:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3691868 to your computer and use it in GitHub Desktop.
Save anonymous/3691868 to your computer and use it in GitHub Desktop.
A convenience component with a simple API for managing files
<cfcomponent output="false" accessors="true">
<cfproperty name="fileRoot">
<cffunction name="init" access="public" output="false">
<cfargument name="fileRoot" required="true" />
<cfscript>
setFileRoot(arguments.fileRoot);
//initialize the directory where we store all the files
initializefileRoot();
return this;
</cfscript>
</cffunction>
<!--- a generic method to be used internally for uploading a file --->
<cffunction name="upload" access="public" output="false">
<cfargument name="field" required="true" />
<cfargument name="newName" required="false" />
<cfargument name="subdirectory" required="false" default="">
<cfargument name="uploadPath" required="false" default="#getFileRoot()#" />
<cfargument name="nameConflict" required="false" default="MakeUnique">
<cfscript>
var result = "";
if(len(trim(arguments.subdirectory))){
arguments.uploadPath = arguments.uploadPath & "/" & arguments.subdirectory & "/";
}
arguments.uploadPath = cleanFilePath(arguments.uploadPath);
initDir(arguments.uploadPath);
</cfscript>
<cftry>
<cffile action="upload" destination="#arguments.uploadPath#" filefield="#arguments.field#" nameconflict="#arguments.nameConflict#" result="result" />
<cfcatch type="Application">
<cfthrow type="net.hoteldelta.fileUploadProblem" message="Sorry, no file was detected or the file was not well formed" detail="SYSTEM MESSAGE: #cfcatch.message# - #cfcatch.detail#"/>
</cfcatch>
<cfcatch>
<cfrethrow />
</cfcatch>
</cftry>
<cfscript>
if(structKeyExists(arguments,"newName")){
local.newNameFull = arguments.newName & "." & result.serverFileExt;
fileMove(arguments.uploadPath & result.serverFile,arguments.uploadPath & local.newNameFull);
result.serverFile = local.newNameFull;
}
return result;
</cfscript>
</cffunction>
<cffunction name="uploadForBlob" access="public" output="false">
<cfargument name="field" required="true">
<cfargument name="newname" required="false">
<cfscript>
local.fileName = upload(argumentCollection=arguments).serverFile;
local.filepackage = fileToBLOBPackage(local.fileName);
fileDelete(makeFilePath(local.fileName));
return local.filepackage;
</cfscript>
</cffunction>
<cffunction name="fileToBLOBPackage" access="public" output="false">
<cfargument name="fileName" required="true">
<cfargument name="basepath" required="false" default="#getFileRoot()#" />
<cfscript>
local.package = {};
local.fullPath = makeFilePath(argumentCollection=arguments);
local.package.label = getFileFromPath(local.fullpath);
local.package.blob = fileReadBinary(local.fullpath);
return local.package;
</cfscript>
</cffunction>
<!--- a helper that inits a directory, since we have to do a few of them --->
<cffunction name="initDir" access="public" output="false">
<cfargument name="dirPath" required="true" />
<cfscript>
//if the directory doesn't exist, try to create it
if(NOT directoryExists(arguments.dirPath)){
try{
directoryCreate(arguments.dirPath);
}
catch(any err){
throw("Cannot create the directory: #arguments.dirPath#","","net.hoteldelta.badfileRoot");
}
}
//now try writing (And then deleting) a tmp file to be sure it's writable
try{
local.tmpFile = arguments.dirPath & "temp_test_can_be_deleted.txt";
fileWrite(local.tmpFile,"test file that can be deleted");
fileDelete(local.tmpFile);
}
catch(any err){
throw("The directory #arguments.dirPath# is not writable","","net.hoteldelta.badfileRoot");
}
</cfscript>
</cffunction>
<!--- initialize the data directory -- be sure it exists and is writable --->
<cffunction name="initializefileRoot" access="private" output="false">
<cfscript>
initDir(getFileRoot());
</cfscript>
</cffunction>
<cffunction name="fileNameIsValid" access="public" output="false">
<cfargument name="fileName" required="true">
<cfreturn fileExists(makeFilePath(arguments.fileName))>
</cffunction>
<cffunction name="download" output="false">
<cfargument name="fileName" required="true">
<cfset var pathtoget = makeFilePath(arguments.fileName)>
<cfif NOT fileExists(pathtoget)>
<cfthrow type="badFilePath" message="The file requested does not exists">
</cfif>
<cfheader name="Content-Disposition" value="attachment; filename=#getFileFromPath(pathToGet)#">
<cfcontent file="#pathtoget#" reset="true" />
<cfabort>
</cffunction>
<cffunction name="downloadblob" access="public" output="false">
<cfargument name="label" required="true">
<cfargument name="blob" required="true">
<cfargument name="type" required="false" default="application/octet-stream">
<cfheader Name="Content-Length" Value="#arraylen(arguments.Blob)#">
<cfheader name="Content-Disposition" value="attachment; filename=#arguments.label#">
<cfcontent variable="#arguments.blob#" reset="true" type="#arguments.type#">
<cfabort>
</cffunction>
<cffunction name="copy" access="public" output="false" hint="Copies a file into the root folder (or sub dir if specified)">
<cfargument name="source" required="true" hint="A full path to the file you want to copy">
<cfargument name="subdirectory" required="false" default="">
<cfargument name="newName" required="false" default="#getFileFromPath(arguments.source)#">
<cfscript>
local.pathto = makeFilePath(arguments.subdirectory & "/");
initDir(local.pathto);
local.pathTo &= arguments.newName;
fileCopy(arguments.source,local.pathto);
</cfscript>
</cffunction>
<cffunction name="makeFilePath" access="public" output="false" >
<cfargument name="fileName" required="true" />
<cfargument name="basepath" required="false" default="#getFileRoot()#">
<cfreturn arguments.basePath & cleanFilePath(arguments.fileName,true) />
</cffunction>
<cffunction name="cleanFilePath" access="public" output="false">
<cfargument name="path" required="true">
<cfargument name="stripLeadingSlash" required="false" default="false">
<cfscript>
local.clean = replace(arguments.path,"\","/","ALL");
local.clean = replace(arguments.path,"//","/","ALL");
if(arguments.stripLeadingSlash and left(local.clean,1) EQ "/") local.clean = replace(local.clean,"/","");
return local.clean;
</cfscript>
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment