Search in sources :

Example 6 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile in project eweb4j-framework by laiweiwei.

the class UploadValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    long countAllSize = 0;
    for (FieldConfigBean f : val.getField()) {
        String fieldName = f.getName();
        String msg = f.getMessage();
        List<UploadFile> files = context.getUploadMap().get(fieldName);
        if (files == null || files.isEmpty())
            continue;
        for (UploadFile file : files) {
            String fileName = file.getFileName();
            String fileContentType = file.getContentType();
            String tmpDir = null;
            long memoryMax = 0;
            String memoryMaxStr = null;
            long allSizeMax = 0;
            String allSizeMaxStr = null;
            long oneSizeMax = 0;
            String oneSizeMaxStr = null;
            String[] suffixArray = null;
            for (ParamConfigBean p : f.getParam()) {
                if (Validators.UPLOAD_TMP_DIR.equalsIgnoreCase(p.getName())) {
                    tmpDir = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_MEMORY_SIZE.equalsIgnoreCase(p.getName())) {
                    memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(p.getValue()) + "");
                    memoryMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_REQ_SIZE.equalsIgnoreCase(p.getName())) {
                    allSizeMax = CommonUtil.parseFileSize(p.getValue());
                    allSizeMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_MAX_FILE_SIZE.equalsIgnoreCase(p.getName())) {
                    oneSizeMax = CommonUtil.parseFileSize(p.getValue());
                    oneSizeMaxStr = p.getValue();
                    continue;
                }
                if (Validators.UPLOAD_SUFFIX.equalsIgnoreCase(p.getName())) {
                    suffixArray = p.getValue().split(",");
                    continue;
                }
            }
            if (suffixArray != null && suffixArray.length > 0) {
                boolean isOk = false;
                for (String suffix : suffixArray) {
                    if (fileName.toLowerCase().endsWith("." + suffix.toLowerCase())) {
                        isOk = true;
                        break;
                    }
                }
                if (!isOk) {
                    valError.put(fieldName, getParamValue(Validators.UPLOAD_SUFFIX_MSG, f.getParam()).replace("{suffix}", Arrays.asList(suffixArray).toString()));
                    break;
                }
            }
            long fileSize = file.getSize();
            if (fileSize > oneSizeMax) {
                valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_FILE_SIZE_MSG, f.getParam()).replace("{maxFileSize}", oneSizeMaxStr));
                break;
            }
            countAllSize += fileSize;
            if (countAllSize > allSizeMax) {
                valError.put(fieldName, getParamValue(Validators.UPLOAD_MAX_REQ_SIZE_MSG, f.getParam()).replace("{maxRequestSize}", allSizeMaxStr));
                break;
            }
        }
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) UploadFile(org.eweb4j.mvc.upload.UploadFile) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Aggregations

UploadFile (org.eweb4j.mvc.upload.UploadFile)6 HashMap (java.util.HashMap)4 List (java.util.List)3 Entry (java.util.Map.Entry)3 File (java.io.File)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 ActionExecution (org.eweb4j.mvc.action.ActionExecution)2 Validation (org.eweb4j.mvc.action.Validation)2 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)2 InterExecution (org.eweb4j.mvc.interceptor.InterExecution)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1