net.kindeditor.util.ConstraintChecker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of filemanager Show documentation
Show all versions of filemanager Show documentation
KindEditor(http://kindeditor.org/) is a lightweight, Open Source(LGPL), cross browser, web based WYSIWYG HTML editor.
kind-file-manager is the upload manager and file manager for kindeditor,
it use Servlet 3.0 technology, and can be deployed as a single web fragment jar.
The newest version!
package net.kindeditor.util;
import static net.kindeditor.util.Constants.*;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletException;
/**
* Convenient class for check subdirectory, file upload size, file name extensions, etc.
* Kind Editor predefined some subdirectories in upload root directory, such as: image,flash,file,media {@link Constants}.
* Both {@link UploadServlet} and {@link FileManagerServlet} needs check subdirectory's existence.
* @author luyanfei
*
*/
public class ConstraintChecker {
private static final String DEFAULT_SUBDIRECTORY = "image";
private final Map> extsMap = new HashMap>();
public ConstraintChecker(Properties properties) {
String imgExts = properties.getProperty(IMG_DIR_EXT);
extsMap.put(properties.getProperty(IMG_DIR), Arrays.asList(imgExts.split(",")));
String fileExts = properties.getProperty(FILE_DIR_EXT);
extsMap.put(properties.getProperty(FILE_DIR), Arrays.asList(fileExts.split(",")));
String mediaExts = properties.getProperty(FILE_DIR_EXT);
extsMap.put(properties.getProperty(MEDIA_DIR), Arrays.asList(mediaExts.split(",")));
String flashExts = properties.getProperty(FLASH_DIR_EXT);
extsMap.put(properties.getProperty(FLASH_DIR), Arrays.asList(flashExts.split(",")));
}
/**
* Check subdirectory's existence.
* @param rootPath
* Upload root path.
* @param subdir
* Subdirectory in root path.
* @return
* File object of the subdirectory.
* @throws ServletException
* Subdirectory doesn't exist or is not a directory.
*/
public File checkSubDirectory(String rootPath, String subdir)
throws ServletException {
subdir = subdir == null ? DEFAULT_SUBDIRECTORY : subdir;
File subDirectory = new File(rootPath, subdir);
if (!subDirectory.isDirectory()) {
throw new ServletException("Invalid Subirectory.");
}
return subDirectory;
}
/**
* Check weather ext is in subdir's allowed exts list.
* @param subdir
* Subdirectory to be checked.
* @param ext
* File's extension.
* @return
* true if ext is permitted in this subdir, otherwise return false.
*/
public boolean checkFileExtension(String subdir, String ext) {
if(subdir == null || ext == null)
return false;
List exts = extsMap.get(subdir);
return exts == null ? false : exts.contains(ext);
}
}