/**
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springblade.core.boot.file;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
public class BladeFileUtil {
// 定义允许上传的文件扩展名
private static HashMap extMap = new HashMap();
// 图片扩展名
private static String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
static {
extMap.put("image", ".gif,.jpg,.jpeg,.png,.bmp,.JPG,.JPEG,.PNG");
extMap.put("flash", ".swf,.flv");
extMap.put("media", ".swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb");
extMap.put("file", ".doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2");
extMap.put("allfile", ".gif,.jpg,.jpeg,.png,.bmp,.swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb,.doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2");
}
/**
* 获取文件后缀
*
* @param @param fileName
* @param @return 设定文件
* @return String 返回类型
*/
public static String getFileExt(String fileName) {
return fileName.substring(fileName.lastIndexOf('.'), fileName.length());
}
/**
* 测试文件后缀 只让指定后缀的文件上传,像jsp,war,sh等危险的后缀禁止
*
* @return
*/
public static boolean testExt(String dir, String fileName) {
String fileExt = getFileExt(fileName);
String ext = extMap.get(dir);
if (StringUtil.isBlank(ext) || ext.indexOf(fileExt) == -1) {
return false;
}
return true;
}
/**
* 文件管理排序
*/
public enum FileSort {
size, type, name;
// 文本排序转换成枚举
public static FileSort of(String sort) {
try {
return FileSort.valueOf(sort);
} catch (Exception e) {
return FileSort.name;
}
}
}
public static class NameComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable) a;
Hashtable hashB = (Hashtable) b;
if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) {
return 1;
} else {
return ((String) hashA.get("filename")).compareTo((String) hashB.get("filename"));
}
}
}
public static class SizeComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable) a;
Hashtable hashB = (Hashtable) b;
if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) {
return 1;
} else {
if (((Long) hashA.get("filesize")) > ((Long) hashB.get("filesize"))) {
return 1;
} else if (((Long) hashA.get("filesize")) < ((Long) hashB.get("filesize"))) {
return -1;
} else {
return 0;
}
}
}
}
public static class TypeComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable) a;
Hashtable hashB = (Hashtable) b;
if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) {
return 1;
} else {
return ((String) hashA.get("filetype")).compareTo((String) hashB.get("filetype"));
}
}
}
public static String formatUrl(String url) {
return url.replaceAll("\\\\", "/");
}
/********************************BladeFile封装********************************************************/
/**
* 获取BladeFile封装类
* @param file
* @return
*/
public static BladeFile getFile(MultipartFile file){
return getFile(file, "image", null, null);
}
/**
* 获取BladeFile封装类
* @param file
* @param dir
* @return
*/
public static BladeFile getFile(MultipartFile file, String dir){
return getFile(file, dir, null, null);
}
/**
* 获取BladeFile封装类
* @param file
* @param dir
* @param path
* @param virtualPath
* @return
*/
public static BladeFile getFile(MultipartFile file, String dir, String path, String virtualPath){
return new BladeFile(file, dir, path, virtualPath);
}
/**
* 获取BladeFile封装类
* @param files
* @return
*/
public static List getFiles(List files){
return getFiles(files, "image", null, null);
}
/**
* 获取BladeFile封装类
* @param files
* @param dir
* @return
*/
public static List getFiles(List files, String dir){
return getFiles(files, dir, null, null);
}
/**
* 获取BladeFile封装类
* @param files
* @param path
* @param virtualPath
* @return
*/
public static List getFiles(List files, String dir, String path, String virtualPath){
List list = new ArrayList<>();
for (MultipartFile file : files){
list.add(new BladeFile(file, dir, path, virtualPath));
}
return list;
}
}