
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
package froala.editor.file;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.ArrayUtils;
import froala.editor.CustomValidation;
/**
* File validation.
*
* @author florin@froala.com
*/
public class FileValidation {
/**
* Allowed file validation default extensions.
*/
public static final String[] allowedFileExtsDefault = new String[] { "txt", "pdf", "doc", "hwp" };
/**
* Allowed file validation default mimetypes.
*/
public static final String[] allowedFileMimeTypesDefault = new String[] { "text/plain", "application/msword",
"application/x-pdf", "application/pdf", "application/haansofthwp" };
/**
* Allowed validation extensions.
*/
protected String[] allowedExts;
/**
* Allowed validation mimetypes.
*/
protected String[] allowedMimeTypes;
/**
* Custom file validation.
*/
protected CustomValidation customValidation;
/**
* Init default file validation settings.
*/
protected void initDefault() {
allowedExts = allowedFileExtsDefault;
allowedMimeTypes = allowedFileMimeTypesDefault;
}
/**
* Constructor. Validates default files with: - allowed file extensions:
* "txt", "pdf", "doc" - allowed mime types: "text/plain",
* "application/msword", "application/x-pdf", "application/pdf"
*/
public FileValidation() {
initDefault();
}
/**
* Constructor.
*
* @param allowedExts
* Allowed validation file extensions.
* @param allowedMimeTypes
* Allowed validation file mimetypes.
*/
public FileValidation(String[] allowedExts, String[] allowedMimeTypes) {
initDefault();
if (allowedExts != null) {
int cnt = allowedExts.length;
this.allowedExts = new String[cnt];
for (int i = 0; i < cnt; i++) {
this.allowedExts[i] = allowedExts[i];
}
}
if (allowedMimeTypes != null) {
int cnt = allowedMimeTypes.length;
this.allowedMimeTypes = new String[cnt];
for (int i = 0; i < cnt; i++) {
this.allowedMimeTypes[i] = allowedMimeTypes[i];
}
}
}
/**
* Constructor.
*
* @param customValidation
* Custom validation.
*/
public FileValidation(CustomValidation customValidation) {
initDefault();
this.customValidation = customValidation;
}
/**
* Check if file is valid. Use only the custom function if provided. Else
* check if the file has an allowed extension and mimetype.
*
* @param filePath
* File path.
* @param mimeType
* File mimetype
* @return
*/
public boolean check(String filePath, String mimeType) {
if (customValidation != null) {
return customValidation.validate(filePath, mimeType);
}
return ArrayUtils.contains(allowedExts, FilenameUtils.getExtension(filePath).toLowerCase())
&& ArrayUtils.contains(allowedMimeTypes, mimeType.toLowerCase());
}
}