Index: lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java =================================================================== diff -u -r4ae89955463ce8f525ce7cb7c473e0dc8c2432ff -r5bb4520b9a3b9b440921e96ed7e2efc5f55f812c --- lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java (.../FileValidatorUtil.java) (revision 4ae89955463ce8f525ce7cb7c473e0dc8c2432ff) +++ lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java (.../FileValidatorUtil.java) (revision 5bb4520b9a3b9b440921e96ed7e2efc5f55f812c) @@ -21,14 +21,18 @@ package org.lamsfoundation.lams.util; import org.apache.struts.validator.Resources; +import org.apache.commons.validator.Arg; import org.apache.commons.validator.ValidatorAction; import org.apache.commons.validator.Field; import org.apache.struts.action.ActionMessages; import org.apache.commons.validator.Validator; -import org.apache.commons.validator.GenericValidator; import org.apache.commons.validator.util.ValidatorUtils; import javax.servlet.http.HttpServletRequest; +import org.lamsfoundation.lams.util.Configuration; +import org.lamsfoundation.lams.util.ConfigurationKeys; +import org.apache.struts.upload.MultipartRequestWrapper; import org.apache.struts.upload.FormFile; +import org.apache.struts.action.ActionForm; /** * This class is used by commons validator. To validate various properties @@ -37,11 +41,13 @@ * @author Anthony Xiao */ public class FileValidatorUtil { + + public static final String LARGE_FILE = "largeFile"; /** * To enable this validator copy the XML entry below to validator-rules.xml * * - * Then to validate your uploaded file, by placing it under the corresponding
tag - * - * - * NOTE: You need to add an errors.maxfilesize entry to your resource bundle or - * you can change the "msg" attribute in the "validator" tag above + * Then to validate your uploaded file size, by placing it under the corresponding tag + * + * We could also use maxFileSize to validated large file sizes by setting the largeFile flag + * to true + * + * + * largeFile + * true + * + * + * + * You need to add an errors.maxfilesize entry to your resource bundle or you can change + * the "msg" attribute in the "validator" tag above. The maximum file size is pass to + * the message as arg0. So the following message or something similar could be used. + * errors.maxfilesize=The uploaded file has exceeded the maximum file size limit of {0} bytes */ public static boolean validateFileSize(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { - + + int maxFileSize; + + //whether we are using large file or not? + if(Boolean.valueOf(field.getVarValue(LARGE_FILE)).booleanValue()) + maxFileSize = Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_LARGE_MAX_SIZE); + else + maxFileSize = Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_MAX_SIZE); + String fileSizeStr = ValidatorUtils.getValueAsString(bean, field.getProperty()); int fileSize = Integer.parseInt(fileSizeStr); - if(fileSize > 1000000){ - errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); - + if(fileSize > maxFileSize){ + //Set arg0 in message bundle + Arg arg = new Arg(); + arg.setPosition(0); + arg.setResource(false); //dont treat this as a locale key + arg.setKey(String.valueOf(maxFileSize)); + field.addArg(arg); + + //set error message + errors.add(field.getKey(), Resources.getActionMessage(request, va, + field)); return false; } - return true; } }