Thread: File Explorer: Limit Upload by File Size
Created on: 06/16/11 12:50 PM
| New topic Reply | Page: 1 |
| Replies: 0 | |
| jlango
|
06/16/11 12:50 PM Hi, I modified the File Explorer so I could limit file uploads to a certain file size. If the upload exceeds the size, the File Explorer puts up a (very generic) 'IO error' message. I thought I would post the changes in case anyone else wanted to use them. Maybe it will find its way into a future version of Mango, and I can stop propagating it with each update. :-) I added a row to the setting table, for the actual max file size. NOTE: The file size is meant to be in KB; A change to FileExplorer.cfc makes the conversion. PATH | NAME | VALUE | BLOG_ID ---------------------------------------------------- system/assets maxfilesize 500 NULL I made the following changes to admin/com/fileexplorer/FileExplorer.cfc: init: <cffunction name="init" output="false" returntype="any" hint="instantiates an object of this class" access="public"> <cfargument name="settings" required="true" type="struct"> <cfset var rootDir = createObject("java","java.io.File").init(arguments.settings.rootDirectory)/> <cfset variables.basePath = rootDir.getCanonicalPath()/> <cfset variables.rootDir = arguments.settings.rootDirectory /> <cfset this.path = variables.basePath/> <!--- check that the base path exists ---> <cfif NOT directoryexists(variables.basePath)> <cfthrow message="Base path does not exist"/> </cfif> <cfset variables.extensions = arguments.settings.allowedExtensions /> <cfif NOT structkeyexists(arguments.settings,'rootUrl')> <cfset arguments.settings.rootUrl = '' /> </cfif> <cfset variables.rootUrl = arguments.settings.rootUrl /> <!--- get the system file separator ---> <cfset variables.fileSeparator = createObject("java","java.io.File").separator /> <!--- jl: limit uploads to under a certain file size ---> <cfset variables.maxfilesize = 0 /> <cfif StructKeyExists(arguments.settings, 'maxfilesize') And IsNumeric(arguments.settings.maxfilesize)> <cfset variables.maxfilesize = arguments.settings.maxfilesize /> </cfif> <cfreturn this /> </cffunction> uploadFile: <cffunction name="uploadFile" output="false" description="Uploads a file in the given folder" access="public" returntype="void"> <cfargument name="filefield" required="true" /> <cfargument name="path" required="false" type="string" default=""/> <cfargument name="filename" type="string" required="true" /> <cfset var basedir = getResolvedPathWithValidation(arguments.path, true) /> <cfif len(arguments.filename) AND isAllowedExtension(arguments.filename)> <cffile action="UPLOAD" filefield="#arguments.filefield#" destination="#basedir##variables.fileSeparator#" nameconflict="OVERWRITE"> <!--- jl: reject a file if it is larger than the max file size. Max file size is expected to be in bytes, which we convert to KB below. ---> <cfif variables.maxfilesize GT 0> <cfif (cffile.fileSize GT (variables.maxfilesize * 1024))> <cfset var result = removeFile(arguments.path, arguments.filename) /> <cfthrow message='"#getExtension(arguments.filename)#" exceeds the maximum file size.' /> </cfif> </cfif> <cfelse> <cfthrow message='"#getExtension(arguments.filename)#" is not a valid extension.' /> </cfif> </cffunction> Finally, I made a couple of small changes to admin/files.cfm: <cfset currentAuthor = request.blogManager.getCurrentUser() />
<cfset currentBlog = request.blogManager.getBlog() /> <cfset currentRole = currentAuthor.getCurrentRole(currentBlog.getId())/> <!---jl: message for file upload limit if one is set in the config ---> <cfset currentBlogAssets = currentBlog.getSetting('assets') /> ... <h2 class="pageTitle">File Explorer</h2> <cfoutput> <!---jl: Display the Max File Size limit to the user ---> <cfif StructKeyExists(currentBlogAssets, 'maxfilesize') And IsNumeric(currentBlogAssets.maxfilesize)> <img class="helpIcon" src="assets/images/icons/help.png" /> <span class="hint"> You cannot upload any files larger than #currentBlogAssets.maxfilesize# KB. Any uploads larger than this will fail. </span> </cfif> |
| Link | Top | Bottom | |
New Post