com.github.javaclub.ossclient.util.AssetsUtils Maven / Gradle / Ivy
package com.github.javaclub.ossclient.util;
import java.io.File;
import java.util.Map;
import com.github.javaclub.BizException;
import com.github.javaclub.ossclient.OssConstants;
import com.github.javaclub.ossclient.OssConstants.LocalStorage;
import com.github.javaclub.toolbox.ToolBox.Objects;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
public class AssetsUtils {
static String[] CAN_VIEW_FILES = new String[] {
".jpg", ".jpeg", ".png", ".pdf",
".txt", ".ini", ".conf", ".xml",
".java", ".html", ".css", ".js",
".json", ".properties"
};
public static boolean canWebView(String filename) {
return Strings.endsWith(filename, CAN_VIEW_FILES);
}
public static boolean isImage(String filename) {
return Strings.endsWith(filename, new String[] {
".jpg", ".jpeg", ".png"
});
}
public static boolean isPDF(String filename) {
return Strings.endsWithIgnoreCase(filename, ".pdf");
}
public static String getFileExt(String filename) {
String suffix = filename.substring(filename.lastIndexOf('.')).toLowerCase();
return suffix.substring(1);
}
public static File localFile(String requestFile) {
String localDir = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.FILE_DIRECTORY);
String directory = Strings.endsWithIgnoreCase(localDir, "/") ? localDir : Strings.concat(localDir, "/");
String filePath = Strings.concat(directory, Strings.replace(requestFile, "!", "/"));
return new File(filePath);
}
public static String html404() {
String html = "" +
"\n" +
" \n" +
" \n" +
" 404 Not Found \n" +
" \n" +
" \n" +
" Resource Not Found
\n" +
" \n" +
"";
return html;
}
public static void checkFileExtention(String ext, String provider, Map params) {
String extention = Strings.startsWithIgnoreCase(ext, ".") ? ext.replace(".", "") : ext;
if (null != params && null != params.get(OssConstants.FILE_FORMAT_PARAM_KEY)) { // 优先上传时即时传过来的限制
String allowedFiles = Objects.toString(params.get(OssConstants.FILE_FORMAT_PARAM_KEY));
if (!allowedFiles.contains(extention)) {
throw new BizException("上传文件格式不支持!");
}
} else if (Strings.equalsAny(provider, new String[] { "aliyun", "minio", "local" })) {
String key = Strings.format("system.configs.oss.{}.allowed-files", provider);
String allowedFiles = CompositeAppConfigProperties.getInstance().getValue(key);
if (null != allowedFiles && !allowedFiles.contains(extention)) {
throw new BizException("上传文件格式不支持!");
}
}
// 兜底限制
String allowedFiles = CompositeAppConfigProperties.getInstance().getValue(OssConstants.ALLOWED_FILES);
if (null != allowedFiles && !allowedFiles.contains(extention)) {
throw new BizException("上传文件格式不支持!");
}
}
}