|
@@ -0,0 +1,421 @@
|
|
|
+package com.fdkankan.scene.oss;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.fdkankan.scene.config.FdkkLaserConfig;
|
|
|
+import com.fdkankan.scene.config.ServiceConfig;
|
|
|
+import com.fdkankan.scene.oss.aliyun.AliyunOssTemplate;
|
|
|
+import com.fdkankan.scene.oss.minio.MinioOssTemplate;
|
|
|
+import com.fdkankan.scene.util.CmdBuildUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.Base64Utils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OssUtil {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AliyunOssTemplate aliyunOssTemplate;
|
|
|
+ @Autowired
|
|
|
+ MinioOssTemplate minioOssTemplate;
|
|
|
+ @Autowired
|
|
|
+ FdkkLaserConfig fdkkLaserConfig;
|
|
|
+ @Autowired
|
|
|
+ ServiceConfig serviceConfig;
|
|
|
+
|
|
|
+ public String getPathKey(String sceneCode, String type, String fileName) {
|
|
|
+ return fdkkLaserConfig.getDefaultFolder() + "/" + sceneCode + "/temp/" + type + "/" + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void rmFileCmd(String targetDir) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ minioOssTemplate.rmFileCmd(targetDir);
|
|
|
+ } else {
|
|
|
+ aliyunOssTemplate.rmFileCmd(targetDir);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.del(FdkkLaserConfig.getProfile() + File.separator + targetDir.replaceAll("/", Matcher.quoteReplacement(File.separator)));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getHostByBucket(String bucket) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.calculateUrl(bucket, "");
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.calculateUrl(bucket, "");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return serviceConfig.getBaseUrl() + "profile";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void uploadFileChunkCmd(String defaultFolder, String sceneCode, String uploadDir, String targetDir, String mergeCode, boolean isLink) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ minioOssTemplate.uploadFileChunkCmd(defaultFolder, sceneCode, uploadDir, targetDir, mergeCode);
|
|
|
+ } else {
|
|
|
+ aliyunOssTemplate.uploadFileChunkCmd(defaultFolder, sceneCode, uploadDir, targetDir, mergeCode);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isLink) {
|
|
|
+ String path = FdkkLaserConfig.getProfile() + File.separator + defaultFolder + File.separator + sceneCode + File.separator + "data" + File.separator + mergeCode;
|
|
|
+ String targetPath = path + File.separator + targetDir;
|
|
|
+ if (!FileUtil.exist(path)) {
|
|
|
+ FileUtil.mkdir(path);
|
|
|
+ }
|
|
|
+ if (FileUtil.exist(targetPath)) {
|
|
|
+ FileUtil.del(targetPath);
|
|
|
+ }
|
|
|
+ CmdBuildUtil.MkLinkDir(targetPath, FileUtil.file(uploadDir).getAbsolutePath());
|
|
|
+ } else {
|
|
|
+ String path = FdkkLaserConfig.getProfile() + File.separator + defaultFolder + File.separator + sceneCode + File.separator + "data" + File.separator + mergeCode;
|
|
|
+ if (FileUtil.exist(FileUtil.file(uploadDir))) {
|
|
|
+ FileUtil.copy(
|
|
|
+ FileUtil.file(uploadDir),
|
|
|
+ FileUtil.file(path),
|
|
|
+ true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void uploadFileDirCmd(String uploadDir, String target, Boolean isLink) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ minioOssTemplate.uploadFileDirCmd(uploadDir, target);
|
|
|
+ } else {
|
|
|
+ aliyunOssTemplate.uploadFileDirCmd(uploadDir, target);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isLink) {
|
|
|
+ String path = FdkkLaserConfig.getProfile() + File.separator + target;
|
|
|
+ File folder = FileUtil.file(path);
|
|
|
+ if (!FileUtil.exist(folder.getParent())) {
|
|
|
+ FileUtil.mkdir(folder.getParent());
|
|
|
+ }
|
|
|
+ CmdBuildUtil.MkLinkDir(folder.getAbsolutePath(), FileUtil.file(uploadDir).getAbsolutePath());
|
|
|
+ } else {
|
|
|
+ File[] ls = FileUtil.ls(uploadDir);
|
|
|
+ for (File l : ls) {
|
|
|
+ FileUtil.copy(
|
|
|
+ FileUtil.file(l),
|
|
|
+ FileUtil.file(FdkkLaserConfig.getProfile() + File.separator + target),
|
|
|
+ true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFile(String pathKey, String filePath, boolean isLink) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFile(pathKey, filePath);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFile(pathKey, filePath);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isLink) {
|
|
|
+ File file = FileUtil.file(filePath);
|
|
|
+ if (!FileUtil.exist(file.getParent())) {
|
|
|
+ FileUtil.mkdir(file.getParent());
|
|
|
+ }
|
|
|
+ File pathKeyFile = FileUtil.file(FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ if (!FileUtil.exist(pathKeyFile.getParent())) {
|
|
|
+ FileUtil.mkdir(pathKeyFile.getParent());
|
|
|
+ }
|
|
|
+ CmdBuildUtil.MkLinkFile(pathKeyFile.getAbsolutePath(), file.getAbsolutePath());
|
|
|
+ } else {
|
|
|
+ FileUtil.copy(filePath,
|
|
|
+ FdkkLaserConfig.getProfile() + File.separator + pathKey,
|
|
|
+ true);
|
|
|
+ }
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileText(String pathKey, String text) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileText(pathKey, text);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileText(pathKey, text);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeBytes(text.getBytes(StandardCharsets.UTF_8), FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileText(String bucket, String pathKey, String text) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileText(bucket, pathKey, text);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileText(bucket, pathKey, text);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeBytes(text.getBytes(StandardCharsets.UTF_8), FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileBase64Image(String pathKey, String base64) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileBase64Image(pathKey, base64);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileBase64Image(pathKey, base64);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ byte[] bytes = Base64Utils.decodeFromString(base64);
|
|
|
+ FileUtil.writeBytes(bytes, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileBase64Image(String bucket, String pathKey, String base64) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileBase64Image(bucket, pathKey, base64);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileBase64Image(bucket, pathKey, base64);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ byte[] bytes = Base64Utils.decodeFromString(base64);
|
|
|
+ FileUtil.writeBytes(bytes, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileBytes(String pathKey, byte[] bytes) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileBytes(pathKey, bytes);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileBytes(pathKey, bytes);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeBytes(bytes, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileBytes(String bucket, String pathKey, byte[] bytes) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileBytes(bucket, pathKey, bytes);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileBytes(bucket, pathKey, bytes);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeBytes(bytes, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileStream(String pathKey, InputStream stream) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileStream(pathKey, stream);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileStream(pathKey, stream);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeFromStream(stream, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileStream(String bucket, String pathKey, InputStream stream) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileStream(bucket, pathKey, stream);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileStream(bucket, pathKey, stream);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.writeFromStream(stream, FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadFileImage(String pathKey, String filePath, boolean isLink) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.uploadFileImage(pathKey, filePath);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.uploadFileImage(pathKey, filePath);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isLink) {
|
|
|
+ File file = FileUtil.file(filePath);
|
|
|
+ if (!FileUtil.exist(file.getParent())) {
|
|
|
+ FileUtil.mkdir(file.getParent());
|
|
|
+ }
|
|
|
+ File pathKeyFile = FileUtil.file(FdkkLaserConfig.getProfile() + File.separator + pathKey);
|
|
|
+ if (!FileUtil.exist(pathKeyFile.getParent())) {
|
|
|
+ FileUtil.mkdir(pathKeyFile.getParent());
|
|
|
+ }
|
|
|
+ CmdBuildUtil.MkLinkFile(pathKeyFile.getAbsolutePath(), file.getAbsolutePath());
|
|
|
+ } else {
|
|
|
+ FileUtil.copy(filePath,
|
|
|
+ FdkkLaserConfig.getProfile() + File.separator + pathKey,
|
|
|
+ true);
|
|
|
+ }
|
|
|
+
|
|
|
+ return pathKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public File downloadFileTmp(String pathKey) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.downloadFileTmp(pathKey);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.downloadFileTmp(pathKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public File downloadFileTmp(String bucket, String pathKey) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.downloadFileTmp(bucket, pathKey);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.downloadFileTmp(bucket, pathKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object downloadFile(String pathKey, String file) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.downloadFile(pathKey, file);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.downloadFile(pathKey, file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object downloadFile(String bucket, String pathKey, String file) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.downloadFile(bucket, pathKey, file);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.downloadFile(bucket, pathKey, file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Boolean copyObject(String oldPath, String newPath) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.copyObject(oldPath, newPath);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.copyObject(oldPath, newPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Boolean copyObject(String bucket, String oldPath, String toBucket, String newPath) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.copyObject(bucket, oldPath, toBucket, newPath);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.copyObject(bucket, oldPath, toBucket, newPath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void deleteObject(String keyName) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ minioOssTemplate.deleteObject(keyName);
|
|
|
+ } else {
|
|
|
+ aliyunOssTemplate.deleteObject(keyName);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FileUtil.del(
|
|
|
+ FdkkLaserConfig.getProfile() + File.separator + keyName
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void deleteObject(String bucket, String keyName) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<String> getFileFolder(String keyName) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public List<String> getFileFolder(String bucket, String keyName) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean doesObjectExist(String bucket, String keyName) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.doesObjectExist(bucket, keyName);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.doesObjectExist(bucket, keyName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean doesObjectExist(String keyName) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return minioOssTemplate.doesObjectExist(keyName);
|
|
|
+ } else {
|
|
|
+ return aliyunOssTemplate.doesObjectExist(keyName);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return FileUtil.exist(FdkkLaserConfig.getProfile() + File.separator + keyName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object downloadFile(String bucket, String pathKey, Consumer handler) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object downloadFile(String pathKey, Consumer handler) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String calculateUrl(String keyName) {
|
|
|
+ if (FdkkLaserConfig.isIsBucket()) {
|
|
|
+ if (FdkkLaserConfig.isLocalization()) {
|
|
|
+ return keyName;
|
|
|
+ } else {
|
|
|
+ return keyName;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return FdkkLaserConfig.getProfile() + File.separator + keyName;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|