|
@@ -1,7 +1,10 @@
|
|
package com.qmth.sop.common.util;
|
|
package com.qmth.sop.common.util;
|
|
|
|
|
|
import com.qmth.boot.core.fss.service.FileService;
|
|
import com.qmth.boot.core.fss.service.FileService;
|
|
|
|
+import com.qmth.boot.core.fss.store.FileStore;
|
|
import com.qmth.sop.common.config.DictionaryConfig;
|
|
import com.qmth.sop.common.config.DictionaryConfig;
|
|
|
|
+import com.qmth.sop.common.contant.SystemConstant;
|
|
|
|
+import com.qmth.sop.common.enums.ExceptionResultEnum;
|
|
import com.qmth.sop.common.enums.UploadFileEnum;
|
|
import com.qmth.sop.common.enums.UploadFileEnum;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
@@ -12,6 +15,8 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
+import java.time.Duration;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Description: 文件存储工具类
|
|
* @Description: 文件存储工具类
|
|
@@ -28,6 +33,26 @@ public class FileStoreUtil {
|
|
@Resource
|
|
@Resource
|
|
private DictionaryConfig dictionaryConfig;
|
|
private DictionaryConfig dictionaryConfig;
|
|
|
|
|
|
|
|
+ public boolean uploadFileEnumIsOss(UploadFileEnum type) {
|
|
|
|
+ return type == UploadFileEnum.FILE ? this.publicIsOss() : this.privateIsOss();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String uploadFileEnumConfig(UploadFileEnum type) {
|
|
|
|
+ return type == UploadFileEnum.FILE ? dictionaryConfig.fssPublicDomain().getConfig() : dictionaryConfig.fssPrivateDomain().getConfig();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean privateIsOss(UploadFileEnum type) {
|
|
|
|
+ return Objects.nonNull(dictionaryConfig.fssPrivateDomain()) ? dictionaryConfig.fssPrivateDomain().getConfig().trim().startsWith("oss:") : false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean publicIsOss() {
|
|
|
|
+ return Objects.nonNull(dictionaryConfig.fssPublicDomain()) ? dictionaryConfig.fssPublicDomain().getConfig().trim().startsWith("oss:") : false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean privateIsOss() {
|
|
|
|
+ return Objects.nonNull(dictionaryConfig.fssPrivateDomain()) ? dictionaryConfig.fssPrivateDomain().getConfig().trim().startsWith("oss:") : false;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 上传文件
|
|
* 上传文件
|
|
*
|
|
*
|
|
@@ -41,44 +66,6 @@ public class FileStoreUtil {
|
|
log.info("dirName:{}", dirName);
|
|
log.info("dirName:{}", dirName);
|
|
}
|
|
}
|
|
|
|
|
|
-// /**
|
|
|
|
-// * 上传文件到本地
|
|
|
|
-// *
|
|
|
|
-// * @param dirName 上传到地址
|
|
|
|
-// * @param inputStream 流
|
|
|
|
-// * @param catalogType 文件
|
|
|
|
-// */
|
|
|
|
-// public void localUpload(String dirName, InputStream inputStream, String md5, LocalCatalogEnum catalogType) throws Exception {
|
|
|
|
-// log.info("ossUpload is come in");
|
|
|
|
-// String configPath = "";
|
|
|
|
-// switch (catalogType) {
|
|
|
|
-// case LOCAL_FILE:
|
|
|
|
-// configPath = dictionaryConfig.fssLocalFileDomain().getConfig();
|
|
|
|
-// break;
|
|
|
|
-// case LOCAL_PDF:
|
|
|
|
-// configPath = dictionaryConfig.fssLocalPdfDomain().getConfig();
|
|
|
|
-// break;
|
|
|
|
-// default:
|
|
|
|
-// break;
|
|
|
|
-// }
|
|
|
|
-// dirName = dirName.replaceAll(configPath, "");
|
|
|
|
-// fileService.getFileStore(catalogType.getType()).write(dirName, inputStream, md5);
|
|
|
|
-// log.info("dirName:{}", dirName);
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// /**
|
|
|
|
-// * 上传文件到本地
|
|
|
|
-// *
|
|
|
|
-// * @param inputStream 流
|
|
|
|
-// * @param finalFile 最终文件
|
|
|
|
-// * @param catalogType 文件类型
|
|
|
|
-// * @throws Exception 异常
|
|
|
|
-// */
|
|
|
|
-// public void copyInputStreamToFile(InputStream inputStream, File finalFile, String md5, LocalCatalogEnum catalogType) throws Exception {
|
|
|
|
-// String dirName = finalFile.getPath().replaceAll("\\\\", "/");
|
|
|
|
-// this.localUpload(dirName, inputStream, md5, catalogType);
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 上传文件
|
|
* 上传文件
|
|
*
|
|
*
|
|
@@ -118,6 +105,20 @@ public class FileStoreUtil {
|
|
return this.saveLocal(fileService.getFileStore(type).read(dirName), localPath);
|
|
return this.saveLocal(fileService.getFileStore(type).read(dirName), localPath);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 从文件存储上下载文件到本地
|
|
|
|
+ *
|
|
|
|
+ * @param dirName 文件地址
|
|
|
|
+ * @param localFile 本地路径
|
|
|
|
+ * @param type fileStore类型
|
|
|
|
+ * @throws Exception 异常
|
|
|
|
+ */
|
|
|
|
+ public File ossDownload(String dirName, File localFile, String type) throws Exception {
|
|
|
|
+ log.info("ossDownload is come in");
|
|
|
|
+ InputStream inputStream = fileService.getFileStore(type).read(dirName);
|
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, localFile);
|
|
|
|
+ return localFile;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 从文件存储上下载文件到byte[]
|
|
* 从文件存储上下载文件到byte[]
|
|
@@ -132,18 +133,18 @@ public class FileStoreUtil {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 从文件存储上下载文件到InputStream
|
|
|
|
|
|
+ * oss删除
|
|
*
|
|
*
|
|
- * @param objectName 文件地址
|
|
|
|
- * @param type fileStore类型
|
|
|
|
- * @throws Exception 异常
|
|
|
|
|
|
+ * @param objectName
|
|
|
|
+ * @param type
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
*/
|
|
*/
|
|
- public InputStream ossDownloadIs(String objectName, String type) throws Exception {
|
|
|
|
- log.info("oss Download is come in");
|
|
|
|
- return fileService.getFileStore(type).read(objectName);
|
|
|
|
|
|
+ public boolean ossDelete(String objectName, String type) throws Exception {
|
|
|
|
+ log.info("oss delete is come in");
|
|
|
|
+ return fileService.getFileStore(type).delete(objectName);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取文件访问url
|
|
* 获取文件访问url
|
|
*
|
|
*
|
|
@@ -153,20 +154,47 @@ public class FileStoreUtil {
|
|
*/
|
|
*/
|
|
public String getPrivateUrl(String objectPath, String type) {
|
|
public String getPrivateUrl(String objectPath, String type) {
|
|
String server = null;
|
|
String server = null;
|
|
-// if ("public".equals(type)) {
|
|
|
|
- server = dictionaryConfig.fssPublicDomain().getServer();
|
|
|
|
- return server + File.separator + objectPath;
|
|
|
|
-// } else if ("private".equals(type)) {
|
|
|
|
-// Boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
|
-// if (Objects.nonNull(oss) && oss) {
|
|
|
|
-// FileStore fileStore = fileService.getFileStore(type);
|
|
|
|
-// return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(5L));
|
|
|
|
-// } else {
|
|
|
|
-// return dictionaryConfig.fssPrivateDomain().getServer() + "/" + objectPath;
|
|
|
|
-// }
|
|
|
|
-// } else {
|
|
|
|
-// throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
|
|
|
|
-// }
|
|
|
|
|
|
+ if ("public".equals(type)) {
|
|
|
|
+ server = dictionaryConfig.fssPublicDomain().getServer();
|
|
|
|
+ return server + SystemConstant.ORG_SPLIT + objectPath;
|
|
|
|
+ } else if ("private".equals(type)) {
|
|
|
|
+ if (this.privateIsOss()) {
|
|
|
|
+ FileStore fileStore = fileService.getFileStore(type);
|
|
|
|
+ return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(5L));
|
|
|
|
+ } else {
|
|
|
|
+ return dictionaryConfig.fssPrivateDomain().getServer() + objectPath;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取文件访问url
|
|
|
|
+ *
|
|
|
|
+ * @param objectPath 文件路径
|
|
|
|
+ * @param type 文件上传的类型
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String getPrivateUrlExpire(String objectPath, String type, Boolean isExpire) {
|
|
|
|
+ String server = null;
|
|
|
|
+ if ("public".equals(type)) {
|
|
|
|
+ server = dictionaryConfig.fssPublicDomain().getServer();
|
|
|
|
+ return server + SystemConstant.ORG_SPLIT + objectPath;
|
|
|
|
+ } else if ("private".equals(type)) {
|
|
|
|
+ if (this.privateIsOss()) {
|
|
|
|
+ FileStore fileStore = fileService.getFileStore(type);
|
|
|
|
+ if (isExpire) {
|
|
|
|
+ return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(5L));
|
|
|
|
+ } else {
|
|
|
|
+ return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(300L));
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ return dictionaryConfig.fssPrivateDomain().getServer() + SystemConstant.ORG_SPLIT + objectPath;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -176,7 +204,7 @@ public class FileStoreUtil {
|
|
* @return 类型
|
|
* @return 类型
|
|
*/
|
|
*/
|
|
public UploadFileEnum getUploadEnumByPath(String path) {
|
|
public UploadFileEnum getUploadEnumByPath(String path) {
|
|
- path = path.replaceAll("\\\\", "/");
|
|
|
|
|
|
+ path = path.replaceAll("\\\\", SystemConstant.ORG_SPLIT);
|
|
String target = path.substring(0, path.indexOf('/'));
|
|
String target = path.substring(0, path.indexOf('/'));
|
|
return UploadFileEnum.valueOf(target.toUpperCase());
|
|
return UploadFileEnum.valueOf(target.toUpperCase());
|
|
}
|
|
}
|
|
@@ -237,4 +265,8 @@ public class FileStoreUtil {
|
|
}
|
|
}
|
|
return file;
|
|
return file;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public DictionaryConfig getDictionaryConfig() {
|
|
|
|
+ return dictionaryConfig;
|
|
|
|
+ }
|
|
}
|
|
}
|